diff --git a/pom.xml b/pom.xml
index 4011db2..177c674 100644
--- a/pom.xml
+++ b/pom.xml
@@ -18,8 +18,12 @@
1.6.3
2.5.1
gpg2
+
+
+ 1.7.5
+ 4.12
-
+
Apache License, Version 2.0
@@ -38,7 +42,7 @@
scm:git:git://github.com/trung/InMemoryJavaCompiler
scm:git:git@github.com:trung/InMemoryJavaCompiler.git
HEAD
-
+
@@ -61,10 +65,21 @@
+
+ org.slf4j
+ slf4j-api
+ ${slf4j.version}
+
+
+ org.slf4j
+ slf4j-jdk14
+ ${slf4j.version}
+ test
+
junit
junit
- 4.12
+ ${junit.version}
test
diff --git a/src/main/java/org/mdkt/compiler/CompilationException.java b/src/main/java/org/mdkt/compiler/CompilationException.java
index a9825f8..00c7f7f 100644
--- a/src/main/java/org/mdkt/compiler/CompilationException.java
+++ b/src/main/java/org/mdkt/compiler/CompilationException.java
@@ -1,25 +1,10 @@
package org.mdkt.compiler;
-import java.util.List;
-import java.util.Locale;
-import javax.tools.Diagnostic;
-import javax.tools.JavaFileObject;
-
public class CompilationException extends RuntimeException {
private static final long serialVersionUID = 5272588827551900536L;
- public CompilationException(List> diags) {
- super(buildMessage(diags));
+ public CompilationException(String msg) {
+ super(msg);
}
- private static String buildMessage(List> diags) {
- StringBuffer msg = new StringBuffer();
- msg.append("Unable to compile the source.");
- for (Diagnostic> diag : diags) {
- msg.append("\n").append("[kind=").append(diag.getKind());
- msg.append(", ").append("line=").append(diag.getLineNumber());
- msg.append(", ").append("message=").append(diag.getMessage(Locale.US)).append("]");
- }
- return msg.toString();
- }
}
diff --git a/src/main/java/org/mdkt/compiler/InMemoryJavaCompiler.java b/src/main/java/org/mdkt/compiler/InMemoryJavaCompiler.java
index 19f0391..b853590 100644
--- a/src/main/java/org/mdkt/compiler/InMemoryJavaCompiler.java
+++ b/src/main/java/org/mdkt/compiler/InMemoryJavaCompiler.java
@@ -32,27 +32,28 @@ public InMemoryJavaCompiler useParentClassLoader(ClassLoader parent) {
/**
* @return the class loader used internally by the compiler
*/
- public ClassLoader getClassloader()
- {
+ public ClassLoader getClassloader() {
return classLoader;
}
/**
* Options used by the compiler, e.g. '-Xlint:unchecked'.
+ *
* @param options
* @return
*/
- public InMemoryJavaCompiler useOptions(String... options)
- {
+ public InMemoryJavaCompiler useOptions(String... options) {
this.options = Arrays.asList(options);
return this;
}
/**
- * Ignore non-critical compiler output, like unchecked/unsafe operation warnings.
+ * Ignore non-critical compiler output, like unchecked/unsafe operation
+ * warnings.
+ *
* @return
*/
- public InMemoryJavaCompiler useIgnoreWarnings() {
+ public InMemoryJavaCompiler ignoreWarnings() {
ignoreWarnings = true;
return this;
}
@@ -65,7 +66,7 @@ public InMemoryJavaCompiler useIgnoreWarnings() {
*/
public Map> compileAll() throws Exception {
if (sourceCodes.size() == 0) {
- throw new Exception("No source code to compile");
+ throw new CompilationException("No source code to compile");
}
Collection compilationUnits = sourceCodes.values();
CompiledCode[] code;
@@ -80,15 +81,30 @@ public Map> compileAll() throws Exception {
JavaCompiler.CompilationTask task = javac.getTask(null, fileManager, collector, options, null, compilationUnits);
boolean result = task.call();
if (!result || collector.getDiagnostics().size() > 0) {
- for (Diagnostic extends JavaFileObject> d : collector.getDiagnostics()) {
- if (ignoreWarnings &&
- (d.getKind() == Diagnostic.Kind.NOTE || d.getKind() == Diagnostic.Kind.MANDATORY_WARNING || d.getKind() == Diagnostic.Kind.WARNING))
- continue;
- else {
- throw new CompilationException(collector.getDiagnostics());
- }
+ StringBuffer exceptionMsg = new StringBuffer();
+ exceptionMsg.append("Unable to compile the source");
+ boolean hasWarnings = false;
+ boolean hasErrors = false;
+ for (Diagnostic extends JavaFileObject> d : collector.getDiagnostics()) {
+ switch (d.getKind()) {
+ case NOTE:
+ case MANDATORY_WARNING:
+ case WARNING:
+ hasWarnings = true;
+ break;
+ case OTHER:
+ case ERROR:
+ default:
+ hasErrors = true;
+ break;
}
-
+ exceptionMsg.append("\n").append("[kind=").append(d.getKind());
+ exceptionMsg.append(", ").append("line=").append(d.getLineNumber());
+ exceptionMsg.append(", ").append("message=").append(d.getMessage(Locale.US)).append("]");
+ }
+ if (hasWarnings && !ignoreWarnings || hasErrors) {
+ throw new CompilationException(exceptionMsg.toString());
+ }
}
Map> classes = new HashMap>();
diff --git a/src/test/java/org/mdkt/compiler/InMemoryJavaCompilerTest.java b/src/test/java/org/mdkt/compiler/InMemoryJavaCompilerTest.java
index 1c58c63..c3c16e8 100644
--- a/src/test/java/org/mdkt/compiler/InMemoryJavaCompilerTest.java
+++ b/src/test/java/org/mdkt/compiler/InMemoryJavaCompilerTest.java
@@ -7,8 +7,12 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
public class InMemoryJavaCompilerTest {
+ private static final Logger logger = LoggerFactory.getLogger(InMemoryJavaCompilerTest.class);
+
@Rule
public ExpectedException thrown = ExpectedException.none();
@@ -69,7 +73,8 @@ public void compile_whenError() throws Exception {
InMemoryJavaCompiler.newInstance().compile("org.mdkt.HelloClass", sourceCode.toString());
}
- @Test public void compile_FailOnWarnings() throws Exception{
+ @Test
+ public void compile_WhenFailOnWarnings() throws Exception {
thrown.expect(CompilationException.class);
StringBuffer sourceCode = new StringBuffer();
@@ -80,15 +85,33 @@ public void compile_whenError() throws Exception {
InMemoryJavaCompiler.newInstance().compile("org.mdkt.HelloClass", sourceCode.toString());
}
- @Test public void compile_CompileAndIgnoreWarnings() throws Exception{
+ @Test
+ public void compile_WhenIgnoreWarnings() throws Exception {
StringBuffer sourceCode = new StringBuffer();
sourceCode.append("package org.mdkt;\n");
sourceCode.append("public class HelloClass {\n");
sourceCode.append(" public java.util.List hello() { return new java.util.ArrayList(); }");
sourceCode.append("}");
- Class> helloClass = InMemoryJavaCompiler.newInstance().useIgnoreWarnings().compile("org.mdkt.HelloClass", sourceCode.toString());
- List> res = (List) helloClass.getMethod("hello").invoke(helloClass.newInstance());
+ Class> helloClass = InMemoryJavaCompiler.newInstance().ignoreWarnings().compile("org.mdkt.HelloClass", sourceCode.toString());
+ List> res = (List>) helloClass.getMethod("hello").invoke(helloClass.newInstance());
Assert.assertEquals(0, res.size());
}
+
+ @Test
+ public void compile_WhenWarningsAndErrors() throws Exception {
+ thrown.expect(CompilationException.class);
+ StringBuffer sourceCode = new StringBuffer();
+
+ sourceCode.append("package org.mdkt;\n");
+ sourceCode.append("public class HelloClass extends xxx {\n");
+ sourceCode.append(" public java.util.List hello() { return new java.util.ArrayList(); }");
+ sourceCode.append("}");
+ try {
+ InMemoryJavaCompiler.newInstance().compile("org.mdkt.HelloClass", sourceCode.toString());
+ } catch (Exception e) {
+ logger.info("Exception caught: {}", e);
+ throw e;
+ }
+ }
}