1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.bcel.generic;
20
21 import java.io.DataOutputStream;
22 import java.io.IOException;
23
24 import org.apache.bcel.Const;
25 import org.apache.bcel.util.ByteSequence;
26
27
28
29
30 public class IINC extends LocalVariableInstruction {
31
32 private boolean wide;
33 private int c;
34
35
36
37
38 IINC() {
39 }
40
41
42
43
44
45 public IINC(final int n, final int c) {
46
47 super.setOpcode(Const.IINC);
48 super.setLength((short) 3);
49 setIndex(n);
50 setIncrement(c);
51 }
52
53
54
55
56
57
58
59 @Override
60 public void accept(final Visitor v) {
61 v.visitLocalVariableInstruction(this);
62 v.visitIINC(this);
63 }
64
65
66
67
68
69
70 @Override
71 public void dump(final DataOutputStream out) throws IOException {
72 if (wide) {
73 out.writeByte(Const.WIDE);
74 }
75 out.writeByte(super.getOpcode());
76 if (wide) {
77 out.writeShort(super.getIndex());
78 out.writeShort(c);
79 } else {
80 out.writeByte(super.getIndex());
81 out.writeByte(c);
82 }
83 }
84
85
86
87
88 public final int getIncrement() {
89 return c;
90 }
91
92
93
94
95 @Override
96 public Type getType(final ConstantPoolGen cp) {
97 return Type.INT;
98 }
99
100
101
102
103 @Override
104 protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException {
105 this.wide = wide;
106 if (wide) {
107 super.setLength(6);
108 super.setIndexOnly(bytes.readUnsignedShort());
109 c = bytes.readShort();
110 } else {
111 super.setLength(3);
112 super.setIndexOnly(bytes.readUnsignedByte());
113 c = bytes.readByte();
114 }
115 }
116
117
118
119
120 public final void setIncrement(final int c) {
121 this.c = c;
122 setWide();
123 }
124
125
126
127
128 @Override
129 public final void setIndex(final int n) {
130 if (n < 0) {
131 throw new ClassGenException("Negative index value: " + n);
132 }
133 super.setIndexOnly(n);
134 setWide();
135 }
136
137 private void setWide() {
138 wide = super.getIndex() > Const.MAX_BYTE;
139 if (c > 0) {
140 wide = wide || c > Byte.MAX_VALUE;
141 } else {
142 wide = wide || c < Byte.MIN_VALUE;
143 }
144 if (wide) {
145 super.setLength(6);
146 } else {
147 super.setLength(3);
148 }
149 }
150
151
152
153
154
155
156 @Override
157 public String toString(final boolean verbose) {
158 return super.toString(verbose) + " " + c;
159 }
160 }