Skip to content

Commit a6bfc19

Browse files
committed
merged with rfrowe/master and Pkeidel/master and minor refactoring
1 parent 7f382f3 commit a6bfc19

3 files changed

Lines changed: 42 additions & 7 deletions

File tree

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
11
package org.mdkt.compiler;
22

3+
import java.util.List;
4+
import java.util.Locale;
5+
import javax.tools.Diagnostic;
6+
import javax.tools.JavaFileObject;
7+
38
public class CompilationException extends RuntimeException {
49
private static final long serialVersionUID = 5272588827551900536L;
510

6-
public CompilationException(String msg) {
7-
super(msg);
11+
public CompilationException(List<Diagnostic<? extends JavaFileObject>> diags) {
12+
super(buildMessage(diags));
13+
}
14+
15+
private static String buildMessage(List<Diagnostic<? extends JavaFileObject>> diags) {
16+
StringBuffer msg = new StringBuffer();
17+
msg.append("Unable to compile the source.");
18+
for (Diagnostic<?> diag : diags) {
19+
msg.append("\n").append("[kind=").append(diag.getKind());
20+
msg.append(", ").append("line=").append(diag.getLineNumber());
21+
msg.append(", ").append("message=").append(diag.getMessage(Locale.US)).append("]");
22+
}
23+
return msg.toString();
824
}
925
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
import java.util.Iterator;
66
import java.util.Map;
77

8+
import javax.tools.DiagnosticCollector;
89
import javax.tools.JavaCompiler;
10+
import javax.tools.JavaFileObject;
911
import javax.tools.ToolProvider;
1012

1113
/**
@@ -49,12 +51,12 @@ public Map<String, Class<?>> compileAll() throws Exception {
4951
for (int i = 0; i < code.length; i++) {
5052
code[i] = new CompiledCode(iter.next().getClassName());
5153
}
52-
54+
DiagnosticCollector<JavaFileObject> collector = new DiagnosticCollector<>();
5355
ExtendedStandardJavaFileManager fileManager = new ExtendedStandardJavaFileManager(javac.getStandardFileManager(null, null, null), classLoader);
54-
JavaCompiler.CompilationTask task = javac.getTask(null, fileManager, null, null, null, compilationUnits);
56+
JavaCompiler.CompilationTask task = javac.getTask(null, fileManager, collector, null, null, compilationUnits);
5557
boolean result = task.call();
56-
if (!result) {
57-
throw new RuntimeException("Unknown error during compilation.");
58+
if (!result || collector.getDiagnostics().size() > 0) {
59+
throw new CompilationException(collector.getDiagnostics());
5860
}
5961

6062
Map<String, Class<?>> classes = new HashMap<String, Class<?>>();

src/test/java/org/mdkt/compiler/InMemoryJavaCompilerTest.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
import java.util.Map;
44

55
import org.junit.Assert;
6+
import org.junit.Rule;
67
import org.junit.Test;
8+
import org.junit.rules.ExpectedException;
79

810
public class InMemoryJavaCompilerTest {
11+
@Rule
12+
public ExpectedException thrown = ExpectedException.none();
913

1014
@Test
1115
public void compile_WhenTypical() throws Exception {
@@ -22,7 +26,7 @@ public void compile_WhenTypical() throws Exception {
2226
}
2327

2428
@Test
25-
public void compile_WhenMultipleSources() throws Exception {
29+
public void compileAll_WhenTypical() throws Exception {
2630
String cls1 = "public class A{ public B b() { return new B(); }}";
2731
String cls2 = "public class B{ public String toString() { return \"B!\"; }}";
2832

@@ -50,4 +54,17 @@ public void compile_WhenSourceContainsInnerClasses() throws Exception {
5054
Assert.assertNotNull(helloClass);
5155
Assert.assertEquals(1, helloClass.getDeclaredMethods().length);
5256
}
57+
58+
@Test
59+
public void compile_whenError() throws Exception {
60+
thrown.expect(CompilationException.class);
61+
thrown.expectMessage("Unable to compile the source");
62+
StringBuffer sourceCode = new StringBuffer();
63+
64+
sourceCode.append("package org.mdkt;\n");
65+
sourceCode.append("public classHelloClass {\n");
66+
sourceCode.append(" public String hello() { return \"hello\"; }");
67+
sourceCode.append("}");
68+
InMemoryJavaCompiler.newInstance().compile("org.mdkt.HelloClass", sourceCode.toString());
69+
}
5370
}

0 commit comments

Comments
 (0)