1 15 16 package javassist.bytecode; 17 18 import java.io.PrintWriter ; 19 import javassist.Modifier; 20 import java.util.List ; 21 22 27 public class ClassFileWriter { 28 31 public static void print(ClassFile cf) { 32 print(cf, new PrintWriter (System.out, true)); 33 } 34 35 38 public static void print(ClassFile cf, PrintWriter out) { 39 List list; 40 int n; 41 42 45 int mod 46 = AccessFlag.toModifier(cf.getAccessFlags() 47 & ~AccessFlag.SYNCHRONIZED); 48 out.println("major: " + cf.major + ", minor: " + cf.minor); 49 out.println(Modifier.toString(mod) + " class " 50 + cf.getName() + " extends " + cf.getSuperclass()); 51 52 String [] infs = cf.getInterfaces(); 53 if (infs != null && infs.length > 0) { 54 out.print(" implements "); 55 out.print(infs[0]); 56 for (int i = 1; i < infs.length; ++i) 57 out.print(", " + infs[i]); 58 59 out.println(); 60 } 61 62 out.println(); 63 ConstPool cp = cf.getConstPool(); 64 list = cf.getFields(); 65 n = list.size(); 66 for (int i = 0; i < n; ++i) { 67 FieldInfo finfo = (FieldInfo)list.get(i); 68 int acc = finfo.getAccessFlags(); 69 out.println(Modifier.toString(AccessFlag.toModifier(acc)) 70 + " " + finfo.getName() + "\t" 71 + finfo.getDescriptor()); 72 printAttributes(finfo.getAttributes(), out); 73 } 74 75 out.println(); 76 list = cf.getMethods(); 77 n = list.size(); 78 for (int i = 0; i < n; ++i) { 79 MethodInfo minfo = (MethodInfo)list.get(i); 80 int acc = minfo.getAccessFlags(); 81 out.println(Modifier.toString(AccessFlag.toModifier(acc)) 82 + " " + minfo.getName() + "\t" 83 + minfo.getDescriptor()); 84 printAttributes(minfo.getAttributes(), out); 85 out.println(); 86 } 87 88 out.println(); 89 printAttributes(cf.getAttributes(), out); 90 } 91 92 static void printAttributes(List list, PrintWriter out) { 93 if (list == null) 94 return; 95 96 int n = list.size(); 97 for (int i = 0; i < n; ++i) { 98 AttributeInfo ai = (AttributeInfo)list.get(i); 99 if (ai instanceof CodeAttribute) { 100 CodeAttribute ca = (CodeAttribute)ai; 101 out.println("attribute: " + ai.getName() + ": " 102 + ai.getClass().getName()); 103 out.println("max stack " + ca.getMaxStack() 104 + ", max locals " + ca.getMaxLocals() 105 + ", " + ca.getExceptionTable().size() 106 + " catch blocks"); 107 out.println("<code attribute begin>"); 108 printAttributes(ca.getAttributes(), out); 109 out.println("<code attribute end>"); 110 } 111 else 112 out.println("attribute: " + ai.getName() 113 + " (" + ai.get().length + " byte): " 114 + ai.getClass().getName()); 115 } 116 } 117 } 118 | Popular Tags |