1 17 package org.apache.tools.ant.taskdefs.optional.sitraka.bytecode; 18 19 import java.io.DataInputStream ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import org.apache.tools.ant.taskdefs.optional.depend.constantpool.ClassCPInfo; 23 import org.apache.tools.ant.taskdefs.optional.depend.constantpool.ConstantPool; 24 import org.apache.tools.ant.taskdefs.optional.depend.constantpool.Utf8CPInfo; 25 import org.apache.tools.ant.taskdefs.optional.sitraka.bytecode.attributes.AttributeInfo; 26 27 28 37 public final class ClassFile { 38 39 private MethodInfo[] methods; 40 41 private String sourceFile; 42 43 private String fullname; 44 45 private int access_flags; 46 47 public ClassFile(InputStream is) throws IOException { 48 DataInputStream dis = new DataInputStream (is); 49 ConstantPool constantPool = new ConstantPool(); 50 51 dis.readInt(); dis.readShort(); 53 dis.readShort(); 54 55 constantPool.read(dis); 56 constantPool.resolve(); 57 58 access_flags = dis.readShort(); 60 int this_class = dis.readShort(); 61 fullname = ((ClassCPInfo) constantPool.getEntry(this_class)).getClassName().replace('/', '.'); 62 dis.readShort(); 63 64 int count = dis.readShort(); 66 dis.skipBytes(count * 2); 68 int numFields = dis.readShort(); 70 for (int i = 0; i < numFields; i++) { 71 dis.skip(2 * 3); 73 int attributes_count = dis.readUnsignedShort(); 75 for (int j = 0; j < attributes_count; j++) { 76 dis.skipBytes(2); int len = dis.readInt(); 78 dis.skipBytes(len); 79 } 80 } 81 82 int method_count = dis.readShort(); 84 methods = new MethodInfo[method_count]; 85 for (int i = 0; i < method_count; i++) { 86 methods[i] = new MethodInfo(); 87 methods[i].read(constantPool, dis); 88 } 89 90 int attributes_count = dis.readUnsignedShort(); 92 for (int j = 0; j < attributes_count; j++) { 93 int attr_id = dis.readShort(); 94 int len = dis.readInt(); 95 String attr_name = Utils.getUTF8Value(constantPool, attr_id); 96 if (AttributeInfo.SOURCE_FILE.equals(attr_name)) { 97 int name_index = dis.readShort(); 98 sourceFile = ((Utf8CPInfo) constantPool.getEntry(name_index)).getValue(); 99 } else { 100 dis.skipBytes(len); 101 } 102 } 103 } 104 105 public int getAccess() { 106 return access_flags; 107 } 108 109 public String getSourceFile() { 110 return sourceFile; 111 } 112 113 public MethodInfo[] getMethods() { 114 return methods; 115 } 116 117 public String getFullName() { 118 return fullname; 119 } 120 121 public String getName() { 122 String name = getFullName(); 123 int pos = name.lastIndexOf('.'); 124 if (pos == -1) { 125 return ""; 126 } 127 return name.substring(pos + 1); 128 } 129 130 public String getPackage() { 131 String name = getFullName(); 132 int pos = name.lastIndexOf('.'); 133 if (pos == -1) { 134 return ""; 135 } 136 return name.substring(0, pos); 137 } 138 139 } 140 141 142 143 144 145 | Popular Tags |