1 15 16 package javassist.bytecode; 17 18 import java.io.DataInputStream ; 19 import java.io.DataOutputStream ; 20 import java.io.IOException ; 21 import java.util.LinkedList ; 22 import java.util.List ; 23 import java.util.Map ; 24 25 31 public final class MethodInfo { 32 ConstPool constPool; 33 int accessFlags; 34 int name; 35 int descriptor; 36 LinkedList attribute; 38 41 44 public static final String nameInit = "<init>"; 45 46 50 public static final String nameClinit = "<clinit>"; 51 52 private MethodInfo(ConstPool cp) { 53 constPool = cp; 54 attribute = null; 55 } 56 57 69 public MethodInfo(ConstPool cp, String methodname, String desc) { 70 this(cp); 71 accessFlags = 0; 72 name = cp.addUtf8Info(methodname); 73 descriptor = constPool.addUtf8Info(desc); 74 } 75 76 MethodInfo(ConstPool cp, DataInputStream in) throws IOException { 77 this(cp); 78 read(in); 79 } 80 81 100 public MethodInfo(ConstPool cp, String methodname, MethodInfo src, 101 Map classnameMap) throws BadBytecode { 102 this(cp); 103 read(src, methodname, classnameMap); 104 } 105 106 109 public String toString() { 110 return constPool.getUtf8Info(name) + " " 111 + constPool.getUtf8Info(descriptor); 112 } 113 114 122 void compact(ConstPool cp) { 123 name = cp.addUtf8Info(getName()); 124 descriptor = cp.addUtf8Info(getDescriptor()); 125 attribute = AttributeInfo.copyAll(attribute, cp); 126 constPool = cp; 127 } 128 129 void prune(ConstPool cp) { 130 attribute = null; 131 name = cp.addUtf8Info(getName()); 132 descriptor = cp.addUtf8Info(getDescriptor()); 133 constPool = cp; 134 } 135 136 139 public String getName() { 140 return constPool.getUtf8Info(name); 141 } 142 143 146 public void setName(String newName) { 147 name = constPool.addUtf8Info(newName); 148 } 149 150 154 public boolean isMethod() { 155 String n = getName(); 156 return !n.equals(nameInit) && !n.equals(nameClinit); 157 } 158 159 162 public ConstPool getConstPool() { 163 return constPool; 164 } 165 166 169 public boolean isConstructor() { 170 return getName().equals(nameInit); 171 } 172 173 176 public boolean isStaticInitializer() { 177 return getName().equals(nameClinit); 178 } 179 180 185 public int getAccessFlags() { 186 return accessFlags; 187 } 188 189 194 public void setAccessFlags(int acc) { 195 accessFlags = acc; 196 } 197 198 203 public String getDescriptor() { 204 return constPool.getUtf8Info(descriptor); 205 } 206 207 212 public void setDescriptor(String desc) { 213 if (!desc.equals(getDescriptor())) 214 descriptor = constPool.addUtf8Info(desc); 215 } 216 217 224 public List getAttributes() { 225 if (attribute == null) 226 attribute = new LinkedList (); 227 228 return attribute; 229 } 230 231 239 public AttributeInfo getAttribute(String name) { 240 return AttributeInfo.lookup(attribute, name); 241 } 242 243 247 public void addAttribute(AttributeInfo info) { 248 if (attribute == null) 249 attribute = new LinkedList (); 250 251 AttributeInfo.remove(attribute, info.getName()); 252 attribute.add(info); 253 } 254 255 260 public ExceptionsAttribute getExceptionsAttribute() { 261 AttributeInfo info = AttributeInfo.lookup(attribute, 262 ExceptionsAttribute.tag); 263 return (ExceptionsAttribute)info; 264 } 265 266 271 public CodeAttribute getCodeAttribute() { 272 AttributeInfo info = AttributeInfo.lookup(attribute, CodeAttribute.tag); 273 return (CodeAttribute)info; 274 } 275 276 279 public void removeExceptionsAttribute() { 280 AttributeInfo.remove(attribute, ExceptionsAttribute.tag); 281 } 282 283 290 public void setExceptionsAttribute(ExceptionsAttribute cattr) { 291 removeExceptionsAttribute(); 292 if (attribute == null) 293 attribute = new LinkedList (); 294 295 attribute.add(cattr); 296 } 297 298 301 public void removeCodeAttribute() { 302 AttributeInfo.remove(attribute, CodeAttribute.tag); 303 } 304 305 312 public void setCodeAttribute(CodeAttribute cattr) { 313 removeCodeAttribute(); 314 if (attribute == null) 315 attribute = new LinkedList (); 316 317 attribute.add(cattr); 318 } 319 320 329 public int getLineNumber(int pos) { 330 CodeAttribute ca = getCodeAttribute(); 331 if (ca == null) 332 return -1; 333 334 LineNumberAttribute ainfo = (LineNumberAttribute)ca 335 .getAttribute(LineNumberAttribute.tag); 336 if (ainfo == null) 337 return -1; 338 339 return ainfo.toLineNumber(pos); 340 } 341 342 363 public void setSuperclass(String superclass) throws BadBytecode { 364 if (!isConstructor()) 365 return; 366 367 CodeAttribute ca = getCodeAttribute(); 368 byte[] code = ca.getCode(); 369 CodeIterator iterator = ca.iterator(); 370 int pos = iterator.skipSuperConstructor(); 371 if (pos >= 0) { ConstPool cp = constPool; 373 int mref = ByteArray.readU16bit(code, pos + 1); 374 int nt = cp.getMethodrefNameAndType(mref); 375 int sc = cp.addClassInfo(superclass); 376 int mref2 = cp.addMethodrefInfo(sc, nt); 377 ByteArray.write16bit(mref2, code, pos + 1); 378 } 379 } 380 381 private void read(MethodInfo src, String methodname, Map classnames) 382 throws BadBytecode { 383 ConstPool destCp = constPool; 384 accessFlags = src.accessFlags; 385 name = destCp.addUtf8Info(methodname); 386 387 ConstPool srcCp = src.constPool; 388 String desc = srcCp.getUtf8Info(src.descriptor); 389 String desc2 = Descriptor.rename(desc, classnames); 390 descriptor = destCp.addUtf8Info(desc2); 391 392 attribute = new LinkedList (); 393 ExceptionsAttribute eattr = src.getExceptionsAttribute(); 394 if (eattr != null) 395 attribute.add(eattr.copy(destCp, classnames)); 396 397 CodeAttribute cattr = src.getCodeAttribute(); 398 if (cattr != null) 399 attribute.add(cattr.copy(destCp, classnames)); 400 } 401 402 private void read(DataInputStream in) throws IOException { 403 accessFlags = in.readUnsignedShort(); 404 name = in.readUnsignedShort(); 405 descriptor = in.readUnsignedShort(); 406 int n = in.readUnsignedShort(); 407 attribute = new LinkedList (); 408 for (int i = 0; i < n; ++i) 409 attribute.add(AttributeInfo.read(constPool, in)); 410 } 411 412 void write(DataOutputStream out) throws IOException { 413 out.writeShort(accessFlags); 414 out.writeShort(name); 415 out.writeShort(descriptor); 416 417 if (attribute == null) 418 out.writeShort(0); 419 else { 420 out.writeShort(attribute.size()); 421 AttributeInfo.writeAll(attribute, out); 422 } 423 } 424 } 425 | Popular Tags |