1 package com.sun.org.apache.bcel.internal.generic; 2 3 56 57 import com.sun.org.apache.bcel.internal.classfile.Utility; 58 import java.util.HashSet ; 59 import java.util.Collection ; 60 import java.util.HashMap ; 61 62 80 public class InstructionHandle implements java.io.Serializable { 81 InstructionHandle next, prev; Instruction instruction; 83 protected int i_position = -1; private HashSet targeters; 85 private HashMap attributes; 86 87 public final InstructionHandle getNext() { return next; } 88 public final InstructionHandle getPrev() { return prev; } 89 public final Instruction getInstruction() { return instruction; } 90 91 95 public void setInstruction(Instruction i) { if(i == null) 97 throw new ClassGenException("Assigning null to handle"); 98 99 if((this.getClass() != BranchHandle.class) && (i instanceof BranchInstruction)) 100 throw new ClassGenException("Assigning branch instruction " + i + " to plain handle"); 101 102 if(instruction != null) 103 instruction.dispose(); 104 105 instruction = i; 106 } 107 108 113 public Instruction swapInstruction(Instruction i) { 114 Instruction oldInstruction = instruction; 115 instruction = i; 116 return oldInstruction; 117 } 118 119 protected InstructionHandle(Instruction i) { 120 setInstruction(i); 121 } 122 123 private static InstructionHandle ih_list = null; 125 127 static final InstructionHandle getInstructionHandle(Instruction i) { 128 if(ih_list == null) 129 return new InstructionHandle(i); 130 else { 131 InstructionHandle ih = ih_list; 132 ih_list = ih.next; 133 134 ih.setInstruction(i); 135 136 return ih; 137 } 138 } 139 140 150 protected int updatePosition(int offset, int max_offset) { 151 i_position += offset; 152 return 0; 153 } 154 155 159 public int getPosition() { return i_position; } 160 161 164 void setPosition(int pos) { i_position = pos; } 165 166 168 protected void addHandle() { 169 next = ih_list; 170 ih_list = this; 171 } 172 173 176 void dispose() { 177 next = prev = null; 178 instruction.dispose(); 179 instruction = null; 180 i_position = -1; 181 attributes = null; 182 removeAllTargeters(); 183 addHandle(); 184 } 185 186 188 public void removeAllTargeters() { 189 if(targeters != null) 190 targeters.clear(); 191 } 192 193 196 public void removeTargeter(InstructionTargeter t) { 197 targeters.remove(t); 198 } 199 200 203 public void addTargeter(InstructionTargeter t) { 204 if(targeters == null) 205 targeters = new HashSet (); 206 207 targeters.add(t); 209 } 210 211 public boolean hasTargeters() { 212 return (targeters != null) && (targeters.size() > 0); 213 } 214 215 218 public InstructionTargeter[] getTargeters() { 219 if(!hasTargeters()) 220 return null; 221 222 InstructionTargeter[] t = new InstructionTargeter[targeters.size()]; 223 targeters.toArray(t); 224 return t; 225 } 226 227 229 public String toString(boolean verbose) { 230 return Utility.format(i_position, 4, false, ' ') + ": " + instruction.toString(verbose); 231 } 232 233 235 public String toString() { 236 return toString(true); 237 } 238 239 244 public void addAttribute(Object key, Object attr) { 245 if(attributes == null) 246 attributes = new HashMap (3); 247 248 attributes.put(key, attr); 249 } 250 251 255 public void removeAttribute(Object key) { 256 if(attributes != null) 257 attributes.remove(key); 258 } 259 260 264 public Object getAttribute(Object key) { 265 if(attributes != null) 266 return attributes.get(key); 267 268 return null; 269 } 270 271 273 public Collection getAttributes() { 274 return attributes.values(); 275 } 276 277 281 public void accept(Visitor v) { 282 instruction.accept(v); 283 } 284 } 285 | Popular Tags |