001    package serp.bytecode;
002    
003    import java.io.*;
004    
005    import serp.bytecode.visitor.*;
006    
007    /**
008     * The <code>iinc</code> instruction.
009     *
010     * @author Abe White
011     */
012    public class IIncInstruction extends LocalVariableInstruction {
013        private int _inc = 0;
014    
015        IIncInstruction(Code owner) {
016            super(owner, Constants.IINC);
017        }
018    
019        int getLength() {
020            return super.getLength() + 2;
021        }
022    
023        /**
024         * Return the increment for this IINC instruction.
025         */
026        public int getIncrement() {
027            return _inc;
028        }
029    
030        /**
031         * Set the increment on this IINC instruction.
032         *
033         * @return this Instruction, for method chaining
034         */
035        public IIncInstruction setIncrement(int val) {
036            _inc = val;
037            return this;
038        }
039    
040        public boolean equalsInstruction(Instruction other) {
041            if (this == other)
042                return true;
043            if (!(other instanceof IIncInstruction))
044                return false;
045            if (!super.equalsInstruction(other))
046                return false;
047    
048            IIncInstruction ins = (IIncInstruction) other;
049            return getIncrement() == 0 || ins.getIncrement() == 0 
050                || getIncrement() == ins.getIncrement();
051        }
052    
053        public void acceptVisit(BCVisitor visit) {
054            visit.enterIIncInstruction(this);
055            visit.exitIIncInstruction(this);
056        }
057    
058        void read(Instruction other) {
059            super.read(other);
060            _inc = ((IIncInstruction) other).getIncrement();
061        }
062    
063        void read(DataInput in) throws IOException {
064            super.read(in);
065            setLocal(in.readUnsignedByte());
066            _inc = in.readByte();
067        }
068    
069        void write(DataOutput out) throws IOException {
070            super.write(out);
071            out.writeByte(getLocal());
072            out.writeByte(_inc);
073        }
074    }