Skip to content

Commit fe47f01

Browse files
committed
merged with turpid-monkey/master with some minor refactoring
1 parent ffca555 commit fe47f01

4 files changed

Lines changed: 78 additions & 39 deletions

File tree

pom.xml

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@
1818
<nexus-staging-maven-plugin.version>1.6.3</nexus-staging-maven-plugin.version>
1919
<maven-release-plugin.version>2.5.1</maven-release-plugin.version>
2020
<gpg.executable>gpg2</gpg.executable>
21+
22+
<!-- dependencies -->
23+
<slf4j.version>1.7.5</slf4j.version>
24+
<junit.version>4.12</junit.version>
2125
</properties>
22-
26+
2327
<licenses>
2428
<license>
2529
<name>Apache License, Version 2.0</name>
@@ -38,7 +42,7 @@
3842
<connection>scm:git:git://github.com/trung/InMemoryJavaCompiler</connection>
3943
<developerConnection>scm:git:git@github.com:trung/InMemoryJavaCompiler.git</developerConnection>
4044
<tag>HEAD</tag>
41-
</scm>
45+
</scm>
4246

4347
<distributionManagement>
4448
<snapshotRepository>
@@ -61,10 +65,21 @@
6165
</developers>
6266

6367
<dependencies>
68+
<dependency>
69+
<groupId>org.slf4j</groupId>
70+
<artifactId>slf4j-api</artifactId>
71+
<version>${slf4j.version}</version>
72+
</dependency>
73+
<dependency>
74+
<groupId>org.slf4j</groupId>
75+
<artifactId>slf4j-jdk14</artifactId>
76+
<version>${slf4j.version}</version>
77+
<scope>test</scope>
78+
</dependency>
6479
<dependency>
6580
<groupId>junit</groupId>
6681
<artifactId>junit</artifactId>
67-
<version>4.12</version>
82+
<version>${junit.version}</version>
6883
<scope>test</scope>
6984
</dependency>
7085
</dependencies>
Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,10 @@
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-
83
public class CompilationException extends RuntimeException {
94
private static final long serialVersionUID = 5272588827551900536L;
105

11-
public CompilationException(List<Diagnostic<? extends JavaFileObject>> diags) {
12-
super(buildMessage(diags));
6+
public CompilationException(String msg) {
7+
super(msg);
138
}
149

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();
24-
}
2510
}

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

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,28 @@ public InMemoryJavaCompiler useParentClassLoader(ClassLoader parent) {
3232
/**
3333
* @return the class loader used internally by the compiler
3434
*/
35-
public ClassLoader getClassloader()
36-
{
35+
public ClassLoader getClassloader() {
3736
return classLoader;
3837
}
3938

4039
/**
4140
* Options used by the compiler, e.g. '-Xlint:unchecked'.
41+
*
4242
* @param options
4343
* @return
4444
*/
45-
public InMemoryJavaCompiler useOptions(String... options)
46-
{
45+
public InMemoryJavaCompiler useOptions(String... options) {
4746
this.options = Arrays.asList(options);
4847
return this;
4948
}
5049

5150
/**
52-
* Ignore non-critical compiler output, like unchecked/unsafe operation warnings.
51+
* Ignore non-critical compiler output, like unchecked/unsafe operation
52+
* warnings.
53+
*
5354
* @return
5455
*/
55-
public InMemoryJavaCompiler useIgnoreWarnings() {
56+
public InMemoryJavaCompiler ignoreWarnings() {
5657
ignoreWarnings = true;
5758
return this;
5859
}
@@ -65,7 +66,7 @@ public InMemoryJavaCompiler useIgnoreWarnings() {
6566
*/
6667
public Map<String, Class<?>> compileAll() throws Exception {
6768
if (sourceCodes.size() == 0) {
68-
throw new Exception("No source code to compile");
69+
throw new CompilationException("No source code to compile");
6970
}
7071
Collection<SourceCode> compilationUnits = sourceCodes.values();
7172
CompiledCode[] code;
@@ -80,15 +81,30 @@ public Map<String, Class<?>> compileAll() throws Exception {
8081
JavaCompiler.CompilationTask task = javac.getTask(null, fileManager, collector, options, null, compilationUnits);
8182
boolean result = task.call();
8283
if (!result || collector.getDiagnostics().size() > 0) {
83-
for (Diagnostic<? extends JavaFileObject> d : collector.getDiagnostics()) {
84-
if (ignoreWarnings &&
85-
(d.getKind() == Diagnostic.Kind.NOTE || d.getKind() == Diagnostic.Kind.MANDATORY_WARNING || d.getKind() == Diagnostic.Kind.WARNING))
86-
continue;
87-
else {
88-
throw new CompilationException(collector.getDiagnostics());
89-
}
84+
StringBuffer exceptionMsg = new StringBuffer();
85+
exceptionMsg.append("Unable to compile the source");
86+
boolean hasWarnings = false;
87+
boolean hasErrors = false;
88+
for (Diagnostic<? extends JavaFileObject> d : collector.getDiagnostics()) {
89+
switch (d.getKind()) {
90+
case NOTE:
91+
case MANDATORY_WARNING:
92+
case WARNING:
93+
hasWarnings = true;
94+
break;
95+
case OTHER:
96+
case ERROR:
97+
default:
98+
hasErrors = true;
99+
break;
90100
}
91-
101+
exceptionMsg.append("\n").append("[kind=").append(d.getKind());
102+
exceptionMsg.append(", ").append("line=").append(d.getLineNumber());
103+
exceptionMsg.append(", ").append("message=").append(d.getMessage(Locale.US)).append("]");
104+
}
105+
if (hasWarnings && !ignoreWarnings || hasErrors) {
106+
throw new CompilationException(exceptionMsg.toString());
107+
}
92108
}
93109

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

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77
import org.junit.Rule;
88
import org.junit.Test;
99
import org.junit.rules.ExpectedException;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
1012

1113
public class InMemoryJavaCompilerTest {
14+
private static final Logger logger = LoggerFactory.getLogger(InMemoryJavaCompilerTest.class);
15+
1216
@Rule
1317
public ExpectedException thrown = ExpectedException.none();
1418

@@ -69,7 +73,8 @@ public void compile_whenError() throws Exception {
6973
InMemoryJavaCompiler.newInstance().compile("org.mdkt.HelloClass", sourceCode.toString());
7074
}
7175

72-
@Test public void compile_FailOnWarnings() throws Exception{
76+
@Test
77+
public void compile_WhenFailOnWarnings() throws Exception {
7378
thrown.expect(CompilationException.class);
7479
StringBuffer sourceCode = new StringBuffer();
7580

@@ -80,15 +85,33 @@ public void compile_whenError() throws Exception {
8085
InMemoryJavaCompiler.newInstance().compile("org.mdkt.HelloClass", sourceCode.toString());
8186
}
8287

83-
@Test public void compile_CompileAndIgnoreWarnings() throws Exception{
88+
@Test
89+
public void compile_WhenIgnoreWarnings() throws Exception {
8490
StringBuffer sourceCode = new StringBuffer();
8591

8692
sourceCode.append("package org.mdkt;\n");
8793
sourceCode.append("public class HelloClass {\n");
8894
sourceCode.append(" public java.util.List<String> hello() { return new java.util.ArrayList(); }");
8995
sourceCode.append("}");
90-
Class<?> helloClass = InMemoryJavaCompiler.newInstance().useIgnoreWarnings().compile("org.mdkt.HelloClass", sourceCode.toString());
91-
List<?> res = (List) helloClass.getMethod("hello").invoke(helloClass.newInstance());
96+
Class<?> helloClass = InMemoryJavaCompiler.newInstance().ignoreWarnings().compile("org.mdkt.HelloClass", sourceCode.toString());
97+
List<?> res = (List<?>) helloClass.getMethod("hello").invoke(helloClass.newInstance());
9298
Assert.assertEquals(0, res.size());
9399
}
100+
101+
@Test
102+
public void compile_WhenWarningsAndErrors() throws Exception {
103+
thrown.expect(CompilationException.class);
104+
StringBuffer sourceCode = new StringBuffer();
105+
106+
sourceCode.append("package org.mdkt;\n");
107+
sourceCode.append("public class HelloClass extends xxx {\n");
108+
sourceCode.append(" public java.util.List<String> hello() { return new java.util.ArrayList(); }");
109+
sourceCode.append("}");
110+
try {
111+
InMemoryJavaCompiler.newInstance().compile("org.mdkt.HelloClass", sourceCode.toString());
112+
} catch (Exception e) {
113+
logger.info("Exception caught: {}", e);
114+
throw e;
115+
}
116+
}
94117
}

0 commit comments

Comments
 (0)