1 52 53 package com.go.trove.classfile; 54 55 import java.io.*; 56 57 64 public class TestClassFileRead { 65 68 public static void main(String [] args) throws Exception { 69 InputStream in = new FileInputStream(args[0]); 70 in = new BufferedInputStream(in); 71 72 ClassFileDataLoader loader = new ResourceClassFileDataLoader(); 73 74 ClassFile cf = ClassFile.readFrom(in, loader, null); 75 in.close(); 76 77 while (cf.getOuterClass() != null) { 78 cf = cf.getOuterClass(); 79 } 80 81 dump(cf); 82 } 83 84 private static void dump(ClassFile cf) { 85 dump(cf, ""); 86 } 87 88 private static void dump(ClassFile cf, String indent) { 89 println(cf, indent); 90 println("className: " + cf.getClassName(), indent); 91 println("superClassName: " + cf.getSuperClassName(), indent); 92 println("innerClass: " + cf.isInnerClass(), indent); 93 println("innerClassName: " + cf.getInnerClassName(), indent); 94 println("type: " + cf.getType(), indent); 95 println("accessFlags: " + cf.getAccessFlags(), indent); 96 97 String [] interfaces = cf.getInterfaces(); 98 print("interfaces: ", indent); 99 for (int i=0; i<interfaces.length; i++) { 100 if (i > 0) { 101 System.out.print(", "); 102 } 103 System.out.print(interfaces[i]); 104 } 105 println(); 106 107 FieldInfo[] fields = cf.getFields(); 108 println("fields: ", indent); 109 for (int i=0; i<fields.length; i++) { 110 dump(fields[i], indent + " "); 111 } 112 113 MethodInfo[] methods = cf.getMethods(); 114 println("methods: ", indent); 115 for (int i=0; i<methods.length; i++) { 116 dump(methods[i], indent + " "); 117 } 118 119 methods = cf.getConstructors(); 120 println("constructors: ", indent); 121 for (int i=0; i<methods.length; i++) { 122 dump(methods[i], indent + " "); 123 } 124 125 MethodInfo init = cf.getInitializer(); 126 println("initializer: ", indent); 127 if (init != null) { 128 dump(init, indent + " "); 129 } 130 131 ClassFile[] innerClasses = cf.getInnerClasses(); 132 println("innerClasses: ", indent); 133 for (int i=0; i<innerClasses.length; i++) { 134 dump(innerClasses[i], indent + " "); 135 } 136 137 println("sourceFile: " + cf.getSourceFile(), indent); 138 println("synthetic: " + cf.isSynthetic(), indent); 139 println("deprecated: " + cf.isDeprecated(), indent); 140 println("attributes: ", indent); 141 dump(cf.getAttributes(), indent + " "); 142 143 println(); 144 } 145 146 private static void dump(FieldInfo field, String indent) { 147 println(field, indent); 148 println("name: " + field.getName(), indent); 149 println("type: " + field.getType(), indent); 150 println("accessFlags: " + field.getAccessFlags(), indent); 151 println("constantValue: " + field.getConstantValue(), indent); 152 println("synthetic: " + field.isSynthetic(), indent); 153 println("deprecated: " + field.isDeprecated(), indent); 154 println("attributes: ", indent); 155 dump(field.getAttributes(), indent + " "); 156 157 println(); 158 } 159 160 private static void dump(MethodInfo method, String indent) { 161 println(method, indent); 162 println("name: " + method.getName(), indent); 163 println("methodDescriptor: " + method.getMethodDescriptor(), indent); 164 println("accessFlags: " + method.getAccessFlags(), indent); 165 166 String [] exceptions = method.getExceptions(); 167 print("exceptions: ", indent); 168 for (int i=0; i<exceptions.length; i++) { 169 if (i > 0) { 170 System.out.print(", "); 171 } 172 System.out.print(exceptions[i]); 173 } 174 println(); 175 176 if (method.getCodeAttr() != null) { 177 println("code:", indent); 178 179 PrintWriter writer = new PrintWriter(System.out); 180 181 TypeDescriptor[] paramTypes = 182 method.getMethodDescriptor().getParameterTypes(); 183 boolean isStatic = method.getAccessFlags().isStatic(); 184 185 new CodeDisassembler(method).disassemble 186 (new CodeAssemblerPrinter(paramTypes, isStatic, 187 writer, indent + " ", null)); 188 189 writer.flush(); 190 } 191 192 println("synthetic: " + method.isSynthetic(), indent); 193 println("deprecated: " + method.isDeprecated(), indent); 194 println("attributes: ", indent); 195 dump(method.getAttributes(), indent + " "); 196 197 println(); 198 } 199 200 private static void dump(Attribute[] attributes, String indent) { 201 if (attributes == null) { 202 return; 203 } 204 for (int i=0; i<attributes.length; i++) { 205 Attribute attribute = attributes[i]; 206 println(attribute, indent); 207 Attribute[] subAttributes = attribute.getAttributes(); 208 if (subAttributes != null && subAttributes.length > 0) { 209 println("attributes: ", indent); 210 dump(subAttributes, indent + " "); 211 } 212 } 213 } 214 215 private static void print(Object obj, String indent) { 216 System.out.print(indent); 217 System.out.print(obj); 218 } 219 220 private static void println(Object obj, String indent) { 221 print(obj, indent); 222 println(); 223 } 224 225 private static void println() { 226 System.out.println(); 227 } 228 } 229 | Popular Tags |