1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 package org.codehaus.groovy.classgen;
48
49 import groovy.lang.GroovyClassLoader;
50
51 import java.io.OutputStreamWriter;
52 import java.io.PrintWriter;
53 import java.io.StringWriter;
54
55 import org.codehaus.groovy.ast.ClassNode;
56 import org.codehaus.groovy.ast.CompileUnit;
57 import org.codehaus.groovy.control.CompilationUnit;
58 import org.codehaus.groovy.control.CompilerConfiguration;
59 import org.objectweb.asm.ClassVisitor;
60 import org.objectweb.asm.Constants;
61 import org.objectweb.asm.util.CheckClassAdapter;
62 import org.objectweb.asm.util.ASMifierClassVisitor;
63
64 /***
65 * A class loader used for debugging the bytecode generation.
66 * This will log all bytecode being loaded
67 *
68 * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
69 * @version $Revision: 1.9 $
70 */
71 public class DumpingClassLoader extends GroovyClassLoader implements Constants {
72
73 protected static boolean CHECK_CLASS = false;
74 protected static boolean DUMP_CLASS = true;
75
76 public DumpingClassLoader(ClassLoader parentLoader) {
77 super(parentLoader);
78 }
79
80
81 protected class DebugCollector extends ClassCollector {
82
83 DebugCollector(GroovyClassLoader cl, CompilationUnit unit) {
84 super(cl, unit);
85 }
86
87 public void call(ClassVisitor classWriter, ClassNode classNode) {
88
89 if (DUMP_CLASS) {
90 dumper.visitClass(classNode);
91 }
92
93 if (CHECK_CLASS) {
94 checker.visitClass(classNode);
95 }
96
97 super.call(classWriter, classNode);
98 }
99 }
100
101 protected ClassCollector createCollector(CompilationUnit unit) {
102 return new DebugCollector(this, unit);
103 }
104
105 protected ASMifierClassVisitor dumpVisitor = new ASMifierClassVisitor(new PrintWriter(new OutputStreamWriter(System.out)));
106 protected ASMifierClassVisitor invisibleDumpVisitor = new ASMifierClassVisitor(new PrintWriter(new StringWriter()));
107 protected CompileUnit unit = new CompileUnit(this, new CompilerConfiguration());
108 protected ClassGenerator checker =
109 new AsmClassGenerator2(new GeneratorContext(unit), new CheckClassAdapter(invisibleDumpVisitor), this, null);
110 protected ClassGenerator dumper = new AsmClassGenerator2(new GeneratorContext(unit), dumpVisitor, this, null);
111
112 }