1 15 16 package javassist.bytecode; 17 18 import java.io.DataInputStream ; 19 import java.io.IOException ; 20 import java.util.Map ; 21 22 26 public class LocalVariableAttribute extends AttributeInfo { 27 30 public static final String tag = "LocalVariableTable"; 31 32 35 public static final String typeTag = "LocalVariableTypeTable"; 36 37 40 public LocalVariableAttribute(ConstPool cp) { 41 super(cp, tag, new byte[2]); 42 ByteArray.write16bit(0, info, 0); 43 } 44 45 LocalVariableAttribute(ConstPool cp, int n, DataInputStream in) 46 throws IOException 47 { 48 super(cp, n, in); 49 } 50 51 private LocalVariableAttribute(ConstPool cp, byte[] i) { 52 super(cp, tag, i); 53 } 54 55 64 public void addEntry(int startPc, int length, int nameIndex, 65 int descriptorIndex, int index) { 66 int size = info.length; 67 byte[] newInfo = new byte[size + 10]; 68 ByteArray.write16bit(tableLength() + 1, newInfo, 0); 69 for (int i = 2; i < size; ++i) 70 newInfo[i] = info[i]; 71 72 ByteArray.write16bit(startPc, newInfo, size); 73 ByteArray.write16bit(length, newInfo, size + 2); 74 ByteArray.write16bit(nameIndex, newInfo, size + 4); 75 ByteArray.write16bit(descriptorIndex, newInfo, size + 6); 76 ByteArray.write16bit(index, newInfo, size + 8); 77 info = newInfo; 78 } 79 80 84 public int tableLength() { 85 return ByteArray.readU16bit(info, 0); 86 } 87 88 95 public int startPc(int i) { 96 return ByteArray.readU16bit(info, i * 10 + 2); 97 } 98 99 106 public int codeLength(int i) { 107 return ByteArray.readU16bit(info, i * 10 + 4); 108 } 109 110 113 void shiftPc(int where, int gapLength, boolean exclusive) { 114 int n = tableLength(); 115 for (int i = 0; i < n; ++i) { 116 int pos = i * 10 + 2; 117 int pc = ByteArray.readU16bit(info, pos); 118 int len = ByteArray.readU16bit(info, pos + 2); 119 120 122 if (pc > where || (exclusive && pc == where && pc != 0)) 123 ByteArray.write16bit(pc + gapLength, info, pos); 124 else if (pc + len > where) 125 ByteArray.write16bit(len + gapLength, info, pos + 2); 126 } 127 } 128 129 135 public int nameIndex(int i) { 136 return ByteArray.readU16bit(info, i * 10 + 6); 137 } 138 139 145 public String variableName(int i) { 146 return getConstPool().getUtf8Info(nameIndex(i)); 147 } 148 149 161 public int descriptorIndex(int i) { 162 return ByteArray.readU16bit(info, i * 10 + 8); 163 } 164 165 174 public int signatureIndex(int i) { 175 return descriptorIndex(i); 176 } 177 178 188 public String descriptor(int i) { 189 return getConstPool().getUtf8Info(descriptorIndex(i)); 190 } 191 192 201 public String signature(int i) { 202 return descriptor(i); 203 } 204 205 211 public int index(int i) { 212 return ByteArray.readU16bit(info, i * 10 + 10); 213 } 214 215 221 public AttributeInfo copy(ConstPool newCp, Map classnames) { 222 byte[] src = get(); 223 byte[] dest = new byte[src.length]; 224 ConstPool cp = getConstPool(); 225 LocalVariableAttribute attr = new LocalVariableAttribute(newCp, dest); 226 int n = ByteArray.readU16bit(src, 0); 227 ByteArray.write16bit(n, dest, 0); 228 int j = 2; 229 for (int i = 0; i < n; ++i) { 230 int start = ByteArray.readU16bit(src, j); 231 int len = ByteArray.readU16bit(src, j + 2); 232 int name = ByteArray.readU16bit(src, j + 4); 233 int type = ByteArray.readU16bit(src, j + 6); 234 int index = ByteArray.readU16bit(src, j + 8); 235 236 ByteArray.write16bit(start, dest, j); 237 ByteArray.write16bit(len, dest, j + 2); 238 if (name != 0) 239 name = cp.copy(name, newCp, null); 240 241 ByteArray.write16bit(name, dest, j + 4); 242 243 if (type != 0) 244 type = cp.copy(type, newCp, null); 245 246 ByteArray.write16bit(type, dest, j + 6); 247 ByteArray.write16bit(index, dest, j + 8); 248 j += 10; 249 } 250 251 return attr; 252 } 253 } 254 | Popular Tags |