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 Constants, InstructionTargeter {
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 > 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 setStart(end);
00043 }
00044
00045
00046
00047 public boolean containsTarget(InstructionHandle ih) {
00048 return (start == ih) || (end == ih);
00049 }
00050
00051
00052
00053
00054 public boolean equals(Object o) {
00055 if(!(o instanceof LocalVariableGen))
00056 return false;
00057
00058 LocalVariableGen l = (LocalVariableGen)o;
00059 return (l.index == index) && (l.start == start) && (l.end == end);
00060 }
00061 public InstructionHandle getEnd() { return end; }
00062 public int getIndex() { return index; }
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 public LocalVariable getLocalVariable(ConstantPoolGen cp) {
00073 int start_pc = start.getPosition();
00074 int length = end.getPosition() - start_pc;
00075 int name_index = cp.addUtf8(name);
00076 int signature_index = cp.addUtf8(type.getSignature());
00077
00078 return new LocalVariable(start_pc, length, name_index,
00079 signature_index, index, cp.getConstantPool());
00080 }
00081 public String getName() { return name; }
00082
00083
00084 public int getSlot() { return index; }
00085 public InstructionHandle getStart() { return start; }
00086 public Type getType() { return type; }
00087 public void setEnd(InstructionHandle end) {
00088 BranchInstruction.notifyTarget(this.end, end, this);
00089 this.end = end;
00090 }
00091 public void setIndex(int index) { this.index = index; }
00092 public void setName(String name) { this.name = name; }
00093
00094
00095 public void setSlot(int index) { this.index = index; }
00096 public void setStart(InstructionHandle start) {
00097 BranchInstruction.notifyTarget(this.start, start, this);
00098 this.start = start;
00099 }
00100 public void setType(Type type) { this.type = type; }
00101 public String toString() {
00102 return "LocalvariableGen(" + name + ", " + type + ", " + start + ", " + end + ")";
00103 }
00104
00105
00106
00107
00108 public void updateTarget(InstructionHandle old_ih, InstructionHandle new_ih) {
00109 boolean targeted = false;
00110
00111 if(start == old_ih) {
00112 targeted = true;
00113 setStart(new_ih);
00114 }
00115
00116 if(end == old_ih) {
00117 targeted = true;
00118 setEnd(new_ih);
00119 }
00120
00121 if(!targeted)
00122 throw new ClassGenException("Not targeting " + old_ih + ", but {" + start + ", " +
00123 end + "}");
00124 }
00125 }