Skip to content

Commit

Permalink
merged with turpid-monkey/master with some minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
trung committed Sep 27, 2017
1 parent ffca555 commit fe47f01
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 39 deletions.
21 changes: 18 additions & 3 deletions pom.xml
Expand Up @@ -18,8 +18,12 @@
<nexus-staging-maven-plugin.version>1.6.3</nexus-staging-maven-plugin.version>
<maven-release-plugin.version>2.5.1</maven-release-plugin.version>
<gpg.executable>gpg2</gpg.executable>

<!-- dependencies -->
<slf4j.version>1.7.5</slf4j.version>
<junit.version>4.12</junit.version>
</properties>

<licenses>
<license>
<name>Apache License, Version 2.0</name>
Expand All @@ -38,7 +42,7 @@
<connection>scm:git:git://github.com/trung/InMemoryJavaCompiler</connection>
<developerConnection>scm:git:git@github.com:trung/InMemoryJavaCompiler.git</developerConnection>
<tag>HEAD</tag>
</scm>
</scm>

<distributionManagement>
<snapshotRepository>
Expand All @@ -61,10 +65,21 @@
</developers>

<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>${slf4j.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
19 changes: 2 additions & 17 deletions 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<Diagnostic<? extends JavaFileObject>> diags) {
super(buildMessage(diags));
public CompilationException(String msg) {
super(msg);
}

private static String buildMessage(List<Diagnostic<? extends JavaFileObject>> 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();
}
}
46 changes: 31 additions & 15 deletions src/main/java/org/mdkt/compiler/InMemoryJavaCompiler.java
Expand Up @@ -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;
}
Expand All @@ -65,7 +66,7 @@ public InMemoryJavaCompiler useIgnoreWarnings() {
*/
public Map<String, Class<?>> compileAll() throws Exception {
if (sourceCodes.size() == 0) {
throw new Exception("No source code to compile");
throw new CompilationException("No source code to compile");
}
Collection<SourceCode> compilationUnits = sourceCodes.values();
CompiledCode[] code;
Expand All @@ -80,15 +81,30 @@ public Map<String, Class<?>> 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<String, Class<?>> classes = new HashMap<String, Class<?>>();
Expand Down
31 changes: 27 additions & 4 deletions src/test/java/org/mdkt/compiler/InMemoryJavaCompilerTest.java
Expand Up @@ -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();

Expand Down Expand Up @@ -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();

Expand All @@ -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<String> 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<String> 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;
}
}
}

0 comments on commit fe47f01

Please sign in to comment.