8062579: JavacTask, DocumentationTask impls should close file manager when possible
authorjjg
Thu, 30 Oct 2014 16:08:43 -0700
changeset 27379 5ae894733e09
parent 27325 948cceef81ba
child 27380 2db080e053df
8062579: JavacTask, DocumentationTask impls should close file manager when possible Reviewed-by: ksrini
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTool.java
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/file/JavacFileManager.java
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/util/BaseFileManager.java
langtools/src/jdk.javadoc/share/classes/com/sun/tools/javadoc/Start.java
langtools/src/jdk.javadoc/share/classes/com/sun/tools/javadoc/api/JavadocTool.java
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java	Wed Jul 05 20:06:12 2017 +0200
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java	Thu Oct 30 16:08:43 2014 -0700
@@ -25,6 +25,7 @@
 
 package com.sun.tools.javac.api;
 
+import java.io.IOException;
 import java.nio.CharBuffer;
 import java.util.*;
 import java.util.concurrent.Callable;
@@ -67,6 +68,7 @@
 public class JavacTaskImpl extends BasicJavacTask {
     private final Arguments args;
     private JavaCompiler compiler;
+    private JavaFileManager fileManager;
     private Locale locale;
     private Map<JavaFileObject, JCCompilationUnit> notYetEntered;
     private ListBuffer<Env<AttrContext>> genList;
@@ -76,6 +78,7 @@
     JavacTaskImpl(Context context) {
         super(context, true);
         args = Arguments.instance(context);
+        fileManager = context.get(JavaFileManager.class);
     }
 
     @Override @DefinedBy(Api.COMPILER)
@@ -202,6 +205,12 @@
     void cleanup() {
         if (compiler != null)
             compiler.close();
+        if (fileManager instanceof BaseFileManager && ((BaseFileManager) fileManager).autoClose) {
+            try {
+                fileManager.close();
+            } catch (IOException ignore) {
+            }
+        }
         compiler = null;
         context = null;
         notYetEntered = null;
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTool.java	Wed Jul 05 20:06:12 2017 +0200
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTool.java	Thu Oct 30 16:08:43 2014 -0700
@@ -43,6 +43,7 @@
 import com.sun.tools.javac.file.JavacFileManager;
 import com.sun.tools.javac.main.Arguments;
 import com.sun.tools.javac.main.Option;
+import com.sun.tools.javac.util.BaseFileManager;
 import com.sun.tools.javac.util.ClientCodeException;
 import com.sun.tools.javac.util.Context;
 import com.sun.tools.javac.util.DefinedBy;
@@ -151,8 +152,12 @@
             else
                 context.put(Log.outKey, new PrintWriter(out, true));
 
-            if (fileManager == null)
+            if (fileManager == null) {
                 fileManager = getStandardFileManager(diagnosticListener, null, null);
+                if (fileManager instanceof BaseFileManager) {
+                    ((BaseFileManager) fileManager).autoClose = true;
+                }
+            }
             fileManager = ccw.wrap(fileManager);
 
             context.put(JavaFileManager.class, fileManager);
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/file/JavacFileManager.java	Wed Jul 05 20:06:12 2017 +0200
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/file/JavacFileManager.java	Thu Oct 30 16:08:43 2014 -0700
@@ -132,6 +132,8 @@
         setContext(context);
         if (System.getProperty("show.fm.open.close") != null)
             System.err.println("JavacFileManager.open " + this.hashCode());
+        if (System.getProperty("trace.fm.open.close") != null)
+            Thread.dumpStack();
     }
 
     /**
@@ -575,6 +577,8 @@
     public void close() {
         if (System.getProperty("show.fm.open.close") != null)
             System.err.println("JavacFileManager.close " + this.hashCode());
+        if (System.getProperty("trace.fm.open.close") != null)
+            Thread.dumpStack();
         for (Iterator<Archive> i = archives.values().iterator(); i.hasNext(); ) {
             Archive a = i.next();
             i.remove();
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/util/BaseFileManager.java	Wed Jul 05 20:06:12 2017 +0200
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/util/BaseFileManager.java	Thu Oct 30 16:08:43 2014 -0700
@@ -104,6 +104,12 @@
 
     protected Locations locations;
 
+    /**
+     * A flag for clients to use to indicate that this file manager should
+     * be closed when it is no longer required.
+     */
+    public boolean autoClose;
+
     protected Source getSource() {
         String sourceName = options.get(Option.SOURCE);
         Source source = null;
--- a/langtools/src/jdk.javadoc/share/classes/com/sun/tools/javadoc/Start.java	Wed Jul 05 20:06:12 2017 +0200
+++ b/langtools/src/jdk.javadoc/share/classes/com/sun/tools/javadoc/Start.java	Thu Oct 30 16:08:43 2014 -0700
@@ -89,7 +89,6 @@
     private boolean apiMode;
 
     private JavaFileManager fileManager;
-    private boolean closeFileManagerOnExit;
 
     Start(String programName,
           PrintWriter errWriter,
@@ -242,7 +241,9 @@
             messager.error(Messager.NOPOS, "main.fatal.exception");
             failed = true;
         } finally {
-            if (fileManager != null && closeFileManagerOnExit) {
+            if (fileManager != null
+                    && fileManager instanceof BaseFileManager
+                    && ((BaseFileManager) fileManager).autoClose) {
                 try {
                     fileManager.close();
                 } catch (IOException ignore) {
@@ -343,7 +344,9 @@
         if (fileManager == null) {
             JavacFileManager.preRegister(context);
             fileManager = context.get(JavaFileManager.class);
-            closeFileManagerOnExit = true;
+            if (fileManager instanceof BaseFileManager) {
+                ((BaseFileManager) fileManager).autoClose = true;
+            }
         }
         if (fileManager instanceof BaseFileManager) {
             ((BaseFileManager) fileManager).handleOptions(fileManagerOpts);
--- a/langtools/src/jdk.javadoc/share/classes/com/sun/tools/javadoc/api/JavadocTool.java	Wed Jul 05 20:06:12 2017 +0200
+++ b/langtools/src/jdk.javadoc/share/classes/com/sun/tools/javadoc/api/JavadocTool.java	Thu Oct 30 16:08:43 2014 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -45,6 +45,7 @@
 
 import com.sun.tools.javac.api.ClientCodeWrapper;
 import com.sun.tools.javac.file.JavacFileManager;
+import com.sun.tools.javac.util.BaseFileManager;
 import com.sun.tools.javac.util.ClientCodeException;
 import com.sun.tools.javac.util.Context;
 import com.sun.tools.javac.util.DefinedBy;
@@ -111,8 +112,12 @@
             else
                 context.put(Log.outKey, new PrintWriter(out, true));
 
-            if (fileManager == null)
+            if (fileManager == null) {
                 fileManager = getStandardFileManager(diagnosticListener, null, null);
+                if (fileManager instanceof BaseFileManager) {
+                    ((BaseFileManager) fileManager).autoClose = true;
+                }
+            }
             fileManager = ccw.wrap(fileManager);
             context.put(JavaFileManager.class, fileManager);