1 17 package org.apache.bcel.generic; 18 19 import java.util.Collection ; 20 import java.util.HashMap ; 21 import java.util.HashSet ; 22 import java.util.Map ; 23 import java.util.Set ; 24 import org.apache.bcel.classfile.Utility; 25 26 44 public class InstructionHandle implements java.io.Serializable { 45 46 InstructionHandle next, prev; Instruction instruction; 48 protected int i_position = -1; private Set targeters; 50 private Map attributes; 51 52 53 public final InstructionHandle getNext() { 54 return next; 55 } 56 57 58 public final InstructionHandle getPrev() { 59 return prev; 60 } 61 62 63 public final Instruction getInstruction() { 64 return instruction; 65 } 66 67 68 72 public void setInstruction( Instruction i ) { if (i == null) { 74 throw new ClassGenException("Assigning null to handle"); 75 } 76 if ((this.getClass() != BranchHandle.class) && (i instanceof BranchInstruction)) { 77 throw new ClassGenException("Assigning branch instruction " + i + " to plain handle"); 78 } 79 if (instruction != null) { 80 instruction.dispose(); 81 } 82 instruction = i; 83 } 84 85 86 91 public Instruction swapInstruction( Instruction i ) { 92 Instruction oldInstruction = instruction; 93 instruction = i; 94 return oldInstruction; 95 } 96 97 98 protected InstructionHandle(Instruction i) { 99 setInstruction(i); 100 } 101 102 private static InstructionHandle ih_list = null; 104 105 107 static final InstructionHandle getInstructionHandle( Instruction i ) { 108 if (ih_list == null) { 109 return new InstructionHandle(i); 110 } else { 111 InstructionHandle ih = ih_list; 112 ih_list = ih.next; 113 ih.setInstruction(i); 114 return ih; 115 } 116 } 117 118 119 129 protected int updatePosition( int offset, int max_offset ) { 130 i_position += offset; 131 return 0; 132 } 133 134 135 139 public int getPosition() { 140 return i_position; 141 } 142 143 144 147 void setPosition( int pos ) { 148 i_position = pos; 149 } 150 151 152 154 protected void addHandle() { 155 next = ih_list; 156 ih_list = this; 157 } 158 159 160 163 void dispose() { 164 next = prev = null; 165 instruction.dispose(); 166 instruction = null; 167 i_position = -1; 168 attributes = null; 169 removeAllTargeters(); 170 addHandle(); 171 } 172 173 174 176 public void removeAllTargeters() { 177 if (targeters != null) { 178 targeters.clear(); 179 } 180 } 181 182 183 186 public void removeTargeter( InstructionTargeter t ) { 187 if (targeters != null) { 188 targeters.remove(t); 189 } 190 } 191 192 193 196 public void addTargeter( InstructionTargeter t ) { 197 if (targeters == null) { 198 targeters = new HashSet (); 199 } 200 targeters.add(t); 202 } 203 204 205 public boolean hasTargeters() { 206 return (targeters != null) && (targeters.size() > 0); 207 } 208 209 210 213 public InstructionTargeter[] getTargeters() { 214 if (!hasTargeters()) { 215 return null; 216 } 217 InstructionTargeter[] t = new InstructionTargeter[targeters.size()]; 218 targeters.toArray(t); 219 return t; 220 } 221 222 223 225 public String toString( boolean verbose ) { 226 return Utility.format(i_position, 4, false, ' ') + ": " + instruction.toString(verbose); 227 } 228 229 230 232 public String toString() { 233 return toString(true); 234 } 235 236 237 242 public void addAttribute( Object key, Object attr ) { 243 if (attributes == null) { 244 attributes = new HashMap (3); 245 } 246 attributes.put(key, attr); 247 } 248 249 250 254 public void removeAttribute( Object key ) { 255 if (attributes != null) { 256 attributes.remove(key); 257 } 258 } 259 260 261 265 public Object getAttribute( Object key ) { 266 if (attributes != null) { 267 return attributes.get(key); 268 } 269 return null; 270 } 271 272 273 275 public Collection getAttributes() { 276 if (attributes == null) { 277 attributes = new HashMap (3); 278 } 279 return attributes.values(); 280 } 281 282 283 287 public void accept( Visitor v ) { 288 instruction.accept(v); 289 } 290 } 291 | Popular Tags |