00001 package de.fub.bytecode.generic;
00002
00003 import de.fub.bytecode.Constants;
00004 import de.fub.bytecode.classfile.*;
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 public class LocalVariableGen implements InstructionTargeter, NamedAndTyped, Cloneable {
00018 private int index;
00019 private String name;
00020 private Type type;
00021 private InstructionHandle start, end;
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 public LocalVariableGen(int index, String name, Type type,
00034 InstructionHandle start, InstructionHandle end) {
00035 if((index < 0) || (index > Constants.MAX_SHORT))
00036 throw new ClassGenException("Invalid index index: " + index);
00037
00038 this.name = name;
00039 this.type = type;
00040 this.index = index;
00041 setStart(start);
00042 setEnd(end);
00043 }
00044 public Object clone() {
00045 try {
00046 return super.clone();
00047 } catch(CloneNotSupportedException e) {
00048 System.err.println(e);
00049 return null;
00050 }
00051 }
00052
00053
00054
00055 public boolean containsTarget(InstructionHandle ih) {
00056 return (start == ih) || (end == ih);
00057 }
00058
00059
00060
00061
00062 public boolean equals(Object o) {
00063 if(!(o instanceof LocalVariableGen))
00064 return false;
00065
00066 LocalVariableGen l = (LocalVariableGen)o;
00067 return (l.index == index) && (l.start == start) && (l.end == end);
00068 }
00069 public InstructionHandle getEnd() { return end; }
00070 public int getIndex() { return index; }
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 public LocalVariable getLocalVariable(ConstantPoolGen cp) {
00081 int start_pc = start.getPosition();
00082 int length = end.getPosition() - start_pc;
00083 int name_index = cp.addUtf8(name);
00084 int signature_index = cp.addUtf8(type.getSignature());
00085
00086 return new LocalVariable(start_pc, length, name_index,
00087 signature_index, index, cp.getConstantPool());
00088 }
00089 public String getName() { return name; }
00090
00091
00092 public int getSlot() { return index; }
00093 public InstructionHandle getStart() { return start; }
00094 public Type getType() { return type; }
00095 public void setEnd(InstructionHandle end) {
00096 BranchInstruction.notifyTarget(this.end, end, this);
00097 this.end = end;
00098 }
00099 public void setIndex(int index) { this.index = index; }
00100 public void setName(String name) { this.name = name; }
00101
00102
00103 public void setSlot(int index) { this.index = index; }
00104 public void setStart(InstructionHandle start) {
00105 BranchInstruction.notifyTarget(this.start, start, this);
00106 this.start = start;
00107 }
00108 public void setType(Type type) { this.type = type; }
00109 public String toString() {
00110 return "LocalVariableGen(" + name + ", " + type + ", " + start + ", " + end + ")";
00111 }
00112
00113
00114
00115
00116 public void updateTarget(InstructionHandle old_ih, InstructionHandle new_ih) {
00117 boolean targeted = false;
00118
00119 if(start == old_ih) {
00120 targeted = true;
00121 setStart(new_ih);
00122 }
00123
00124 if(end == old_ih) {
00125 targeted = true;
00126 setEnd(new_ih);
00127 }
00128
00129 if(!targeted)
00130 throw new ClassGenException("Not targeting " + old_ih + ", but {" + start + ", " +
00131 end + "}");
00132 }
00133 }