8147414: java.nio.file.ClosedFileSystemException in javadoc
authorjjg
Thu, 19 Jan 2017 19:14:51 -0800
changeset 43268 12436ebea906
parent 43267 81e70bde1b96
child 43269 12f989542165
child 43277 edf2836a33f7
8147414: java.nio.file.ClosedFileSystemException in javadoc Reviewed-by: vromero
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassReader.java
langtools/test/tools/javac/classreader/FileSystemClosedTest.java
langtools/test/tools/javadoc/8147801/T8147801.java
langtools/test/tools/javadoc/8147801/jarsrc/lib/Lib2.java
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassReader.java	Thu Jan 19 15:12:59 2017 -0800
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassReader.java	Thu Jan 19 19:14:51 2017 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2017, 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
@@ -29,6 +29,7 @@
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.nio.CharBuffer;
+import java.nio.file.ClosedFileSystemException;
 import java.util.Arrays;
 import java.util.EnumSet;
 import java.util.HashMap;
@@ -2756,8 +2757,8 @@
                     currentModule.provides = List.nil();
                 }
             }
-        } catch (IOException ex) {
-            throw badClassFile("unable.to.access.file", ex.getMessage());
+        } catch (IOException | ClosedFileSystemException ex) {
+            throw badClassFile("unable.to.access.file", ex.toString());
         } catch (ArrayIndexOutOfBoundsException ex) {
             throw badClassFile("bad.class.file", c.flatname);
         } finally {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/classreader/FileSystemClosedTest.java	Thu Jan 19 19:14:51 2017 -0800
@@ -0,0 +1,124 @@
+/*
+ * Copyright (c) 2016, 2017, 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8147414
+ * @summary java.nio.file.ClosedFileSystemException in javadoc
+ * @library /tools/lib
+ * @modules jdk.compiler/com.sun.tools.javac.api
+ *          jdk.compiler/com.sun.tools.javac.main
+ * @build toolbox.JarTask toolbox.JavacTask toolbox.ToolBox
+ * @run main FileSystemClosedTest
+ */
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.nio.file.ClosedFileSystemException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Arrays;
+import java.util.List;
+
+import javax.lang.model.element.PackageElement;
+import javax.lang.model.element.TypeElement;
+import javax.lang.model.util.Elements;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileObject;
+import javax.tools.StandardJavaFileManager;
+import javax.tools.ToolProvider;
+
+import toolbox.ToolBox;
+import toolbox.JarTask;
+import toolbox.JavacTask;
+
+public class FileSystemClosedTest {
+    public static void main(String... args) throws Exception {
+        new FileSystemClosedTest().run();
+    }
+
+    void run() throws Exception {
+        ToolBox tb = new ToolBox();
+        Path jar = createJar(tb);
+
+        Path src = Paths.get("src");
+        tb.writeJavaFiles(src, "class C { p1.C1 c1; }");
+
+        JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
+        PrintWriter out = new PrintWriter(System.err, true);
+        StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
+        List<String> options = Arrays.asList("-classpath", jar.toString());
+        Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(src.resolve("C.java"));
+        com.sun.source.util.JavacTask task =
+                (com.sun.source.util.JavacTask) comp.getTask(out, fm, null, options, null, files);
+        task.parse();
+
+        Elements elems = task.getElements();
+
+        try {
+            // Use  p1, p1.C1 as a control to verify normal behavior
+            PackageElement p1 = elems.getPackageElement("p1");
+            TypeElement p1C1 = elems.getTypeElement("p1.C1");
+            System.err.println("p1: " + p1 + ";  p1C1: " + p1C1);
+            if (p1C1 == null) {
+                throw new Exception("p1.C1 not found");
+            }
+
+            // Now repeat for p2, p2.C2, closing the file manager early
+            PackageElement p2 = elems.getPackageElement("p2");
+            System.err.println("closing file manager");
+            fm.close();
+            TypeElement p2C2 = elems.getTypeElement("p2.C2");
+            System.err.println("p2: " + p2 + ";  p2C2: " + p2C2);
+            if (p2C2 != null) {
+                throw new Exception("p1.C1 found unexpectedly");
+            }
+        } catch (ClosedFileSystemException e) {
+            throw new Exception("unexpected exception thrown", e);
+        }
+
+    }
+
+    private Path createJar(ToolBox tb) throws IOException {
+        Path jarSrc = Paths.get("jarSrc");
+        Path jarClasses = Paths.get("jarClasses");
+        Path jar = Paths.get("jar.jar");
+        Files.createDirectories(jarClasses);
+
+        tb.writeJavaFiles(jarSrc,
+                "package p1; public class C1 { }",
+                "package p2; public class C2 { }");
+
+        new JavacTask(tb)
+                .outdir(jarClasses)
+                .files(tb.findJavaFiles(jarSrc))
+                .run()
+                .writeAll();
+        new JarTask(tb)
+                .run("cf", jar.toString(), "-C", jarClasses.toString(), "p1", "p2");
+
+        return jar;
+    }
+}
+
--- a/langtools/test/tools/javadoc/8147801/T8147801.java	Thu Jan 19 15:12:59 2017 -0800
+++ b/langtools/test/tools/javadoc/8147801/T8147801.java	Thu Jan 19 19:14:51 2017 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2017, 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
@@ -32,6 +32,8 @@
  */
 
 import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.StringWriter;
 import java.nio.file.ClosedFileSystemException;
 import java.nio.file.Files;
 import java.nio.file.Path;
@@ -80,19 +82,22 @@
     void test(boolean withOption) {
         System.err.println("Testing " + (withOption ? "with" : "without") + " option");
         try {
+            String dump = "";
             RootDoc root = getRootDoc(withOption);
             for (ClassDoc cd: root.specifiedClasses()) {
-                dump("", cd);
+                dump += dump(cd);
             }
-            if (!withOption) {
-                error("expected option did not occur");
+            if (dump.contains("lib.Lib2.i")) {
+                if (!withOption) {
+                    error("control case failed: Lib2 class file was read, unexpectedly, without using option");
+                }
+            } else {
+                if (withOption) {
+                    error("test case failed: could not read Lib2 class file, using option");
+                }
             }
         } catch (ClosedFileSystemException e) {
-            if (withOption) {
-                error("Unexpected exception: " + e);
-            } else {
-                System.err.println("Exception received as expected: " + e);
-            }
+            error("Unexpected exception: " + e);
         }
         System.err.println();
     }
@@ -118,12 +123,21 @@
         return cachedRoot;
     }
 
-    void dump(String prefix, ClassDoc cd) {
-        System.err.println(prefix + "class: " + cd);
+    String dump(ClassDoc cd) {
+        StringWriter sw = new StringWriter();
+        PrintWriter pw = new PrintWriter(sw);
+        dump(pw, "", cd);
+        String out = sw.toString();
+        System.err.println(out);
+        return out;
+    }
+
+    void dump(PrintWriter out, String prefix, ClassDoc cd) {
+        out.println(prefix + "class: " + cd);
         for (FieldDoc fd: cd.fields()) {
-            System.err.println(fd);
+            out.println(prefix + "  " + fd);
             if (fd.type().asClassDoc() != null) {
-                dump(prefix + "  ", fd.type().asClassDoc());
+                dump(out, prefix + "    ", fd.type().asClassDoc());
             }
         }
     }
--- a/langtools/test/tools/javadoc/8147801/jarsrc/lib/Lib2.java	Thu Jan 19 15:12:59 2017 -0800
+++ b/langtools/test/tools/javadoc/8147801/jarsrc/lib/Lib2.java	Thu Jan 19 19:14:51 2017 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2017, 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
@@ -24,5 +24,5 @@
 package lib;
 
 public class Lib2 {
-    int i;
+    public int i;
 }