1 24 25 package org.aspectj.compiler.base.bytecode; 26 27 import java.io.*; 28 import java.lang.reflect.Modifier ; 29 30 public class ClassFile { 31 ConstantPool pool; 32 33 int modifiers; 34 ConstantPool.Class thisClass; 35 ConstantPool.Class superClass; 36 ConstantPool.Class[] interfaces; 37 38 FieldInfo[] fields; 39 MethodInfo[] methods; 40 41 Attributes attributes; 42 43 46 public String getClassName() { 47 return thisClass.getName(); 48 } 49 50 private void assertTrue(boolean test, String message) { 51 if (!test) throw new RuntimeException (message); 52 } 53 54 void readConstantPool(DataInputStream stream) throws IOException { 55 pool = new ConstantPool(); 56 pool.readFrom(stream); 57 } 58 59 void readInterfaces(DataInputStream stream) throws IOException { 60 int interfacesCount = stream.readUnsignedShort(); 61 interfaces = new ConstantPool.Class[interfacesCount]; 62 for(int i=0; i<interfacesCount; i++) { 63 interfaces[i] = pool.getClass(stream.readUnsignedShort()); 64 } 65 } 66 67 class MemberInfo { 68 int modifiers; 69 ConstantPool.UTF name; 70 ConstantPool.UTF descriptor; 71 Attributes attributes; 72 73 void readFrom(DataInputStream stream) throws IOException { 74 modifiers = stream.readUnsignedShort(); 75 name = pool.getUTF(stream.readUnsignedShort()); 76 descriptor = pool.getUTF(stream.readUnsignedShort()); 77 attributes = Attributes.createFrom(ClassFile.this, stream); 78 } 79 80 public ConstantPool.Class[] getExceptions() { 81 Attributes.ExceptionsAttribute excs = 82 (Attributes.ExceptionsAttribute) 83 attributes.findAttribute(Attributes.ExceptionsAttribute.class); 84 if (excs == null) return null; 85 else return excs.exceptions; 86 } 87 88 public boolean isDeprecated() { 89 return attributes.findAttribute(Attributes.DeprecatedAttribute.class) != null; 90 } 91 92 93 public String toString() { 94 return Modifier.toString(modifiers)+" "+name.value+" "+descriptor.value+": "+attributes; 95 } 96 } 97 98 class FieldInfo extends MemberInfo {} 99 100 void readFields(DataInputStream stream) throws IOException { 101 int fieldsCount = stream.readUnsignedShort(); 102 fields = new FieldInfo[fieldsCount]; 103 for(int i=0; i<fieldsCount; i++) { 104 fields[i] = new FieldInfo(); 105 fields[i].readFrom(stream); 106 } 107 } 108 109 class MethodInfo extends MemberInfo { 110 } 114 115 void readMethods(DataInputStream stream) throws IOException { 116 int methodsCount = stream.readUnsignedShort(); 117 methods = new MethodInfo[methodsCount]; 118 for(int i=0; i<methodsCount; i++) { 119 methods[i] = new MethodInfo(); 120 methods[i].readFrom(stream); 121 } 122 } 123 124 void readAttributes(DataInputStream stream) throws IOException { 125 attributes = Attributes.createFrom(this, stream); 126 } 127 128 public void readFrom(DataInputStream stream) throws IOException { 129 int magic = stream.readInt(); 130 int minorVersion = stream.readUnsignedShort(); 131 int majorVersion = stream.readUnsignedShort(); 132 assertTrue(magic == 0xCAFEBABE, "bad magic number: "+Integer.toString(magic, 16)); 133 assertTrue(majorVersion >= 45, "bad major version: "+majorVersion); 136 138 readConstantPool(stream); 139 140 modifiers = stream.readUnsignedShort(); 141 142 thisClass = pool.getClass(stream.readUnsignedShort()); 143 superClass = pool.getClass(stream.readUnsignedShort()); 144 145 readInterfaces(stream); 146 readFields(stream); 147 readMethods(stream); 148 readAttributes(stream); 149 } 150 151 void print() { 152 System.err.println("modifiers: "+Modifier.toString(modifiers)); 153 System.err.println("this: "+thisClass); 154 System.err.println("extends: "+superClass); 155 System.err.println("interfaces"); 156 printObjects(interfaces); 157 System.err.println("fields"); 158 printObjects(fields); 159 System.err.println("methods"); 160 printObjects(methods); 161 System.err.println("attributes: "+attributes); 162 } 163 164 static void printObjects(Object [] objects) { 165 for(int i=0; i<objects.length; i++) { 166 System.err.println(""+i+": "+objects[i]); 167 } 168 } 169 170 public static void main(String [] args) throws Exception { 171 ClassFile cf = new ClassFile(); 172 cf.readFrom(new DataInputStream(new FileInputStream(args[0]))); 173 cf.print(); 174 for (int i = 0; i < cf.pool.constants.length; i++) { 175 System.err.println(cf.pool.constants[i]); 176 } 177 } 178 } 179 | Popular Tags |