8220687: Add StandardJavaFileManager.getJavaFileObjectsFromPaths overload

Reviewed-by: jjg
This commit is contained in:
Liam Miller-Cushon 2019-03-26 15:00:02 -07:00
parent c532be950a
commit 31f64932c3
5 changed files with 97 additions and 4 deletions
src
java.compiler/share/classes/javax/tools
jdk.compiler/share/classes/com/sun/tools/javac
test/langtools/tools/javac/api/file

@ -28,9 +28,11 @@ package javax.tools;
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,13 +201,42 @@ public interface StandardJavaFileManager extends JavaFileManager {
* a directory or if this file manager does not support any of the
* given paths.
*
* @since 9
* @since 13
*/
default Iterable<? extends JavaFileObject> getJavaFileObjectsFromPaths(
Iterable<? extends Path> paths) {
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 getJavaFileObjectsFromPaths(asCollection(paths));
}
/**
* Returns file objects representing the given files.
* Convenience method equivalent to:
@ -484,4 +515,13 @@ public interface StandardJavaFileManager extends JavaFileManager {
}
};
}
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;
}
}

@ -436,6 +436,18 @@ public class ClientCodeWrapper {
}
}
@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 {

@ -897,7 +897,7 @@ public class JavacFileManager extends BaseFileManager implements StandardJavaFil
@Override @DefinedBy(Api.COMPILER)
public Iterable<? extends JavaFileObject> getJavaFileObjectsFromPaths(
Iterable<? extends Path> paths)
Collection<? extends Path> paths)
{
ArrayList<PathFileObject> result;
if (paths instanceof Collection<?>)

@ -204,6 +204,13 @@ public class DelegatingJavaFileManager implements JavaFileManager {
return baseSJFM.getJavaFileObjectsFromFiles(files);
}
@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) {

@ -23,7 +23,7 @@
/*
* @test
* @bug 8059977
* @bug 8059977 8220687
* @summary StandardJavaFileManager should support java.nio.file.Path.
* Test getFileObject methods.
* @modules java.compiler
@ -115,6 +115,40 @@ public class SJFM_GetFileObjects extends SJFM_TestBase {
}
//----------------------------------------------------------------------------------------------
@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);
}
}
//----------------------------------------------------------------------------------------------
/**