001: /*
002: * Copyright 2003,2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package net.sf.cglib.core;
017:
018: import org.objectweb.asm.ClassWriter;
019: import org.objectweb.asm.ClassReader;
020: import org.objectweb.asm.util.TraceClassVisitor;
021:
022: import java.io.*;
023:
024: public class DebuggingClassWriter extends ClassWriter {
025:
026: public static final String DEBUG_LOCATION_PROPERTY = "cglib.debugLocation";
027:
028: private static String debugLocation;
029: private static boolean traceEnabled;
030:
031: private String className;
032: private String super Name;
033:
034: static {
035: debugLocation = System.getProperty(DEBUG_LOCATION_PROPERTY);
036: if (debugLocation != null) {
037: System.err.println("CGLIB debugging enabled, writing to '"
038: + debugLocation + "'");
039: try {
040: Class
041: .forName("org.objectweb.asm.util.TraceClassVisitor");
042: traceEnabled = true;
043: } catch (Throwable ignore) {
044: }
045: }
046: }
047:
048: public DebuggingClassWriter(boolean computeMaxs) {
049: super (computeMaxs);
050: }
051:
052: /**
053: * @deprecated
054: */
055: public DebuggingClassWriter(boolean computeMaxs, int major,
056: int minor) {
057: super (computeMaxs);
058: }
059:
060: public void visit(int version, int access, String name,
061: String signature, String super Name, String[] interfaces) {
062: className = name.replace('/', '.');
063: this .super Name = super Name.replace('/', '.');
064: super .visit(version, access, name, signature, super Name,
065: interfaces);
066: }
067:
068: public String getClassName() {
069: return className;
070: }
071:
072: public String getSuperName() {
073: return super Name;
074: }
075:
076: public byte[] toByteArray() {
077:
078: return (byte[]) java.security.AccessController
079: .doPrivileged(new java.security.PrivilegedAction() {
080: public Object run() {
081:
082: byte[] b = DebuggingClassWriter.super
083: .toByteArray();
084: if (debugLocation != null) {
085: String dirs = className.replace('.',
086: File.separatorChar);
087: try {
088: new File(debugLocation
089: + File.separatorChar + dirs)
090: .getParentFile().mkdirs();
091:
092: File file = new File(new File(
093: debugLocation), dirs + ".class");
094: OutputStream out = new BufferedOutputStream(
095: new FileOutputStream(file));
096: try {
097: out.write(b);
098: } finally {
099: out.close();
100: }
101:
102: if (traceEnabled) {
103: file = new File(new File(
104: debugLocation), dirs
105: + ".asm");
106: out = new BufferedOutputStream(
107: new FileOutputStream(file));
108: try {
109: ClassReader cr = new ClassReader(
110: b);
111: PrintWriter pw = new PrintWriter(
112: new OutputStreamWriter(
113: out));
114: TraceClassVisitor tcv = new TraceClassVisitor(
115: null, pw);
116: cr.accept(tcv, false);
117: pw.flush();
118: } finally {
119: out.close();
120: }
121: }
122: } catch (IOException e) {
123: throw new CodeGenerationException(e);
124: }
125: }
126: return b;
127: }
128: });
129:
130: }
131: }
|