1 17 package org.apache.bcel.generic; 18 19 import org.apache.bcel.Constants; 20 import org.apache.bcel.classfile.LocalVariable; 21 22 33 public class LocalVariableGen implements InstructionTargeter, NamedAndTyped, Cloneable , 34 java.io.Serializable { 35 36 private int index; 37 private String name; 38 private Type type; 39 private InstructionHandle start, end; 40 41 42 52 public LocalVariableGen(int index, String name, Type type, InstructionHandle start, 53 InstructionHandle end) { 54 if ((index < 0) || (index > Constants.MAX_SHORT)) { 55 throw new ClassGenException("Invalid index index: " + index); 56 } 57 this.name = name; 58 this.type = type; 59 this.index = index; 60 setStart(start); 61 setEnd(end); 62 } 63 64 65 79 public LocalVariable getLocalVariable( ConstantPoolGen cp ) { 80 int start_pc = start.getPosition(); 81 int length = end.getPosition() - start_pc; 82 if (length > 0) { 83 length += end.getInstruction().getLength(); 84 } 85 int name_index = cp.addUtf8(name); 86 int signature_index = cp.addUtf8(type.getSignature()); 87 return new LocalVariable(start_pc, length, name_index, signature_index, index, cp 88 .getConstantPool()); 89 } 90 91 92 public void setIndex( int index ) { 93 this.index = index; 94 } 95 96 97 public int getIndex() { 98 return index; 99 } 100 101 102 public void setName( String name ) { 103 this.name = name; 104 } 105 106 107 public String getName() { 108 return name; 109 } 110 111 112 public void setType( Type type ) { 113 this.type = type; 114 } 115 116 117 public Type getType() { 118 return type; 119 } 120 121 122 public InstructionHandle getStart() { 123 return start; 124 } 125 126 127 public InstructionHandle getEnd() { 128 return end; 129 } 130 131 132 public void setStart( InstructionHandle start ) { 133 BranchInstruction.notifyTarget(this.start, start, this); 134 this.start = start; 135 } 136 137 138 public void setEnd( InstructionHandle end ) { 139 BranchInstruction.notifyTarget(this.end, end, this); 140 this.end = end; 141 } 142 143 144 148 public void updateTarget( InstructionHandle old_ih, InstructionHandle new_ih ) { 149 boolean targeted = false; 150 if (start == old_ih) { 151 targeted = true; 152 setStart(new_ih); 153 } 154 if (end == old_ih) { 155 targeted = true; 156 setEnd(new_ih); 157 } 158 if (!targeted) { 159 throw new ClassGenException("Not targeting " + old_ih + ", but {" + start + ", " + end 160 + "}"); 161 } 162 } 163 164 165 168 public boolean containsTarget( InstructionHandle ih ) { 169 return (start == ih) || (end == ih); 170 } 171 172 173 175 public int hashCode() { 176 int hc = index ^ name.hashCode() ^ type.hashCode(); 178 return hc; 179 } 180 181 182 186 public boolean equals( Object o ) { 187 if (!(o instanceof LocalVariableGen)) { 188 return false; 189 } 190 LocalVariableGen l = (LocalVariableGen) o; 191 return (l.index == index) && (l.start == start) && (l.end == end); 192 } 193 194 195 public String toString() { 196 return "LocalVariableGen(" + name + ", " + type + ", " + start + ", " + end + ")"; 197 } 198 199 200 public Object clone() { 201 try { 202 return super.clone(); 203 } catch (CloneNotSupportedException e) { 204 System.err.println(e); 205 return null; 206 } 207 } 208 } 209 | Popular Tags |