Skip to content

Commit 23ee074

Browse files
dfelliottDavid Elliott
andauthored
Close StandardJavaFileManager (#98)
* Use try-with-resources to close StandardJavaFileManager Co-authored-by: David Elliott <dfe@apple.com>
1 parent 04f61a8 commit 23ee074

File tree

1 file changed

+51
-49
lines changed

1 file changed

+51
-49
lines changed

plexus-compilers/plexus-compiler-javac/src/main/java/org/codehaus/plexus/compiler/javac/JavaxToolsCompiler.java

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -114,67 +114,69 @@ public CompilerResult compileInProcess( String[] args, final CompilerConfigurati
114114
final String sourceEncoding = config.getSourceEncoding();
115115
final Charset sourceCharset = sourceEncoding == null ? null : Charset.forName( sourceEncoding );
116116
final DiagnosticCollector<JavaFileObject> collector = new DiagnosticCollector<JavaFileObject>();
117-
final StandardJavaFileManager standardFileManager =
118-
compiler.getStandardFileManager( collector, null, sourceCharset );
119-
120-
final Iterable<? extends JavaFileObject> fileObjects =
121-
standardFileManager.getJavaFileObjectsFromStrings( Arrays.asList( sourceFiles ) );
122-
123-
/*(Writer out,
124-
JavaFileManager fileManager,
125-
DiagnosticListener<? super JavaFileObject> diagnosticListener,
126-
Iterable<String> options,
127-
Iterable<String> classes,
128-
Iterable<? extends JavaFileObject> compilationUnits)*/
129-
130-
List<String> arguments = Arrays.asList( args );
131-
132-
final JavaCompiler.CompilationTask task =
133-
compiler.getTask( null, standardFileManager, collector, arguments, null, fileObjects );
134-
final Boolean result = task.call();
135-
final ArrayList<CompilerMessage> compilerMsgs = new ArrayList<CompilerMessage>();
136-
for ( Diagnostic<? extends JavaFileObject> diagnostic : collector.getDiagnostics() )
117+
try ( final StandardJavaFileManager standardFileManager =
118+
compiler.getStandardFileManager( collector, null, sourceCharset ) )
137119
{
138-
CompilerMessage.Kind kind = convertKind(diagnostic);
139-
String baseMessage = diagnostic.getMessage( null );
140-
if ( baseMessage == null )
141-
{
142-
continue;
143-
}
144-
JavaFileObject source = diagnostic.getSource();
145-
String longFileName = source == null ? null : source.toUri().getPath();
146-
String shortFileName = source == null ? null : source.getName();
147-
String formattedMessage = baseMessage;
148-
int lineNumber = Math.max( 0, (int) diagnostic.getLineNumber() );
149-
int columnNumber = Math.max( 0, (int) diagnostic.getColumnNumber() );
150-
if ( source != null && lineNumber > 0 )
120+
121+
final Iterable<? extends JavaFileObject> fileObjects =
122+
standardFileManager.getJavaFileObjectsFromStrings( Arrays.asList( sourceFiles ) );
123+
124+
/*(Writer out,
125+
JavaFileManager fileManager,
126+
DiagnosticListener<? super JavaFileObject> diagnosticListener,
127+
Iterable<String> options,
128+
Iterable<String> classes,
129+
Iterable<? extends JavaFileObject> compilationUnits)*/
130+
131+
List<String> arguments = Arrays.asList( args );
132+
133+
final JavaCompiler.CompilationTask task =
134+
compiler.getTask( null, standardFileManager, collector, arguments, null, fileObjects );
135+
final Boolean result = task.call();
136+
final ArrayList<CompilerMessage> compilerMsgs = new ArrayList<CompilerMessage>();
137+
for ( Diagnostic<? extends JavaFileObject> diagnostic : collector.getDiagnostics() )
151138
{
152-
// Some compilers like to copy the file name into the message, which makes it appear twice.
153-
String possibleTrimming = longFileName + ":" + lineNumber + ": ";
154-
if ( formattedMessage.startsWith( possibleTrimming ) )
139+
CompilerMessage.Kind kind = convertKind(diagnostic);
140+
String baseMessage = diagnostic.getMessage( null );
141+
if ( baseMessage == null )
155142
{
156-
formattedMessage = formattedMessage.substring( possibleTrimming.length() );
143+
continue;
157144
}
158-
else
145+
JavaFileObject source = diagnostic.getSource();
146+
String longFileName = source == null ? null : source.toUri().getPath();
147+
String shortFileName = source == null ? null : source.getName();
148+
String formattedMessage = baseMessage;
149+
int lineNumber = Math.max( 0, (int) diagnostic.getLineNumber() );
150+
int columnNumber = Math.max( 0, (int) diagnostic.getColumnNumber() );
151+
if ( source != null && lineNumber > 0 )
159152
{
160-
possibleTrimming = shortFileName + ":" + lineNumber + ": ";
153+
// Some compilers like to copy the file name into the message, which makes it appear twice.
154+
String possibleTrimming = longFileName + ":" + lineNumber + ": ";
161155
if ( formattedMessage.startsWith( possibleTrimming ) )
162156
{
163157
formattedMessage = formattedMessage.substring( possibleTrimming.length() );
164158
}
159+
else
160+
{
161+
possibleTrimming = shortFileName + ":" + lineNumber + ": ";
162+
if ( formattedMessage.startsWith( possibleTrimming ) )
163+
{
164+
formattedMessage = formattedMessage.substring( possibleTrimming.length() );
165+
}
166+
}
165167
}
168+
compilerMsgs.add(
169+
new CompilerMessage( longFileName, kind, lineNumber, columnNumber, lineNumber, columnNumber,
170+
formattedMessage ) );
171+
}
172+
if ( result != Boolean.TRUE && compilerMsgs.isEmpty() )
173+
{
174+
compilerMsgs.add(
175+
new CompilerMessage( "An unknown compilation problem occurred", CompilerMessage.Kind.ERROR ) );
166176
}
167-
compilerMsgs.add(
168-
new CompilerMessage( longFileName, kind, lineNumber, columnNumber, lineNumber, columnNumber,
169-
formattedMessage ) );
170-
}
171-
if ( result != Boolean.TRUE && compilerMsgs.isEmpty() )
172-
{
173-
compilerMsgs.add(
174-
new CompilerMessage( "An unknown compilation problem occurred", CompilerMessage.Kind.ERROR ) );
175-
}
176177

177-
return new CompilerResult( result, compilerMsgs );
178+
return new CompilerResult( result, compilerMsgs );
179+
}
178180
}
179181
catch ( Exception e )
180182
{

0 commit comments

Comments
 (0)