Skip to content

Commit 85dd50d

Browse files
committed
Added support for compiler options
Added ignore compiler warning
1 parent 0543084 commit 85dd50d

1 file changed

Lines changed: 36 additions & 12 deletions

File tree

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

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
package org.mdkt.compiler;
22

3-
import java.util.Collection;
4-
import java.util.HashMap;
5-
import java.util.Iterator;
6-
import java.util.Map;
3+
import java.util.*;
74

8-
import javax.tools.DiagnosticCollector;
9-
import javax.tools.JavaCompiler;
10-
import javax.tools.JavaFileObject;
11-
import javax.tools.ToolProvider;
5+
import javax.tools.*;
126

137
/**
14-
* Complile Java sources in-memory
8+
* Compile Java sources in-memory
159
*/
1610
public class InMemoryJavaCompiler {
1711
private JavaCompiler javac;
1812
private DynamicClassLoader classLoader;
13+
private Iterable<String> options;
14+
boolean ignoreWarnings = false;
1915

2016
private Map<String, SourceCode> sourceCodes = new HashMap<String, SourceCode>();
2117

@@ -33,10 +29,30 @@ public InMemoryJavaCompiler useParentClassLoader(ClassLoader parent) {
3329
return this;
3430
}
3531

32+
/**
33+
* Options used by the compiler, e.g. '-Xlint:unchecked'.
34+
* @param options
35+
* @return
36+
*/
37+
public InMemoryJavaCompiler useOptions(String... options)
38+
{
39+
this.options = Arrays.asList(options);
40+
return this;
41+
}
42+
43+
/**
44+
* Ignore non-critical compiler output, like unchecked/unsafe operation warnings.
45+
* @return
46+
*/
47+
public InMemoryJavaCompiler useIgnoreWarnings() {
48+
ignoreWarnings = true;
49+
return this;
50+
}
51+
3652
/**
3753
* Compile all sources
3854
*
39-
* @return
55+
* @return Map containing instances of all compiled classes
4056
* @throws Exception
4157
*/
4258
public Map<String, Class<?>> compileAll() throws Exception {
@@ -53,10 +69,18 @@ public Map<String, Class<?>> compileAll() throws Exception {
5369
}
5470
DiagnosticCollector<JavaFileObject> collector = new DiagnosticCollector<>();
5571
ExtendedStandardJavaFileManager fileManager = new ExtendedStandardJavaFileManager(javac.getStandardFileManager(null, null, null), classLoader);
56-
JavaCompiler.CompilationTask task = javac.getTask(null, fileManager, collector, null, null, compilationUnits);
72+
JavaCompiler.CompilationTask task = javac.getTask(null, fileManager, collector, options, null, compilationUnits);
5773
boolean result = task.call();
5874
if (!result || collector.getDiagnostics().size() > 0) {
59-
throw new CompilationException(collector.getDiagnostics());
75+
for (Diagnostic<? extends JavaFileObject> d : collector.getDiagnostics()) {
76+
if (ignoreWarnings &&
77+
(d.getKind() == Diagnostic.Kind.NOTE || d.getKind() == Diagnostic.Kind.MANDATORY_WARNING || d.getKind() == Diagnostic.Kind.WARNING))
78+
continue;
79+
else {
80+
throw new CompilationException(collector.getDiagnostics());
81+
}
82+
}
83+
6084
}
6185

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

0 commit comments

Comments
 (0)