forked from dsaff/junit.contrib
/
Compiler.java
287 lines (264 loc) · 10.2 KB
/
Compiler.java
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.junit.contrib.assertthrows.proxy;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.HashMap;
/*# Java 6 #
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import javax.tools.JavaCompiler.CompilationTask;
//*/
/**
* This class allows to convert source code to a class. It uses one class loader
* per class, and internally uses <code>javax.tools.JavaCompiler</code> if
* available, and <code>com.sun.tools.javac.Main</code> otherwise.
* <p>
* Implementation note: this class is uses reflection to try to load a
* <code>javax.tools.JavaCompiler</code>, which also runs on Java 5, and can be
* compiled with Java 5. The source code can be switched to Java 6 however. To
* do that, replace <code>/*#
* Java 6 #</code> with <code>//#
* Java 6 # before compiling.
*
* @author Thomas Mueller
*/
public class Compiler {
/**
* The class name to source code map.
*/
HashMap<String, String> sources = new HashMap<String, String>();
/**
* The class name to byte code map.
*/
HashMap<String, Class<?>> compiled = new HashMap<String, Class<?>>();
/**
* Whether using the system java compiler (
* <code>javax.tools.ToolProvider.getSystemJavaCompiler</code>) is allowed.
*/
private boolean useSystemJavaCompiler = true;
private Class<?> javacSun;
/*# Java 6 #
private JavaCompiler javaCompiler;
/*/
private Object javaCompiler;
//*/
private String compileDir = System.getProperty("java.io.tmpdir", ".");
public void setUseSystemJavaCompiler(boolean useSystemJavaCompiler) {
this.useSystemJavaCompiler = useSystemJavaCompiler;
}
private void initCompiler() {
javaCompiler = null;
if (useSystemJavaCompiler) {
try {
/*# Java 6 #
javaCompiler = ToolProvider.getSystemJavaCompiler();
/*/
javaCompiler = ReflectionUtils.callStaticMethod(
"javax.tools.ToolProvider.getSystemJavaCompiler");
//*/
} catch (Exception e) {
// ignore
}
}
Class<?> javac;
try {
javac = Class.forName("com.sun.tools.javac.Main");
} catch (Exception e) {
javac = null;
}
javacSun = javac;
}
/**
* Set the source code for the specified class.
*
* @param className the class name
* @param source the source code
*/
public void setSource(String className, String source) {
sources.put(className, source);
}
/**
* Get the class object for the given name.
*
* @param packageAndClassName the class name
* @return the class
* @throws ClassNotFoundException if the class is not found or can't be compiled
*/
public Class<?> getClass(String packageAndClassName) throws ClassNotFoundException {
Class<?> compiledClass = compiled.get(packageAndClassName);
if (compiledClass != null) {
return compiledClass;
}
ClassLoader classLoader = new ClassLoader(getClass().getClassLoader()) {
public Class<?> findClass(String name) throws ClassNotFoundException {
Class<?> classInstance = compiled.get(name);
if (classInstance == null) {
String source = sources.get(name);
String packageName = null;
int idx = name.lastIndexOf('.');
String className;
if (idx >= 0) {
packageName = name.substring(0, idx);
className = name.substring(idx + 1);
} else {
className = name;
}
byte[] data;
try {
data = javacCompile(packageName, className, source);
} catch (IOException e) {
throw new ClassNotFoundException(
"Could not compile class " + className + ": " + e.getMessage(), e);
}
if (data == null) {
classInstance = findSystemClass(name);
} else {
classInstance = defineClass(name, data, 0, data.length);
compiled.put(name, classInstance);
}
}
return classInstance;
}
};
return classLoader.loadClass(packageAndClassName);
}
/**
* Compile the given class. This method tries to use the class
* "com.sun.tools.javac.Main" if available. If not, it tries to run "javac"
* in a separate process.
*
* @param packageName the package name
* @param className the class name
* @param source the source code
* @return the class file
*/
byte[] javacCompile(String packageName, String className, String source) throws IOException {
File dir = new File(compileDir);
if (packageName != null) {
dir = new File(dir, packageName.replace('.', '/'));
dir.mkdirs();
}
File javaFile = new File(dir, className + ".java");
File classFile = new File(dir, className + ".class");
classFile.delete();
FileOutputStream f = null;
try {
f = new FileOutputStream(javaFile);
f.write(source.getBytes("UTF-8"));
f.close();
f = null;
try {
initCompiler();
if (javaCompiler != null) {
javaxToolsJavac(javaFile);
} else if (javacSun != null) {
javacSun(javaFile);
} else {
throw new IOException("Could not load a java compiler");
}
} catch (IOException e) {
throw e;
} catch (Exception e) {
IOException io = new IOException("Error compiling " +
javaFile.getAbsolutePath() + ": " + e.getMessage());
io.initCause(e);
throw io;
} finally {
if (f != null) {
f.close();
}
}
byte[] data = new byte[(int) classFile.length()];
DataInputStream in = new DataInputStream(new FileInputStream(classFile));
in.readFully(data);
in.close();
return data;
} finally {
javaFile.delete();
classFile.delete();
}
}
private void javaxToolsJavac(File javaFile) throws Exception {
StringWriter writer = new StringWriter();
Iterable<String> options = Arrays.asList(
"-sourcepath", compileDir,
"-d", compileDir,
"-encoding", "UTF-8");
/*# Java 6 #
JavaCompiler compiler = (JavaCompiler) javaCompiler;
StandardJavaFileManager fileManager = compiler.
getStandardFileManager(null, null, Charset.forName("UTF-8"));
Iterable<? extends JavaFileObject> compilationUnits =
fileManager.getJavaFileObjects(javaFile);
CompilationTask task = compiler.getTask(
writer, fileManager, null, options, null, compilationUnits);
task.call();
fileManager.close();
/*/
Object compiler = javaCompiler;
Object fileManager = ReflectionUtils.callMethod(compiler, "getStandardFileManager",
null, null, Charset.forName("UTF-8"));
Object compilationUnits = ReflectionUtils.callMethod(fileManager, "getJavaFileObjects",
(Object) new File[] { javaFile });
Object task = ReflectionUtils.callMethod(compiler, "getTask",
writer, fileManager, null, options, null, compilationUnits);
ReflectionUtils.callMethod(task, "call");
ReflectionUtils.callMethod(fileManager, "close");
//*/
String err = writer.toString();
throwSyntaxError(err);
}
private void javacSun(File javaFile) throws Exception {
PrintStream old = System.err;
ByteArrayOutputStream buff = new ByteArrayOutputStream();
PrintStream temp = new PrintStream(buff);
try {
System.setErr(temp);
Method compile;
compile = javacSun.getMethod("compile", String[].class);
Object javac = javacSun.newInstance();
compile.invoke(javac, (Object) new String[] {
"-sourcepath", compileDir,
"-d", compileDir,
"-encoding", "UTF-8",
javaFile.getAbsolutePath() });
String err = new String(buff.toByteArray(), "UTF-8");
throwSyntaxError(err);
} finally {
System.setErr(old);
}
}
private void throwSyntaxError(String err) throws IOException {
if (err.startsWith("Note:")) {
// unchecked or unsafe operations - just a warning
} else if (err.length() > 0) {
throw new IOException(err);
}
}
}