1 package com.sun.org.apache.bcel.internal.generic; 2 3 56 57 import com.sun.org.apache.bcel.internal.Constants; 58 import com.sun.org.apache.bcel.internal.classfile.*; 59 60 71 public class LocalVariableGen implements InstructionTargeter, NamedAndTyped, Cloneable { 72 private int index; 73 private String name; 74 private Type type; 75 private InstructionHandle start, end; 76 77 87 public LocalVariableGen(int index, String name, Type type, 88 InstructionHandle start, InstructionHandle end) { 89 if((index < 0) || (index > Constants.MAX_SHORT)) 90 throw new ClassGenException("Invalid index index: " + index); 91 92 this.name = name; 93 this.type = type; 94 this.index = index; 95 setStart(start); 96 setEnd(end); 97 } 98 99 108 public LocalVariable getLocalVariable(ConstantPoolGen cp) { 109 int start_pc = start.getPosition(); 110 int length = end.getPosition() - start_pc; 111 int name_index = cp.addUtf8(name); 112 int signature_index = cp.addUtf8(type.getSignature()); 113 114 return new LocalVariable(start_pc, length, name_index, 115 signature_index, index, cp.getConstantPool()); 116 } 117 118 public void setIndex(int index) { this.index = index; } 119 public int getIndex() { return index; } 120 public void setName(String name) { this.name = name; } 121 public String getName() { return name; } 122 public void setType(Type type) { this.type = type; } 123 public Type getType() { return type; } 124 125 public InstructionHandle getStart() { return start; } 126 public InstructionHandle getEnd() { return end; } 127 128 public void setStart(InstructionHandle start) { 129 BranchInstruction.notifyTarget(this.start, start, this); 130 this.start = start; 131 } 132 133 public void setEnd(InstructionHandle end) { 134 BranchInstruction.notifyTarget(this.end, end, this); 135 this.end = end; 136 } 137 138 142 public void updateTarget(InstructionHandle old_ih, InstructionHandle new_ih) { 143 boolean targeted = false; 144 145 if(start == old_ih) { 146 targeted = true; 147 setStart(new_ih); 148 } 149 150 if(end == old_ih) { 151 targeted = true; 152 setEnd(new_ih); 153 } 154 155 if(!targeted) 156 throw new ClassGenException("Not targeting " + old_ih + ", but {" + start + ", " + 157 end + "}"); 158 } 159 160 163 public boolean containsTarget(InstructionHandle ih) { 164 return (start == ih) || (end == ih); 165 } 166 167 171 public boolean equals(Object o) { 172 if(!(o instanceof LocalVariableGen)) 173 return false; 174 175 LocalVariableGen l = (LocalVariableGen)o; 176 return (l.index == index) && (l.start == start) && (l.end == end); 177 } 178 179 public String toString() { 180 return "LocalVariableGen(" + name + ", " + type + ", " + start + ", " + end + ")"; 181 } 182 183 public Object clone() { 184 try { 185 return super.clone(); 186 } catch(CloneNotSupportedException e) { 187 System.err.println(e); 188 return null; 189 } 190 } 191 } 192 | Popular Tags |