1 | |
package org.truth0.codegen; |
2 | |
|
3 | |
import static java.util.Collections.singleton; |
4 | |
|
5 | |
import java.io.ByteArrayOutputStream; |
6 | |
import java.io.IOException; |
7 | |
import java.io.OutputStream; |
8 | |
import java.net.URI; |
9 | |
import java.net.URISyntaxException; |
10 | |
import java.util.HashMap; |
11 | |
import java.util.LinkedList; |
12 | |
import java.util.List; |
13 | |
import java.util.Map; |
14 | |
|
15 | |
import javax.tools.DiagnosticListener; |
16 | |
import javax.tools.FileObject; |
17 | |
import javax.tools.ForwardingJavaFileManager; |
18 | |
import javax.tools.JavaCompiler; |
19 | |
import javax.tools.JavaFileManager; |
20 | |
import javax.tools.JavaFileObject; |
21 | |
import javax.tools.SimpleJavaFileObject; |
22 | |
import javax.tools.ToolProvider; |
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 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | 0 | public class CompilingClassLoader extends ClassLoader { |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
public static class CompilerException extends Exception { |
69 | |
|
70 | |
private static final long serialVersionUID = -2936958840023603270L; |
71 | |
|
72 | |
public CompilerException(String message) { |
73 | 0 | super(message); |
74 | 0 | } |
75 | |
} |
76 | |
|
77 | 0 | private final Map<String, ByteArrayOutputStream> byteCodeForClasses = |
78 | |
new HashMap<String, ByteArrayOutputStream>(); |
79 | |
|
80 | |
private static final URI EMPTY_URI; |
81 | |
|
82 | |
static { |
83 | |
try { |
84 | |
|
85 | 0 | EMPTY_URI = new URI(""); |
86 | 0 | } catch (URISyntaxException e) { |
87 | 0 | throw new Error(e); |
88 | 0 | } |
89 | 0 | } |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
public CompilingClassLoader(ClassLoader parent, String className, String sourceCode, |
98 | |
DiagnosticListener<JavaFileObject> diagnosticListener) throws CompilerException { |
99 | 0 | super(parent); |
100 | 0 | if (!compileSourceCodeToByteCode(className, sourceCode, diagnosticListener)) { |
101 | 0 | throw new CompilerException("Could not compile " + className); |
102 | |
} |
103 | 0 | } |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
@Override |
110 | |
public Class<?> findClass(String name) throws ClassNotFoundException { |
111 | 0 | ByteArrayOutputStream byteCode = byteCodeForClasses.get(name); |
112 | 0 | if (byteCode == null) { |
113 | 0 | throw new ClassNotFoundException(name); |
114 | |
} |
115 | 0 | return defineClass(name, byteCode.toByteArray(), 0, byteCode.size()); |
116 | |
} |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
private boolean compileSourceCodeToByteCode(String className, String sourceCode, |
122 | |
DiagnosticListener<JavaFileObject> diagnosticListener) { |
123 | 0 | JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler(); |
124 | |
|
125 | |
|
126 | 0 | InMemoryFileManager fileManager = |
127 | |
new InMemoryFileManager(javaCompiler.getStandardFileManager(null, null, null)); |
128 | 0 | JavaFileObject javaFile = new InMemoryJavaFile(className, sourceCode); |
129 | |
|
130 | |
|
131 | |
|
132 | 0 | System.setProperty("useJavaUtilZip", "true"); |
133 | 0 | List<String> options = new LinkedList<String>(); |
134 | |
|
135 | |
|
136 | 0 | options.add("-XDuseJavaUtilZip"); |
137 | |
|
138 | |
|
139 | 0 | JavaCompiler.CompilationTask compilationTask = javaCompiler.getTask(null, |
140 | |
|
141 | |
|
142 | |
fileManager, diagnosticListener, options, null, singleton(javaFile)); |
143 | 0 | return compilationTask.call(); |
144 | |
} |
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
private class InMemoryFileManager extends ForwardingJavaFileManager<JavaFileManager> { |
156 | |
|
157 | 0 | public InMemoryFileManager(JavaFileManager fileManager) { |
158 | 0 | super(fileManager); |
159 | 0 | } |
160 | |
|
161 | |
@Override |
162 | |
public JavaFileObject getJavaFileForOutput(Location location, final String className, |
163 | |
JavaFileObject.Kind kind, FileObject sibling) throws IOException { |
164 | 0 | return new SimpleJavaFileObject(EMPTY_URI, kind) { |
165 | |
@Override |
166 | |
public OutputStream openOutputStream() throws IOException { |
167 | 0 | ByteArrayOutputStream outputStream = byteCodeForClasses.get(className); |
168 | 0 | if (outputStream != null) { |
169 | 0 | throw new IllegalStateException("Cannot write more than once"); |
170 | |
} |
171 | |
|
172 | 0 | outputStream = new ByteArrayOutputStream(256); |
173 | 0 | byteCodeForClasses.put(className, outputStream); |
174 | 0 | return outputStream; |
175 | |
} |
176 | |
}; |
177 | |
} |
178 | |
} |
179 | |
|
180 | |
private static class InMemoryJavaFile extends SimpleJavaFileObject { |
181 | |
|
182 | |
private final String sourceCode; |
183 | |
|
184 | |
public InMemoryJavaFile(String className, String sourceCode) { |
185 | 0 | super(makeUri(className), Kind.SOURCE); |
186 | 0 | this.sourceCode = sourceCode; |
187 | 0 | } |
188 | |
|
189 | |
private static URI makeUri(String className) { |
190 | |
try { |
191 | 0 | return new URI(className.replaceAll("\\.", "/") + Kind.SOURCE.extension); |
192 | 0 | } catch (URISyntaxException e) { |
193 | 0 | throw new RuntimeException(e); |
194 | |
} |
195 | |
} |
196 | |
|
197 | |
@Override |
198 | |
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { |
199 | 0 | return sourceCode; |
200 | |
} |
201 | |
} |
202 | |
} |