8220687: Add StandardJavaFileManager.getJavaFileObjectsFromPaths overload
authorcushon
Tue, 26 Mar 2019 15:00:02 -0700
changeset 54298 6207397a6603
parent 54297 f98f158c1db8
child 54299 656789f95658
8220687: Add StandardJavaFileManager.getJavaFileObjectsFromPaths overload Reviewed-by: jjg
src/java.compiler/share/classes/javax/tools/StandardJavaFileManager.java
src/jdk.compiler/share/classes/com/sun/tools/javac/api/ClientCodeWrapper.java
src/jdk.compiler/share/classes/com/sun/tools/javac/file/JavacFileManager.java
src/jdk.compiler/share/classes/com/sun/tools/javac/main/DelegatingJavaFileManager.java
test/langtools/tools/javac/api/file/SJFM_GetFileObjects.java
--- a/src/java.compiler/share/classes/javax/tools/StandardJavaFileManager.java	Tue Mar 26 11:34:40 2019 -0700
+++ b/src/java.compiler/share/classes/javax/tools/StandardJavaFileManager.java	Tue Mar 26 15:00:02 2019 -0700
@@ -28,9 +28,11 @@
 import java.io.File;
 import java.io.IOException;
 import java.nio.file.Path;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Iterator;
+import java.util.List;
 
 /**
  * File manager based on {@linkplain File java.io.File} and {@linkplain Path java.nio.file.Path}.
@@ -199,11 +201,40 @@
      * a directory or if this file manager does not support any of the
      * given paths.
      *
-     * @since 9
+     * @since 13
      */
     default Iterable<? extends JavaFileObject> getJavaFileObjectsFromPaths(
+            Collection<? extends Path> paths) {
+        return getJavaFileObjectsFromFiles(asFiles(paths));
+    }
+
+    /**
+     * Returns file objects representing the given paths.
+     *
+     * @implSpec
+     * The default implementation converts each path to a file and calls
+     * {@link #getJavaFileObjectsFromFiles getJavaObjectsFromFiles}.
+     * IllegalArgumentException will be thrown if any of the paths
+     * cannot be converted to a file.
+     *
+     * @param paths a list of paths
+     * @return a list of file objects
+     * @throws IllegalArgumentException if the list of paths includes
+     * a directory or if this file manager does not support any of the
+     * given paths.
+     *
+     * @since 9
+     * @deprecated use {@link #getJavaFileObjectsFromPaths(Collection)} instead,
+     * to prevent the possibility of accidentally calling the method with a
+     * single {@code Path} as such an argument. Although {@code Path} implements
+     * {@code Iterable<Path>}, it would almost never be correct to pass a single
+     * {@code Path} and have it be treated as an {@code Iterable} of its
+     * components.
+     */
+    @Deprecated(since = "13")
+    default Iterable<? extends JavaFileObject> getJavaFileObjectsFromPaths(
             Iterable<? extends Path> paths) {
-        return getJavaFileObjectsFromFiles(asFiles(paths));
+        return getJavaFileObjectsFromPaths(asCollection(paths));
     }
 
     /**
@@ -484,4 +515,13 @@
             }
         };
     }
+
+    private static <T> Collection<T> asCollection(Iterable<T> iterable) {
+        if (iterable instanceof Collection) {
+            return (Collection<T>) iterable;
+        }
+        List<T> result = new ArrayList<>();
+        for (T item : iterable) result.add(item);
+        return result;
+    }
 }
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/api/ClientCodeWrapper.java	Tue Mar 26 11:34:40 2019 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/api/ClientCodeWrapper.java	Tue Mar 26 15:00:02 2019 -0700
@@ -437,6 +437,18 @@
         }
 
         @Override @DefinedBy(Api.COMPILER)
+        public Iterable<? extends JavaFileObject> getJavaFileObjectsFromPaths(Collection<? extends Path> paths) {
+            try {
+                return ((StandardJavaFileManager)clientJavaFileManager).getJavaFileObjectsFromPaths(paths);
+            } catch (ClientCodeException e) {
+                throw e;
+            } catch (RuntimeException | Error e) {
+                throw new ClientCodeException(e);
+            }
+        }
+
+        @Deprecated(since = "13")
+        @Override @DefinedBy(Api.COMPILER)
         public Iterable<? extends JavaFileObject> getJavaFileObjectsFromPaths(Iterable<? extends Path> paths) {
             try {
                 return ((StandardJavaFileManager)clientJavaFileManager).getJavaFileObjectsFromPaths(paths);
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/file/JavacFileManager.java	Tue Mar 26 11:34:40 2019 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/file/JavacFileManager.java	Tue Mar 26 15:00:02 2019 -0700
@@ -897,7 +897,7 @@
 
     @Override @DefinedBy(Api.COMPILER)
     public Iterable<? extends JavaFileObject> getJavaFileObjectsFromPaths(
-        Iterable<? extends Path> paths)
+        Collection<? extends Path> paths)
     {
         ArrayList<PathFileObject> result;
         if (paths instanceof Collection<?>)
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/main/DelegatingJavaFileManager.java	Tue Mar 26 11:34:40 2019 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/main/DelegatingJavaFileManager.java	Tue Mar 26 15:00:02 2019 -0700
@@ -206,6 +206,13 @@
 
         @Override
         public Iterable<? extends JavaFileObject> getJavaFileObjectsFromPaths
+                                                  (Collection<? extends Path> paths) {
+            return baseSJFM.getJavaFileObjectsFromPaths(paths);
+        }
+
+        @Deprecated(since = "13")
+        @Override
+        public Iterable<? extends JavaFileObject> getJavaFileObjectsFromPaths
                                                   (Iterable<? extends Path> paths) {
             return baseSJFM.getJavaFileObjectsFromPaths(paths);
         }
--- a/test/langtools/tools/javac/api/file/SJFM_GetFileObjects.java	Tue Mar 26 11:34:40 2019 -0700
+++ b/test/langtools/tools/javac/api/file/SJFM_GetFileObjects.java	Tue Mar 26 15:00:02 2019 -0700
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 8059977
+ * @bug 8059977 8220687
  * @summary StandardJavaFileManager should support java.nio.file.Path.
  *          Test getFileObject methods.
  * @modules java.compiler
@@ -117,6 +117,40 @@
 
     //----------------------------------------------------------------------------------------------
 
+    @Test
+    void test_getJavaFileObjectsFromPaths_Iterable(StandardJavaFileManager fm) throws IOException {
+        test_getJavaFileObjectsFromPaths_Iterable(fm, getTestFilePaths());
+        test_getJavaFileObjectsFromPaths_Iterable(fm, getTestZipPaths());
+    }
+
+    /**
+     * Tests the {@code getJavaFileObjectsFromPaths(Iterable)} method for a specific file
+     * manager and a series of paths.
+     *
+     * Note: instances of MyStandardJavaFileManager only support
+     * encapsulating paths for files in the default file system.
+     *
+     * @param fm  the file manager to be tested
+     * @param paths  the paths to be tested
+     * @throws IOException
+     */
+    void test_getJavaFileObjectsFromPaths_Iterable(StandardJavaFileManager fm, List<Path> paths)
+            throws IOException {
+        boolean expectException = !isGetFileObjectsSupported(fm, paths);
+        try {
+            compile(fm.getJavaFileObjectsFromPaths((Iterable<Path>) paths));
+            if (expectException)
+                error("expected exception not thrown: " + IllegalArgumentException.class.getName());
+        } catch (RuntimeException e) {
+            if (expectException && e instanceof IllegalArgumentException)
+                return;
+            error("unexpected exception thrown: " + e);
+        }
+    }
+
+
+    //----------------------------------------------------------------------------------------------
+
     /**
      * Compiles a set of files.
      *