You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2019/01/27 09:06:04 UTC

[groovy] branch master updated: Close file manager when compilation is completed.

This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new 0e94796  Close file manager when compilation is completed.
0e94796 is described below

commit 0e9479610dacf0d96a66f0a47fc30e056f4b80d0
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sun Jan 27 17:04:49 2019 +0800

    Close file manager when compilation is completed.
---
 .../groovy/tools/javac/JavacJavaCompiler.java      | 50 ++++++++++++----------
 1 file changed, 27 insertions(+), 23 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/tools/javac/JavacJavaCompiler.java b/src/main/java/org/codehaus/groovy/tools/javac/JavacJavaCompiler.java
index c8a64b0..631a1cd 100644
--- a/src/main/java/org/codehaus/groovy/tools/javac/JavacJavaCompiler.java
+++ b/src/main/java/org/codehaus/groovy/tools/javac/JavacJavaCompiler.java
@@ -31,6 +31,7 @@ import javax.tools.JavaFileObject;
 import javax.tools.StandardJavaFileManager;
 import javax.tools.ToolProvider;
 import java.io.File;
+import java.io.IOException;
 import java.io.PrintWriter;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
@@ -83,29 +84,29 @@ public class JavacJavaCompiler implements JavaCompiler {
         return javacReturnValue;
     }
 
-    private boolean doCompileWithSystemJavaCompiler(CompilationUnit cu, List<String> files, String[] javacParameters, StringBuilderWriter javacOutput) {
+    private boolean doCompileWithSystemJavaCompiler(CompilationUnit cu, List<String> files, String[] javacParameters, StringBuilderWriter javacOutput) throws IOException {
         javax.tools.JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
-        StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, charset);
-
-        final Set<JavaFileObject> compilationUnitSet = cu.getJavaCompilationUnitSet(); // java stubs already added
-
-        // add java source files to compile
-        fileManager.getJavaFileObjectsFromFiles(
-                files.stream()
-                        .map(File::new)
-                        .collect(Collectors.toList())
-        ).forEach(compilationUnitSet::add);
-
-        javax.tools.JavaCompiler.CompilationTask compilationTask = compiler.getTask(
-                javacOutput,
-                fileManager,
-                null,
-                Arrays.asList(javacParameters),
-                Collections.emptyList(),
-                compilationUnitSet
-        );
-
-        return compilationTask.call();
+        try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, charset)) {
+            final Set<JavaFileObject> compilationUnitSet = cu.getJavaCompilationUnitSet(); // java stubs already added
+
+            // add java source files to compile
+            fileManager.getJavaFileObjectsFromFiles(
+                    files.stream()
+                            .map(File::new)
+                            .collect(Collectors.toList())
+            ).forEach(compilationUnitSet::add);
+
+            javax.tools.JavaCompiler.CompilationTask compilationTask = compiler.getTask(
+                    javacOutput,
+                    fileManager,
+                    null,
+                    Arrays.asList(javacParameters),
+                    Collections.emptyList(),
+                    compilationUnitSet
+            );
+
+            return compilationTask.call();
+        }
     }
 
     public void compile(List<String> files, CompilationUnit cu) {
@@ -114,7 +115,7 @@ public class JavacJavaCompiler implements JavaCompiler {
 
     private void compile(List<String> files, CompilationUnit cu, boolean toCompileStubInMem) {
         String[] javacParameters = makeParameters(files, cu.getClassLoader(), toCompileStubInMem);
-        StringBuilderWriter javacOutput = new StringBuilderWriter();;
+        StringBuilderWriter javacOutput = new StringBuilderWriter();
         int javacReturnValue = 0;
         try {
             if (toCompileStubInMem) {
@@ -126,6 +127,9 @@ public class JavacJavaCompiler implements JavaCompiler {
                 } catch (IllegalArgumentException e) {
                     javacReturnValue = 2; // any of the options are invalid
                     cu.getErrorCollector().addFatalError(new ExceptionMessage(e, true, cu));
+                } catch (IOException e) {
+                    javacReturnValue = 1;
+                    cu.getErrorCollector().addFatalError(new ExceptionMessage(e, true, cu));
                 }
             } else {
                 try {