KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > cglib > core > DebuggingClassWriter


1 /*
2  * Copyright 2003,2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc DEBUG_LOCATION_PROPERTY = "cglib.debugLocation";
27     
28     private static String JavaDoc debugLocation;
29     private static boolean traceEnabled;
30     
31     private String JavaDoc className;
32     private String JavaDoc 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 JavaDoc ignore) {
42             }
43         }
44     }
45     
46     public DebuggingClassWriter(boolean computeMaxs) {
47         super(computeMaxs);
48     }
49
50     /**
51      * @deprecated
52      */

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 JavaDoc name,
60                       String JavaDoc signature,
61                       String JavaDoc superName,
62                       String JavaDoc[] interfaces) {
63         className = name.replace('/', '.');
64         this.superName = superName.replace('/', '.');
65         super.visit(version, access, name, signature, superName, interfaces);
66     }
67     
68     public String JavaDoc getClassName() {
69         return className;
70     }
71     
72     public String JavaDoc getSuperName() {
73         return superName;
74     }
75     
76     public byte[] toByteArray() {
77         
78       return (byte[]) java.security.AccessController.doPrivileged(
79         new java.security.PrivilegedAction JavaDoc() {
80             public Object JavaDoc run() {
81                 
82                 
83                 byte[] b = DebuggingClassWriter.super.toByteArray();
84                 if (debugLocation != null) {
85                     String JavaDoc 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