@@ -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 <?>>();
0 commit comments