Skip to content

Commit d77a452

Browse files
committed
merge pr trung#18&trung#19
1 parent d84c404 commit d77a452

File tree

6 files changed

+283
-104
lines changed

6 files changed

+283
-104
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.mdkt.compiler;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
import javax.tools.Diagnostic;
7+
import javax.tools.JavaFileObject;
8+
9+
public interface CompilationResult {
10+
/**
11+
* Return the compiled classes
12+
*/
13+
Map<String, Class<?>> classMap() throws ClassNotFoundException;
14+
15+
/**
16+
* Determine if the compilation did succeed
17+
*/
18+
boolean compilationSucceeded();
19+
20+
/**
21+
* Determine if any warnings are present
22+
*/
23+
boolean hasWarnings();
24+
25+
/**
26+
* Determine if any errors are present
27+
*/
28+
boolean hasErrors();
29+
30+
/**
31+
* Return the diagnostics produced by the compiler
32+
*/
33+
List<Diagnostic<? extends JavaFileObject>> getDiagnostics();
34+
35+
/**
36+
* Throw an exception if the compilation did not succeed
37+
*/
38+
CompilationResult checkNoErrors();
39+
40+
}

src/main/java/org/mdkt/compiler/ExtendedStandardJavaFileManager.java

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package org.mdkt.compiler;
22

3-
import java.io.FileNotFoundException;
4-
import java.io.IOException;
5-
import java.util.ArrayList;
6-
import java.util.Arrays;
7-
import java.util.List;
83

94
import javax.tools.FileObject;
105
import javax.tools.ForwardingJavaFileManager;
116
import javax.tools.JavaFileManager;
127
import javax.tools.JavaFileObject;
8+
import java.io.IOException;
9+
import java.util.Arrays;
10+
import java.util.Map;
11+
import java.util.stream.Collectors;
1312

1413
/**
1514
* Created by trung on 5/3/15. Edited by turpid-monkey on 9/25/15, completed
@@ -18,20 +17,20 @@
1817
public class ExtendedStandardJavaFileManager extends
1918
ForwardingJavaFileManager<JavaFileManager> {
2019

21-
private List<CompiledCode> compiledCode = new ArrayList<CompiledCode>();
20+
private Map<String, CompiledCode> compiledCode;
2221
private DynamicClassLoader cl;
2322

2423
/**
2524
* Creates a new instance of ForwardingJavaFileManager.
2625
*
27-
* @param fileManager
28-
* delegate to this file manager
26+
* @param fileManager delegate to this file manager
2927
* @param cl
3028
*/
3129
protected ExtendedStandardJavaFileManager(JavaFileManager fileManager,
32-
DynamicClassLoader cl) {
30+
DynamicClassLoader cl, CompiledCode[] compiledCode) {
3331
super(fileManager);
3432
this.cl = cl;
33+
this.compiledCode = Arrays.stream(compiledCode).collect(Collectors.toMap(CompiledCode::getClassName, c -> c));
3534
}
3635

3736
@Override
@@ -40,8 +39,11 @@ public JavaFileObject getJavaFileForOutput(
4039
JavaFileObject.Kind kind, FileObject sibling) throws IOException {
4140

4241
try {
43-
CompiledCode innerClass = new CompiledCode(className);
44-
compiledCode.add(innerClass);
42+
CompiledCode innerClass = compiledCode.get(className);
43+
if (innerClass == null) {
44+
innerClass = new CompiledCode(className);
45+
compiledCode.put(className, innerClass);
46+
}
4547
cl.addCode(innerClass);
4648
return innerClass;
4749
} catch (Exception e) {

0 commit comments

Comments
 (0)