00001 package de.fub.bytecode.generic;
00002
00003 import de.fub.bytecode.classfile.Utility;
00004 import java.util.Vector;
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 public class InstructionHandle implements java.io.Serializable {
00026 InstructionHandle next, prev;
00027 Instruction instruction;
00028 protected int i_position = -1;
00029 private Vector targeters;
00030
00031
00032
00033 private static InstructionHandle ih_list = null;
00034
00035 protected InstructionHandle(Instruction i) {
00036 setInstruction(i);
00037 }
00038
00039
00040 protected void addHandle() {
00041 next = ih_list;
00042 ih_list = this;
00043 }
00044
00045
00046
00047 public void addTargeter(InstructionTargeter t) {
00048 if(targeters == null)
00049 targeters = new Vector();
00050
00051 if(!targeters.contains(t))
00052 targeters.addElement(t);
00053 }
00054
00055
00056
00057 void dispose() {
00058 next = prev = null;
00059 instruction.dispose();
00060 instruction = null;
00061 i_position = -1;
00062 removeAllTargeters();
00063 addHandle();
00064 }
00065 public final Instruction getInstruction() { return instruction; }
00066 static final InstructionHandle getInstructionHandle(Instruction i) {
00067 if(ih_list == null)
00068 return new InstructionHandle(i);
00069 else {
00070 InstructionHandle ih = ih_list;
00071 ih_list = ih.next;
00072
00073 ih.setInstruction(i);
00074
00075 return ih;
00076 }
00077 }
00078 public final InstructionHandle getNext() { return next; }
00079
00080
00081 public int getPosition() { return i_position; }
00082 public final InstructionHandle getPrev() { return prev; }
00083
00084
00085
00086 public InstructionTargeter[] getTargeters() {
00087 if(!hasTargeters())
00088 return null;
00089
00090 InstructionTargeter[] t = new InstructionTargeter[targeters.size()];
00091 targeters.copyInto(t);
00092 return t;
00093 }
00094 public boolean hasTargeters() {
00095 return (targeters != null) && (targeters.size() > 0);
00096 }
00097
00098
00099 public void removeAllTargeters() {
00100 if(targeters != null)
00101 targeters.removeAllElements();
00102 }
00103
00104
00105
00106 public void removeTargeter(InstructionTargeter t) {
00107 targeters.removeElement(t);
00108 }
00109
00110
00111
00112
00113 public void setInstruction(Instruction i) {
00114 if(i == null)
00115 throw new ClassGenException("Assigning null to handle");
00116
00117 if((this.getClass() != BranchHandle.class) && (i instanceof BranchInstruction))
00118 throw new ClassGenException("Assigning branch instruction " + i + " to plain handle");
00119
00120 if(instruction != null)
00121 instruction.dispose();
00122
00123 instruction = i;
00124 }
00125
00126 void setPosition(int pos) { i_position = pos; }
00127 public String toString() {
00128 return toString(true);
00129 }
00130 public String toString(boolean verbose) {
00131 return Utility.format(i_position, 4, false, ' ') + ": " + instruction.toString(verbose);
00132 }
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143 protected int updatePosition(int offset, int max_offset) {
00144 i_position += offset;
00145 return 0;
00146 }
00147 }