{
CompilerMessage message =
new CompilerMessage( "No compiler is provided in this environment. "
+ "Perhaps you are running on a JRE rather than a JDK?",
CompilerMessage.Kind.ERROR );
return new CompilerResult( false, Collections.singletonList( message ) );
}
final String sourceEncoding = config.getSourceEncoding();
final Charset sourceCharset = sourceEncoding == null ? null : Charset.forName( sourceEncoding );
final DiagnosticCollector<JavaFileObject> collector = new DiagnosticCollector<JavaFileObject>();
final StandardJavaFileManager standardFileManager =
compiler.getStandardFileManager( collector, null, sourceCharset );
final Iterable<? extends JavaFileObject> fileObjects =
standardFileManager.getJavaFileObjectsFromStrings( Arrays.asList( sourceFiles ) );
final JavaCompiler.CompilationTask task =
/*(Writer out,
JavaFileManager fileManager,
DiagnosticListener<? super JavaFileObject> diagnosticListener,
Iterable<String> options,
Iterable<String> classes,
Iterable<? extends JavaFileObject> compilationUnits)*/
compiler.getTask( null, standardFileManager, collector, Arrays.asList( args ), null, fileObjects );
final Boolean result = task.call();
final ArrayList<CompilerMessage> compilerMsgs = new ArrayList<CompilerMessage>();
for ( Diagnostic<? extends JavaFileObject> diagnostic : collector.getDiagnostics() )
{
CompilerMessage.Kind kind;
switch ( diagnostic.getKind() )
{
case ERROR:
kind = CompilerMessage.Kind.ERROR;
break;
case WARNING:
kind = CompilerMessage.Kind.WARNING;
break;
case MANDATORY_WARNING:
kind = CompilerMessage.Kind.MANDATORY_WARNING;
break;
case NOTE:
kind = CompilerMessage.Kind.NOTE;
break;
default:
kind = CompilerMessage.Kind.OTHER;
break;
}
String baseMessage = diagnostic.getMessage( null );
if ( baseMessage == null )
{
continue;
}
JavaFileObject source = diagnostic.getSource();
String longFileName = source == null ? null : source.toUri().getPath();
String shortFileName = source == null ? null : source.getName();
String formattedMessage = baseMessage;
int lineNumber = Math.max( 0, (int) diagnostic.getLineNumber() );
int columnNumber = Math.max( 0, (int) diagnostic.getColumnNumber() );
if ( source != null && lineNumber > 0 )
{
// Some compilers like to copy the file name into the message, which makes it appear twice.
String possibleTrimming = longFileName + ":" + lineNumber + ": ";
if ( formattedMessage.startsWith( possibleTrimming ) )
{
formattedMessage = formattedMessage.substring( possibleTrimming.length() );
}
else
{
possibleTrimming = shortFileName + ":" + lineNumber + ": ";
if ( formattedMessage.startsWith( possibleTrimming ) )
{
formattedMessage = formattedMessage.substring( possibleTrimming.length() );
}
}
}
compilerMsgs.add(
new CompilerMessage( longFileName, kind, lineNumber, columnNumber, lineNumber, columnNumber,
formattedMessage ) );
}
if ( result != Boolean.TRUE && compilerMsgs.isEmpty() )
{
compilerMsgs.add(
new CompilerMessage( "An unknown compilation problem occurred", CompilerMessage.Kind.ERROR ) );
}
return new CompilerResult( result, compilerMsgs);
}
catch ( Exception e )
{
throw new CompilerException( e.getMessage(), e );
}