mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-15 16:44:36 +02:00
8171005: Fix JavaFileManager.getLocationForModule(Location location, JavaFileObject fo, String pkgName) to work with location == CLASS_OUTPUT
JavaFileManager operations that allow module-oriented locations should also allow output locations. Reviewed-by: jjg
This commit is contained in:
parent
59e977e56b
commit
5f63bc3ff0
4 changed files with 263 additions and 18 deletions
|
@ -479,8 +479,10 @@ public interface JavaFileManager extends Closeable, Flushable, OptionChecker {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a location for the module containing a specific file representing a Java
|
* Gets a location for the module containing a specific file representing a Java
|
||||||
* source or class, to be found in a module-oriented location.
|
* source or class, to be found within a location, which may be either
|
||||||
* The result will be a package-oriented location.
|
* a module-oriented location or an output location.
|
||||||
|
* The result will be an output location if the given location is
|
||||||
|
* an output location, or it will be a package-oriented location.
|
||||||
*
|
*
|
||||||
* @apiNote the package name is used to identify the position of the file object
|
* @apiNote the package name is used to identify the position of the file object
|
||||||
* within the <em>module/package/class</em> hierarchy identified by by the location.
|
* within the <em>module/package/class</em> hierarchy identified by by the location.
|
||||||
|
@ -494,7 +496,8 @@ public interface JavaFileManager extends Closeable, Flushable, OptionChecker {
|
||||||
*
|
*
|
||||||
* @throws IOException if an I/O error occurred
|
* @throws IOException if an I/O error occurred
|
||||||
* @throws UnsupportedOperationException if this operation if not supported by this file manager
|
* @throws UnsupportedOperationException if this operation if not supported by this file manager
|
||||||
* @throws IllegalArgumentException if the location is not a module-oriented location
|
* @throws IllegalArgumentException if the location is neither an output location nor a
|
||||||
|
* module-oriented location
|
||||||
* @since 9
|
* @since 9
|
||||||
*/
|
*/
|
||||||
default Location getLocationForModule(Location location, JavaFileObject fo, String pkgName) throws IOException {
|
default Location getLocationForModule(Location location, JavaFileObject fo, String pkgName) throws IOException {
|
||||||
|
@ -543,8 +546,9 @@ public interface JavaFileManager extends Closeable, Flushable, OptionChecker {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Lists the locations for all the modules in a module-oriented location.
|
* Lists the locations for all the modules in a module-oriented location or an output location.
|
||||||
* The locations that are returned will be package-oriented locations.
|
* The locations that are returned will be output locations if the given location is an output,
|
||||||
|
* or it will be a package-oriented locations.
|
||||||
*
|
*
|
||||||
* @implSpec This implementation throws {@code UnsupportedOperationException}.
|
* @implSpec This implementation throws {@code UnsupportedOperationException}.
|
||||||
*
|
*
|
||||||
|
|
|
@ -950,11 +950,7 @@ public class JavacFileManager extends BaseFileManager implements StandardJavaFil
|
||||||
|
|
||||||
@Override @DefinedBy(Api.COMPILER)
|
@Override @DefinedBy(Api.COMPILER)
|
||||||
public Location getLocationForModule(Location location, String moduleName) throws IOException {
|
public Location getLocationForModule(Location location, String moduleName) throws IOException {
|
||||||
Objects.requireNonNull(location);
|
checkModuleOrientedOrOutputLocation(location);
|
||||||
if (!location.isOutputLocation() && !location.isModuleOrientedLocation())
|
|
||||||
throw new IllegalArgumentException(
|
|
||||||
"location is not an output location or a module-oriented location: "
|
|
||||||
+ location.getName());
|
|
||||||
nullCheck(moduleName);
|
nullCheck(moduleName);
|
||||||
return locations.getLocationForModule(location, moduleName);
|
return locations.getLocationForModule(location, moduleName);
|
||||||
}
|
}
|
||||||
|
@ -978,7 +974,7 @@ public class JavacFileManager extends BaseFileManager implements StandardJavaFil
|
||||||
|
|
||||||
@Override @DefinedBy(Api.COMPILER)
|
@Override @DefinedBy(Api.COMPILER)
|
||||||
public Location getLocationForModule(Location location, JavaFileObject fo, String pkgName) throws IOException {
|
public Location getLocationForModule(Location location, JavaFileObject fo, String pkgName) throws IOException {
|
||||||
checkModuleOrientedLocation(location);
|
checkModuleOrientedOrOutputLocation(location);
|
||||||
if (!(fo instanceof PathFileObject))
|
if (!(fo instanceof PathFileObject))
|
||||||
throw new IllegalArgumentException(fo.getName());
|
throw new IllegalArgumentException(fo.getName());
|
||||||
int depth = 1; // allow 1 for filename
|
int depth = 1; // allow 1 for filename
|
||||||
|
@ -1012,7 +1008,7 @@ public class JavacFileManager extends BaseFileManager implements StandardJavaFil
|
||||||
|
|
||||||
@Override @DefinedBy(Api.COMPILER)
|
@Override @DefinedBy(Api.COMPILER)
|
||||||
public Iterable<Set<Location>> listLocationsForModules(Location location) throws IOException {
|
public Iterable<Set<Location>> listLocationsForModules(Location location) throws IOException {
|
||||||
checkModuleOrientedLocation(location);
|
checkModuleOrientedOrOutputLocation(location);
|
||||||
return locations.listLocationsForModules(location);
|
return locations.listLocationsForModules(location);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1098,10 +1094,12 @@ public class JavacFileManager extends BaseFileManager implements StandardJavaFil
|
||||||
throw new IllegalArgumentException("location is not an output location: " + location.getName());
|
throw new IllegalArgumentException("location is not an output location: " + location.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkModuleOrientedLocation(Location location) {
|
private void checkModuleOrientedOrOutputLocation(Location location) {
|
||||||
Objects.requireNonNull(location);
|
Objects.requireNonNull(location);
|
||||||
if (!location.isModuleOrientedLocation())
|
if (!location.isModuleOrientedLocation() && !location.isOutputLocation())
|
||||||
throw new IllegalArgumentException("location is not module-oriented: " + location.getName());
|
throw new IllegalArgumentException(
|
||||||
|
"location is not an output location or a module-oriented location: "
|
||||||
|
+ location.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkNotModuleOrientedLocation(Location location) {
|
private void checkNotModuleOrientedLocation(Location location) {
|
||||||
|
|
|
@ -476,6 +476,7 @@ public class Locations {
|
||||||
|
|
||||||
private Path outputDir;
|
private Path outputDir;
|
||||||
private Map<String, Location> moduleLocations;
|
private Map<String, Location> moduleLocations;
|
||||||
|
private Map<Path, Location> pathLocations;
|
||||||
|
|
||||||
OutputLocationHandler(Location location, Option... options) {
|
OutputLocationHandler(Location location, Option... options) {
|
||||||
super(location, options);
|
super(location, options);
|
||||||
|
@ -521,22 +522,51 @@ public class Locations {
|
||||||
outputDir = dir;
|
outputDir = dir;
|
||||||
}
|
}
|
||||||
moduleLocations = null;
|
moduleLocations = null;
|
||||||
|
pathLocations = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
Location getLocationForModule(String name) {
|
Location getLocationForModule(String name) {
|
||||||
if (moduleLocations == null)
|
if (moduleLocations == null) {
|
||||||
moduleLocations = new HashMap<>();
|
moduleLocations = new HashMap<>();
|
||||||
|
pathLocations = new HashMap<>();
|
||||||
|
}
|
||||||
Location l = moduleLocations.get(name);
|
Location l = moduleLocations.get(name);
|
||||||
if (l == null) {
|
if (l == null) {
|
||||||
|
Path out = outputDir.resolve(name);
|
||||||
l = new ModuleLocationHandler(location.getName() + "[" + name + "]",
|
l = new ModuleLocationHandler(location.getName() + "[" + name + "]",
|
||||||
name,
|
name,
|
||||||
Collections.singleton(outputDir.resolve(name)),
|
Collections.singleton(out),
|
||||||
true, false);
|
true, false);
|
||||||
moduleLocations.put(name, l);
|
moduleLocations.put(name, l);
|
||||||
|
pathLocations.put(out.toAbsolutePath(), l);
|
||||||
}
|
}
|
||||||
return l;
|
return l;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
Location getLocationForModule(Path dir) {
|
||||||
|
return pathLocations.get(dir);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean listed;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
Iterable<Set<Location>> listLocationsForModules() throws IOException {
|
||||||
|
if (!listed && outputDir != null) {
|
||||||
|
try (DirectoryStream<Path> stream = Files.newDirectoryStream(outputDir)) {
|
||||||
|
for (Path p : stream) {
|
||||||
|
getLocationForModule(p.getFileName().toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
listed = true;
|
||||||
|
}
|
||||||
|
if (moduleLocations == null)
|
||||||
|
return Collections.emptySet();
|
||||||
|
Set<Location> locns = new LinkedHashSet<>();
|
||||||
|
moduleLocations.forEach((k, v) -> locns.add(v));
|
||||||
|
return Collections.singleton(locns);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
213
langtools/test/tools/javac/file/ModuleAndPackageLocations.java
Normal file
213
langtools/test/tools/javac/file/ModuleAndPackageLocations.java
Normal file
|
@ -0,0 +1,213 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2016, 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 8171005
|
||||||
|
* @summary Verify behavior of JavaFileManager methods w.r.t. module/package oriented locations
|
||||||
|
* @library /tools/lib
|
||||||
|
* @modules java.compiler
|
||||||
|
* @build toolbox.TestRunner ModuleAndPackageLocations
|
||||||
|
* @run main ModuleAndPackageLocations
|
||||||
|
*/
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.EnumSet;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.StreamSupport;
|
||||||
|
|
||||||
|
import javax.tools.JavaCompiler;
|
||||||
|
import javax.tools.JavaFileManager;
|
||||||
|
import javax.tools.JavaFileManager.Location;
|
||||||
|
import javax.tools.JavaFileObject;
|
||||||
|
import javax.tools.JavaFileObject.Kind;
|
||||||
|
import javax.tools.StandardJavaFileManager;
|
||||||
|
import javax.tools.StandardLocation;
|
||||||
|
import javax.tools.ToolProvider;
|
||||||
|
|
||||||
|
import toolbox.TestRunner;
|
||||||
|
import toolbox.TestRunner.Test;
|
||||||
|
|
||||||
|
public class ModuleAndPackageLocations extends TestRunner {
|
||||||
|
|
||||||
|
public static void main(String... args) throws Exception {
|
||||||
|
new ModuleAndPackageLocations().runTests(m -> new Object[] { Paths.get(m.getName()) });
|
||||||
|
}
|
||||||
|
|
||||||
|
public ModuleAndPackageLocations() {
|
||||||
|
super(System.err);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testListLocations(Path outerBase) throws Exception {
|
||||||
|
doRunTest(outerBase, (base, fm) -> {
|
||||||
|
assertLocations(fm.listLocationsForModules(StandardLocation.MODULE_SOURCE_PATH),
|
||||||
|
toSet("MODULE_SOURCE_PATH[a]:false:false",
|
||||||
|
"MODULE_SOURCE_PATH[b]:false:false",
|
||||||
|
"MODULE_SOURCE_PATH[c]:false:false"));
|
||||||
|
assertLocations(fm.listLocationsForModules(StandardLocation.MODULE_PATH),
|
||||||
|
toSet("MODULE_PATH[0.X,a]:false:false",
|
||||||
|
"MODULE_PATH[0.X,b]:false:false"),
|
||||||
|
toSet("MODULE_PATH[1.X,c]:false:false",
|
||||||
|
"MODULE_PATH[1.X,b]:false:false"));
|
||||||
|
assertLocations(fm.listLocationsForModules(StandardLocation.SOURCE_OUTPUT),
|
||||||
|
toSet("SOURCE_OUTPUT[a]:false:true",
|
||||||
|
"SOURCE_OUTPUT[b]:false:true"));
|
||||||
|
|
||||||
|
fm.getLocationForModule(StandardLocation.SOURCE_OUTPUT, "c");
|
||||||
|
|
||||||
|
assertLocations(fm.listLocationsForModules(StandardLocation.SOURCE_OUTPUT),
|
||||||
|
toSet("SOURCE_OUTPUT[a]:false:true",
|
||||||
|
"SOURCE_OUTPUT[b]:false:true",
|
||||||
|
"SOURCE_OUTPUT[c]:false:true"));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetModuleForPath(Path outerBase) throws Exception {
|
||||||
|
doRunTest(outerBase, (base, fm) -> {
|
||||||
|
Location cOutput = fm.getLocationForModule(StandardLocation.SOURCE_OUTPUT, "c");
|
||||||
|
JavaFileObject testFO = fm.getJavaFileForOutput(cOutput, "test.Test", Kind.CLASS, null);
|
||||||
|
testFO.openOutputStream().close();
|
||||||
|
Location cOutput2 = fm.getLocationForModule(StandardLocation.SOURCE_OUTPUT, testFO, "test");
|
||||||
|
|
||||||
|
if (cOutput != cOutput2) {
|
||||||
|
throw new AssertionError("Unexpected location: " + cOutput2 + ", expected: " +cOutput);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRejects(Path outerBase) throws Exception {
|
||||||
|
doRunTest(outerBase, (base, fm) -> {
|
||||||
|
assertRefused(() -> fm.getClassLoader(StandardLocation.MODULE_SOURCE_PATH));
|
||||||
|
assertRefused(() -> fm.getFileForInput(StandardLocation.MODULE_SOURCE_PATH, "", ""));
|
||||||
|
assertRefused(() -> fm.getFileForOutput(StandardLocation.MODULE_SOURCE_PATH, "", "", null));
|
||||||
|
assertRefused(() -> fm.getJavaFileForInput(StandardLocation.MODULE_SOURCE_PATH, "", Kind.SOURCE));
|
||||||
|
assertRefused(() -> fm.getJavaFileForOutput(StandardLocation.MODULE_SOURCE_PATH, "", Kind.SOURCE, null));
|
||||||
|
assertRefused(() -> fm.getLocationForModule(StandardLocation.SOURCE_PATH, "test"));
|
||||||
|
JavaFileObject out = fm.getJavaFileForInput(StandardLocation.CLASS_OUTPUT, "test.Test", Kind.CLASS);
|
||||||
|
assertRefused(() -> fm.getLocationForModule(StandardLocation.SOURCE_PATH, out, "test"));
|
||||||
|
assertRefused(() -> fm.inferBinaryName(StandardLocation.MODULE_PATH, out));
|
||||||
|
assertRefused(() -> fm.inferModuleName(StandardLocation.MODULE_SOURCE_PATH));
|
||||||
|
assertRefused(() -> fm.list(StandardLocation.MODULE_SOURCE_PATH, "test", EnumSet.allOf(Kind.class), false));
|
||||||
|
assertRefused(() -> fm.listLocationsForModules(StandardLocation.SOURCE_PATH));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void doRunTest(Path base, TestExec test) throws Exception {
|
||||||
|
try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) {
|
||||||
|
Path msp = base.resolve("msp");
|
||||||
|
Path msp1 = msp.resolve("1");
|
||||||
|
Path msp2 = msp.resolve("2");
|
||||||
|
|
||||||
|
Files.createDirectories(msp1.resolve("a"));
|
||||||
|
Files.createDirectories(msp1.resolve("b"));
|
||||||
|
Files.createDirectories(msp2.resolve("b"));
|
||||||
|
Files.createDirectories(msp2.resolve("c"));
|
||||||
|
|
||||||
|
Path mp = base.resolve("mp");
|
||||||
|
Path mp1 = mp.resolve("1");
|
||||||
|
Path mp2 = mp.resolve("2");
|
||||||
|
|
||||||
|
touch(mp1.resolve("a/module-info.class"),
|
||||||
|
mp1.resolve("b/module-info.class"),
|
||||||
|
mp2.resolve("b/module-info.class"),
|
||||||
|
mp2.resolve("c/module-info.class"));
|
||||||
|
|
||||||
|
Path so = base.resolve("so");
|
||||||
|
|
||||||
|
Files.createDirectories(so.resolve("a"));
|
||||||
|
Files.createDirectories(so.resolve("b"));
|
||||||
|
|
||||||
|
List<String> mspOpt = Arrays.asList(msp1.toAbsolutePath().toString() +
|
||||||
|
File.pathSeparatorChar +
|
||||||
|
msp2.toAbsolutePath().toString());
|
||||||
|
|
||||||
|
List<String> mpOpt = Arrays.asList(mp1.toAbsolutePath().toString() +
|
||||||
|
File.pathSeparatorChar +
|
||||||
|
mp2.toAbsolutePath().toString());
|
||||||
|
|
||||||
|
fm.handleOption("--module-source-path", mspOpt.iterator());
|
||||||
|
fm.handleOption("--module-path", mpOpt.iterator());
|
||||||
|
fm.handleOption("-s", Arrays.asList(so.toString()).iterator());
|
||||||
|
|
||||||
|
test.run(base, fm);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Set<String> toSet(String... values) {
|
||||||
|
return new HashSet<>(Arrays.asList(values));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void touch(Path... paths) throws IOException {
|
||||||
|
for (Path p : paths) {
|
||||||
|
Files.createDirectories(p.getParent());
|
||||||
|
Files.newOutputStream(p).close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@SafeVarargs
|
||||||
|
private void assertLocations(Iterable<Set<Location>> locations, Set<String>... expected) {
|
||||||
|
List<Set<String>> actual =
|
||||||
|
StreamSupport.stream(locations.spliterator(), true)
|
||||||
|
.map(locs -> locs.stream()
|
||||||
|
.map(l -> toString(l))
|
||||||
|
.collect(Collectors.toSet()))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
if (!Objects.equals(actual, Arrays.asList(expected))) {
|
||||||
|
throw new AssertionError("Unexpected output: " + actual);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertRefused(Callable r) throws Exception {
|
||||||
|
try {
|
||||||
|
r.call();
|
||||||
|
throw new AssertionError("Expected exception did not occur");
|
||||||
|
} catch (IllegalArgumentException ex) {
|
||||||
|
//ok
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String toString(Location l) {
|
||||||
|
return l.getName().replaceAll("\\[([0-9])\\.[0-9]:", "[$1.X,") + ":" +
|
||||||
|
l.isModuleOrientedLocation() + ":" + l.isOutputLocation();
|
||||||
|
}
|
||||||
|
|
||||||
|
static interface TestExec {
|
||||||
|
public void run(Path base, JavaFileManager fm) throws Exception;
|
||||||
|
}
|
||||||
|
|
||||||
|
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue