1 17 package org.apache.tools.ant.taskdefs.optional.sitraka.bytecode; 18 19 import java.io.DataInputStream ; 20 import java.io.IOException ; 21 import org.apache.tools.ant.taskdefs.optional.depend.constantpool.ConstantPool; 22 import org.apache.tools.ant.taskdefs.optional.sitraka.bytecode.attributes.AttributeInfo; 23 24 29 public final class MethodInfo { 30 private int access_flags; 31 private int loc = -1; 32 private String name; 33 private String descriptor; 34 35 public MethodInfo() { 36 } 37 38 public void read(ConstantPool constantPool, DataInputStream dis) throws IOException { 39 access_flags = dis.readShort(); 40 41 int name_index = dis.readShort(); 42 name = Utils.getUTF8Value(constantPool, name_index); 43 44 int descriptor_index = dis.readShort(); 45 descriptor = Utils.getUTF8Value(constantPool, descriptor_index); 46 47 int attributes_count = dis.readUnsignedShort(); 48 for (int i = 0; i < attributes_count; i++) { 49 int attr_id = dis.readShort(); 50 String attr_name = Utils.getUTF8Value(constantPool, attr_id); 51 int len = dis.readInt(); 52 if (AttributeInfo.CODE.equals(attr_name)) { 53 readCode(constantPool, dis); 54 } else { 55 dis.skipBytes(len); 56 } 57 } 58 59 } 60 61 protected void readCode(ConstantPool constantPool, DataInputStream dis) throws IOException { 62 dis.skipBytes(2 * 2); 64 65 int bytecode_len = dis.readInt(); 67 dis.skip(bytecode_len); 68 69 int exception_count = dis.readShort(); 71 dis.skipBytes(exception_count * 4 * 2); 72 73 int attributes_count = dis.readUnsignedShort(); 75 for (int i = 0; i < attributes_count; i++) { 76 int attr_id = dis.readShort(); 77 String attr_name = Utils.getUTF8Value(constantPool, attr_id); 78 int len = dis.readInt(); 79 if (AttributeInfo.LINE_NUMBER_TABLE.equals(attr_name)) { 80 loc = dis.readShort(); 82 dis.skip(loc * 2 * 2); 84 } else { 85 dis.skipBytes(len); 86 } 87 } 88 } 89 90 public int getAccessFlags() { 91 return access_flags; 92 } 93 94 public String getName() { 95 return name; 96 } 97 98 public String getDescriptor() { 99 return descriptor; 100 } 101 102 public String getFullSignature() { 103 return getReturnType() + " " + getShortSignature(); 104 } 105 106 public String getShortSignature() { 107 StringBuffer buf = new StringBuffer (getName()); 108 buf.append("("); 109 String [] params = getParametersType(); 110 for (int i = 0; i < params.length; i++) { 111 buf.append(params[i]); 112 if (i != params.length - 1) { 113 buf.append(", "); 114 } 115 } 116 buf.append(")"); 117 return buf.toString(); 118 } 119 120 public String getReturnType() { 121 return Utils.getMethodReturnType(getDescriptor()); 122 } 123 124 public String [] getParametersType() { 125 return Utils.getMethodParams(getDescriptor()); 126 } 127 128 public int getNumberOfLines() { 129 return loc; 130 } 131 132 public String getAccess() { 133 return Utils.getMethodAccess(access_flags); 134 } 135 136 public String toString() { 137 StringBuffer sb = new StringBuffer (); 138 sb.append("Method: ").append(getAccess()).append(" "); 139 sb.append(getFullSignature()); 140 return sb.toString(); 141 } 142 } 143 144 145 | Popular Tags |