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 abstract class LocalVariableInstruction extends Instruction implements TypedInstruction, IndexedInstruction {
31
32
33
34
35 @Deprecated
36 protected int n = -1;
37
38 private short cTag = -1;
39 private short canonTag = -1;
40
41
42
43
44 LocalVariableInstruction() {
45 }
46
47
48
49
50
51 LocalVariableInstruction(final short canonTag, final short cTag) {
52 this.canonTag = canonTag;
53 this.cTag = cTag;
54 }
55
56
57
58
59
60
61 protected LocalVariableInstruction(final short opcode, final short cTag, final int n) {
62 super(opcode, (short) 2);
63 this.cTag = cTag;
64 canonTag = opcode;
65 setIndex(n);
66 }
67
68
69
70
71
72
73 @Override
74 public void dump(final DataOutputStream out) throws IOException {
75 if (wide()) {
76 out.writeByte(Const.WIDE);
77 }
78 out.writeByte(super.getOpcode());
79 if (super.getLength() > 1) {
80 if (wide()) {
81 out.writeShort(n);
82 } else {
83 out.writeByte(n);
84 }
85 }
86 }
87
88
89
90
91 public short getCanonicalTag() {
92 return canonTag;
93 }
94
95
96
97
98 @Override
99 public final int getIndex() {
100 return n;
101 }
102
103
104
105
106
107
108
109
110 @Override
111 public Type getType(final ConstantPoolGen cp) {
112 switch (canonTag) {
113 case Const.ILOAD:
114 case Const.ISTORE:
115 return Type.INT;
116 case Const.LLOAD:
117 case Const.LSTORE:
118 return Type.LONG;
119 case Const.DLOAD:
120 case Const.DSTORE:
121 return Type.DOUBLE;
122 case Const.FLOAD:
123 case Const.FSTORE:
124 return Type.FLOAT;
125 case Const.ALOAD:
126 case Const.ASTORE:
127 return Type.OBJECT;
128 default:
129 throw new ClassGenException("Unknown case in switch" + canonTag);
130 }
131 }
132
133
134
135
136
137
138
139
140 @Override
141 protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException {
142 if (wide) {
143 n = bytes.readUnsignedShort();
144 super.setLength(4);
145 } else {
146 final short opcode = super.getOpcode();
147 if (opcode >= Const.ILOAD && opcode <= Const.ALOAD || opcode >= Const.ISTORE && opcode <= Const.ASTORE) {
148 n = bytes.readUnsignedByte();
149 super.setLength(2);
150 } else {
151 if (opcode <= Const.ALOAD_3) {
152 n = (opcode - Const.ILOAD_0) % 4;
153 } else {
154 n = (opcode - Const.ISTORE_0) % 4;
155 }
156 super.setLength(1);
157 }
158 }
159 }
160
161
162
163
164
165
166 @Override
167 public void setIndex(final int n) {
168 if (n < 0 || n > Const.MAX_SHORT) {
169 throw new ClassGenException("Illegal value: " + n);
170 }
171 this.n = n;
172
173 if (n <= 3) {
174 super.setOpcode((short) (cTag + n));
175 super.setLength(1);
176 } else {
177 super.setOpcode(canonTag);
178 if (wide()) {
179 super.setLength(4);
180 } else {
181 super.setLength(2);
182 }
183 }
184 }
185
186
187
188
189
190
191
192 final void setIndexOnly(final int n) {
193 this.n = n;
194 }
195
196
197
198
199
200
201
202
203
204
205 @Override
206 public String toString(final boolean verbose) {
207 final short opcode = super.getOpcode();
208 if (opcode >= Const.ILOAD_0 && opcode <= Const.ALOAD_3 || opcode >= Const.ISTORE_0 && opcode <= Const.ASTORE_3) {
209 return super.toString(verbose);
210 }
211 return super.toString(verbose) + " " + n;
212 }
213
214 private boolean wide() {
215 return n > Const.MAX_BYTE;
216 }
217 }