diff --git a/plexus-compilers/plexus-compiler-javac/src/main/java/org/codehaus/plexus/compiler/javac/JavaxToolsCompiler.java b/plexus-compilers/plexus-compiler-javac/src/main/java/org/codehaus/plexus/compiler/javac/JavaxToolsCompiler.java index 85b048bc..1f16661c 100644 --- a/plexus-compilers/plexus-compiler-javac/src/main/java/org/codehaus/plexus/compiler/javac/JavaxToolsCompiler.java +++ b/plexus-compilers/plexus-compiler-javac/src/main/java/org/codehaus/plexus/compiler/javac/JavaxToolsCompiler.java @@ -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 collector = new DiagnosticCollector(); - final StandardJavaFileManager standardFileManager = - compiler.getStandardFileManager( collector, null, sourceCharset ); + try ( final StandardJavaFileManager standardFileManager = + compiler.getStandardFileManager( collector, null, sourceCharset ) ) + { - final Iterable fileObjects = - standardFileManager.getJavaFileObjectsFromStrings( Arrays.asList( sourceFiles ) ); + final Iterable fileObjects = + standardFileManager.getJavaFileObjectsFromStrings( Arrays.asList( sourceFiles ) ); - /*(Writer out, - JavaFileManager fileManager, - DiagnosticListener diagnosticListener, - Iterable options, - Iterable classes, - Iterable compilationUnits)*/ + /*(Writer out, + JavaFileManager fileManager, + DiagnosticListener diagnosticListener, + Iterable options, + Iterable classes, + Iterable compilationUnits)*/ - List arguments = Arrays.asList( args ); + List arguments = Arrays.asList( args ); - final JavaCompiler.CompilationTask task = - compiler.getTask( null, standardFileManager, collector, arguments, null, fileObjects ); - final Boolean result = task.call(); - final ArrayList compilerMsgs = new ArrayList(); - for ( Diagnostic 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 compilerMsgs = new ArrayList(); + for ( Diagnostic 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 ) {