1 package serp.bytecode;
2
3 import java.io.*;
4
5 import serp.bytecode.visitor.*;
6
7 /***
8 * The <code>iinc</code> instruction.
9 *
10 * @author Abe White
11 */
12 public class IIncInstruction extends LocalVariableInstruction {
13 private int _inc = 0;
14
15 IIncInstruction(Code owner) {
16 super(owner, Constants.IINC);
17 }
18
19 int getLength() {
20 return super.getLength() + 2;
21 }
22
23 /***
24 * Return the increment for this IINC instruction.
25 */
26 public int getIncrement() {
27 return _inc;
28 }
29
30 /***
31 * Set the increment on this IINC instruction.
32 *
33 * @return this Instruction, for method chaining
34 */
35 public IIncInstruction setIncrement(int val) {
36 _inc = val;
37 return this;
38 }
39
40 public boolean equalsInstruction(Instruction other) {
41 if (this == other)
42 return true;
43 if (!(other instanceof IIncInstruction))
44 return false;
45 if (!super.equalsInstruction(other))
46 return false;
47
48 IIncInstruction ins = (IIncInstruction) other;
49 return getIncrement() == 0 || ins.getIncrement() == 0
50 || getIncrement() == ins.getIncrement();
51 }
52
53 public void acceptVisit(BCVisitor visit) {
54 visit.enterIIncInstruction(this);
55 visit.exitIIncInstruction(this);
56 }
57
58 void read(Instruction other) {
59 super.read(other);
60 _inc = ((IIncInstruction) other).getIncrement();
61 }
62
63 void read(DataInput in) throws IOException {
64 super.read(in);
65 setLocal(in.readUnsignedByte());
66 _inc = in.readByte();
67 }
68
69 void write(DataOutput out) throws IOException {
70 super.write(out);
71 out.writeByte(getLocal());
72 out.writeByte(_inc);
73 }
74 }