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