|
1 | 1 | package org.mdkt.compiler; |
2 | 2 |
|
3 | | -import java.io.IOException; |
4 | 3 | import java.util.Collection; |
5 | 4 | import java.util.HashMap; |
6 | 5 | import java.util.Iterator; |
7 | 6 | import java.util.Map; |
8 | 7 |
|
9 | | -import javax.tools.Diagnostic; |
10 | | -import javax.tools.DiagnosticListener; |
11 | 8 | import javax.tools.JavaCompiler; |
12 | | -import javax.tools.JavaFileObject; |
13 | 9 | import javax.tools.ToolProvider; |
14 | 10 |
|
15 | 11 | /** |
16 | | - * Created by trung on 5/3/15. |
17 | | - * Edited by turpid-monkey on 9/25/15, added support for multiple, dependent compile units. |
| 12 | + * Complile Java sources in-memory |
18 | 13 | */ |
19 | 14 | public class InMemoryJavaCompiler { |
20 | | - JavaCompiler javac; |
21 | | - DynamicClassLoader classLoader; |
| 15 | + private JavaCompiler javac; |
| 16 | + private DynamicClassLoader classLoader; |
22 | 17 |
|
23 | | - Map<String, SourceCode> clazzCode = new HashMap<String, SourceCode>(); |
| 18 | + private Map<String, SourceCode> sourceCodes = new HashMap<String, SourceCode>(); |
24 | 19 |
|
25 | | - public InMemoryJavaCompiler(ClassLoader parent) { |
26 | | - this(ToolProvider.getSystemJavaCompiler(), parent); |
| 20 | + public static InMemoryJavaCompiler newInstance() { |
| 21 | + return new InMemoryJavaCompiler(); |
27 | 22 | } |
28 | 23 |
|
29 | | - public InMemoryJavaCompiler(JavaCompiler javac, ClassLoader parent) { |
30 | | - this.javac = javac; |
31 | | - this.classLoader = new DynamicClassLoader(parent); |
32 | | - } |
33 | | - |
34 | | - public InMemoryJavaCompiler() { |
35 | | - this(ToolProvider.getSystemJavaCompiler(), ClassLoader |
36 | | - .getSystemClassLoader()); |
| 24 | + private InMemoryJavaCompiler() { |
| 25 | + this.javac = ToolProvider.getSystemJavaCompiler(); |
| 26 | + this.classLoader = new DynamicClassLoader(ClassLoader.getSystemClassLoader()); |
37 | 27 | } |
38 | 28 |
|
39 | | - public void addSource(String className, String sourceCodeInText) |
40 | | - throws Exception { |
41 | | - clazzCode.put(className, new SourceCode(className, sourceCodeInText)); |
| 29 | + public InMemoryJavaCompiler useParentClassLoader(ClassLoader parent) { |
| 30 | + this.classLoader = new DynamicClassLoader(parent); |
| 31 | + return this; |
42 | 32 | } |
43 | 33 |
|
| 34 | + /** |
| 35 | + * Compile all sources |
| 36 | + * |
| 37 | + * @return |
| 38 | + * @throws Exception |
| 39 | + */ |
44 | 40 | public Map<String, Class<?>> compileAll() throws Exception { |
45 | | - Collection<SourceCode> compilationUnits = clazzCode.values(); |
| 41 | + if (sourceCodes.size() == 0) { |
| 42 | + throw new Exception("No source code to compile"); |
| 43 | + } |
| 44 | + Collection<SourceCode> compilationUnits = sourceCodes.values(); |
46 | 45 | CompiledCode[] code; |
47 | | - |
| 46 | + |
48 | 47 | code = new CompiledCode[compilationUnits.size()]; |
49 | 48 | Iterator<SourceCode> iter = compilationUnits.iterator(); |
50 | | - for (int i=0; i<code.length; i++) |
51 | | - { |
| 49 | + for (int i = 0; i < code.length; i++) { |
52 | 50 | code[i] = new CompiledCode(iter.next().getClassName()); |
53 | 51 | } |
54 | | - |
55 | | - ExtendedStandardJavaFileManager fileManager = new ExtendedStandardJavaFileManager( |
56 | | - javac.getStandardFileManager(null, null, null), classLoader); |
57 | | - JavaCompiler.CompilationTask task = javac.getTask(null, fileManager, |
58 | | - null, null, null, compilationUnits); |
| 52 | + |
| 53 | + ExtendedStandardJavaFileManager fileManager = new ExtendedStandardJavaFileManager(javac.getStandardFileManager(null, null, null), classLoader); |
| 54 | + JavaCompiler.CompilationTask task = javac.getTask(null, fileManager, null, null, null, compilationUnits); |
59 | 55 | boolean result = task.call(); |
60 | | - if (!result) |
| 56 | + if (!result) { |
61 | 57 | throw new RuntimeException("Unknown error during compilation."); |
| 58 | + } |
| 59 | + |
62 | 60 | Map<String, Class<?>> classes = new HashMap<String, Class<?>>(); |
63 | | - for (String className : clazzCode.keySet()) { |
| 61 | + for (String className : sourceCodes.keySet()) { |
64 | 62 | classes.put(className, classLoader.loadClass(className)); |
65 | 63 | } |
66 | 64 | return classes; |
67 | 65 | } |
68 | 66 |
|
69 | | - public static Class<?> compile(String className, String sourceCodeInText) |
70 | | - throws Exception { |
71 | | - InMemoryJavaCompiler comp = new InMemoryJavaCompiler(); |
72 | | - comp.addSource(className, sourceCodeInText); |
73 | | - Map<String, Class<?>> clzzes = comp.compileAll(); |
74 | | - Class<?> result = clzzes.get(className); |
75 | | - return result; |
| 67 | + /** |
| 68 | + * Compile single source |
| 69 | + * |
| 70 | + * @param className |
| 71 | + * @param sourceCode |
| 72 | + * @return |
| 73 | + * @throws Exception |
| 74 | + */ |
| 75 | + public Class<?> compile(String className, String sourceCode) throws Exception { |
| 76 | + return addSource(className, sourceCode).compileAll().get(className); |
76 | 77 | } |
77 | | - |
78 | | - public DynamicClassLoader getClassLoader() { |
79 | | - return classLoader; |
| 78 | + |
| 79 | + /** |
| 80 | + * Add source code to the compiler |
| 81 | + * |
| 82 | + * @param className |
| 83 | + * @param sourceCode |
| 84 | + * @return |
| 85 | + * @throws Exception |
| 86 | + * @see {@link #compileAll()} |
| 87 | + */ |
| 88 | + public InMemoryJavaCompiler addSource(String className, String sourceCode) throws Exception { |
| 89 | + sourceCodes.put(className, new SourceCode(className, sourceCode)); |
| 90 | + return this; |
80 | 91 | } |
81 | 92 | } |
0 commit comments