Skip to content

Close StandardJavaFileManager #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 23, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -106,67 +106,69 @@ static CompilerResult compileInProcess( String[] args, final CompilerConfigurati
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 );
try ( final StandardJavaFileManager standardFileManager =
compiler.getStandardFileManager( collector, null, sourceCharset ) )
{

final Iterable<? extends JavaFileObject> fileObjects =
standardFileManager.getJavaFileObjectsFromStrings( Arrays.asList( sourceFiles ) );
final Iterable<? extends JavaFileObject> fileObjects =
standardFileManager.getJavaFileObjectsFromStrings( Arrays.asList( sourceFiles ) );

/*(Writer out,
JavaFileManager fileManager,
DiagnosticListener<? super JavaFileObject> diagnosticListener,
Iterable<String> options,
Iterable<String> classes,
Iterable<? extends JavaFileObject> compilationUnits)*/
/*(Writer out,
JavaFileManager fileManager,
DiagnosticListener<? super JavaFileObject> diagnosticListener,
Iterable<String> options,
Iterable<String> classes,
Iterable<? extends JavaFileObject> compilationUnits)*/

List<String> arguments = Arrays.asList( args );
List<String> arguments = Arrays.asList( args );

final JavaCompiler.CompilationTask task =
compiler.getTask( null, standardFileManager, collector, arguments, null, fileObjects );
final Boolean result = task.call();
final ArrayList<CompilerMessage> compilerMsgs = new ArrayList<CompilerMessage>();
for ( Diagnostic<? extends JavaFileObject> diagnostic : collector.getDiagnostics() )
{
CompilerMessage.Kind kind = convertKind(diagnostic);
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 )
final JavaCompiler.CompilationTask task =
compiler.getTask( null, standardFileManager, collector, arguments, null, fileObjects );
final Boolean result = task.call();
final ArrayList<CompilerMessage> compilerMsgs = new ArrayList<CompilerMessage>();
for ( Diagnostic<? extends JavaFileObject> diagnostic : collector.getDiagnostics() )
{
// Some compilers like to copy the file name into the message, which makes it appear twice.
String possibleTrimming = longFileName + ":" + lineNumber + ": ";
if ( formattedMessage.startsWith( possibleTrimming ) )
CompilerMessage.Kind kind = convertKind(diagnostic);
String baseMessage = diagnostic.getMessage( null );
if ( baseMessage == null )
{
formattedMessage = formattedMessage.substring( possibleTrimming.length() );
continue;
}
else
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 )
{
possibleTrimming = shortFileName + ":" + lineNumber + ": ";
// 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 ) );
}
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 );
return new CompilerResult( result, compilerMsgs );
}
}
catch ( Exception e )
{
Expand Down