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.util.Collection;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.Map;
25 import java.util.Set;
26
27 import org.apache.bcel.classfile.Utility;
28
29
30
31
32
33
34
35
36
37
38
39
40
41 public class InstructionHandle {
42
43
44
45
46
47
48 public static final InstructionHandle[] EMPTY_ARRAY = {};
49
50
51
52
53 static final InstructionTargeter[] EMPTY_INSTRUCTION_TARGETER_ARRAY = {};
54
55
56
57
58 static InstructionHandle getInstructionHandle(final Instruction i) {
59 return new InstructionHandle(i);
60 }
61
62 private InstructionHandle next;
63 private InstructionHandle prev;
64
65 private Instruction instruction;
66
67
68
69
70 @Deprecated
71 protected int i_position = -1;
72 private Set<InstructionTargeter> targeters;
73
74 private Map<Object, Object> attributes;
75
76 protected InstructionHandle(final Instruction i) {
77 setInstruction(i);
78 }
79
80
81
82
83
84
85 public void accept(final Visitor v) {
86 instruction.accept(v);
87 }
88
89
90
91
92
93
94
95 public void addAttribute(final Object key, final Object attr) {
96 if (attributes == null) {
97 attributes = new HashMap<>(3);
98 }
99 attributes.put(key, attr);
100 }
101
102
103
104
105
106
107 @Deprecated
108 protected void addHandle() {
109
110 }
111
112
113
114
115 public void addTargeter(final InstructionTargeter t) {
116 if (targeters == null) {
117 targeters = new HashSet<>();
118 }
119
120 targeters.add(t);
121 }
122
123
124
125
126 void dispose() {
127 next = prev = null;
128 instruction.dispose();
129 instruction = null;
130 i_position = -1;
131 attributes = null;
132 removeAllTargeters();
133 }
134
135
136
137
138
139
140 public Object getAttribute(final Object key) {
141 return attributes != null ? attributes.get(key) : null;
142 }
143
144
145
146
147 public Collection<Object> getAttributes() {
148 if (attributes == null) {
149 attributes = new HashMap<>(3);
150 }
151 return attributes.values();
152 }
153
154 public final Instruction getInstruction() {
155 return instruction;
156 }
157
158 public final InstructionHandle getNext() {
159 return next;
160 }
161
162
163
164
165
166 public int getPosition() {
167 return i_position;
168 }
169
170 public final InstructionHandle getPrev() {
171 return prev;
172 }
173
174
175
176
177 public InstructionTargeter[] getTargeters() {
178 if (!hasTargeters()) {
179 return EMPTY_INSTRUCTION_TARGETER_ARRAY;
180 }
181 final InstructionTargeter[] t = new InstructionTargeter[targeters.size()];
182 targeters.toArray(t);
183 return t;
184 }
185
186 public boolean hasTargeters() {
187 return targeters != null && !targeters.isEmpty();
188 }
189
190
191
192
193 public void removeAllTargeters() {
194 if (targeters != null) {
195 targeters.clear();
196 }
197 }
198
199
200
201
202
203
204 public void removeAttribute(final Object key) {
205 if (attributes != null) {
206 attributes.remove(key);
207 }
208 }
209
210
211
212
213 public void removeTargeter(final InstructionTargeter t) {
214 if (targeters != null) {
215 targeters.remove(t);
216 }
217 }
218
219
220
221
222 public void setInstruction(final Instruction i) {
223 if (i == null) {
224 throw new ClassGenException("Assigning null to handle");
225 }
226 if (this.getClass() != BranchHandle.class && i instanceof BranchInstruction) {
227 throw new ClassGenException("Assigning branch instruction " + i + " to plain handle");
228 }
229 if (instruction != null) {
230 instruction.dispose();
231 }
232 instruction = i;
233 }
234
235
236
237
238
239 final InstructionHandle setNext(final InstructionHandle next) {
240 this.next = next;
241 return next;
242 }
243
244
245
246
247 void setPosition(final int pos) {
248 i_position = pos;
249 }
250
251
252
253
254
255 final InstructionHandle setPrev(final InstructionHandle prev) {
256 this.prev = prev;
257 return prev;
258 }
259
260
261
262
263
264
265
266
267
268
269 public Instruction swapInstruction(final Instruction i) {
270 final Instruction oldInstruction = instruction;
271 instruction = i;
272 return oldInstruction;
273 }
274
275
276
277
278 @Override
279 public String toString() {
280 return toString(true);
281 }
282
283
284
285
286 public String toString(final boolean verbose) {
287 return Utility.format(i_position, 4, false, ' ') + ": " + instruction.toString(verbose);
288 }
289
290
291
292
293
294
295
296
297
298
299 protected int updatePosition(final int offset, final int maxOffset) {
300 i_position += offset;
301 return 0;
302 }
303 }