1 16 package net.sf.cglib.core; 17 18 import org.objectweb.asm.ClassWriter; 19 import org.objectweb.asm.ClassReader; 20 import org.objectweb.asm.util.TraceClassVisitor; 21 22 import java.io.*; 23 24 public class DebuggingClassWriter extends ClassWriter { 25 26 public static final String DEBUG_LOCATION_PROPERTY = "cglib.debugLocation"; 27 28 private static String debugLocation; 29 private static boolean traceEnabled; 30 31 private String className; 32 private String superName; 33 34 static { 35 debugLocation = System.getProperty(DEBUG_LOCATION_PROPERTY); 36 if (debugLocation != null) { 37 System.err.println("CGLIB debugging enabled, writing to '" + debugLocation + "'"); 38 try { 39 Class.forName("org.objectweb.asm.util.TraceClassVisitor"); 40 traceEnabled = true; 41 } catch (Throwable ignore) { 42 } 43 } 44 } 45 46 public DebuggingClassWriter(boolean computeMaxs) { 47 super(computeMaxs); 48 } 49 50 53 public DebuggingClassWriter(boolean computeMaxs, int major, int minor) { 54 super(computeMaxs); 55 } 56 57 public void visit(int version, 58 int access, 59 String name, 60 String signature, 61 String superName, 62 String [] interfaces) { 63 className = name.replace('/', '.'); 64 this.superName = superName.replace('/', '.'); 65 super.visit(version, access, name, signature, superName, interfaces); 66 } 67 68 public String getClassName() { 69 return className; 70 } 71 72 public String getSuperName() { 73 return superName; 74 } 75 76 public byte[] toByteArray() { 77 78 return (byte[]) java.security.AccessController.doPrivileged( 79 new java.security.PrivilegedAction () { 80 public Object run() { 81 82 83 byte[] b = DebuggingClassWriter.super.toByteArray(); 84 if (debugLocation != null) { 85 String dirs = className.replace('.', File.separatorChar); 86 try { 87 new File(debugLocation + File.separatorChar + dirs).getParentFile().mkdirs(); 88 89 File file = new File(new File(debugLocation), dirs + ".class"); 90 OutputStream out = new BufferedOutputStream(new FileOutputStream(file)); 91 try { 92 out.write(b); 93 } finally { 94 out.close(); 95 } 96 97 if (traceEnabled) { 98 file = new File(new File(debugLocation), dirs + ".asm"); 99 out = new BufferedOutputStream(new FileOutputStream(file)); 100 try { 101 ClassReader cr = new ClassReader(b); 102 PrintWriter pw = new PrintWriter(new OutputStreamWriter(out)); 103 TraceClassVisitor tcv = new TraceClassVisitor(null, pw); 104 cr.accept(tcv, false); 105 pw.flush(); 106 } finally { 107 out.close(); 108 } 109 } 110 } catch (IOException e) { 111 throw new CodeGenerationException(e); 112 } 113 } 114 return b; 115 } 116 }); 117 118 } 119 } 120 | Popular Tags |