Skip to content

Commit

Permalink
8188035: JavaFileManager.listLocationsForModules does not always refl…
Browse files Browse the repository at this point in the history
…ect values set through StandardJavaFileManager.setLocationForModule.

Summary: Prepending explictely set module locations in listLocationsForModules.
Reviewed-by: jjg
  • Loading branch information
jlahoda committed Oct 19, 2017
1 parent d0b0a2f commit 3978bff
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.Log;
import com.sun.tools.javac.jvm.ModuleNameReader;
import com.sun.tools.javac.util.Assert;
import com.sun.tools.javac.util.Iterators;
import com.sun.tools.javac.util.Pair;
import com.sun.tools.javac.util.StringUtils;

Expand Down Expand Up @@ -964,6 +964,7 @@ private class ModuleLocationHandler extends LocationHandler implements Location
private final String name;
private final String moduleName;
private final boolean output;
boolean explicit;
Collection<Path> searchPath;

ModuleLocationHandler(LocationHandler parent, String name, String moduleName,
Expand Down Expand Up @@ -1083,6 +1084,14 @@ boolean contains(Path file) throws IOException {
Set<Location> locations() {
return Collections.unmodifiableSet(nameMap.values().stream().collect(Collectors.toSet()));
}

Set<Location> explicitLocations() {
return Collections.unmodifiableSet(nameMap.entrySet()
.stream()
.filter(e -> e.getValue().explicit)
.map(e -> e.getValue())
.collect(Collectors.toSet()));
}
}

/**
Expand Down Expand Up @@ -1119,10 +1128,20 @@ public Location getLocationForModule(Path file) {

@Override
Iterable<Set<Location>> listLocationsForModules() {
Set<Location> explicitLocations = moduleTable != null ?
moduleTable.explicitLocations() : Collections.emptySet();
Iterable<Set<Location>> explicitLocationsList = !explicitLocations.isEmpty()
? Collections.singletonList(explicitLocations)
: Collections.emptyList();

if (searchPath == null)
return Collections.emptyList();
return explicitLocationsList;

return ModulePathIterator::new;
Iterable<Set<Location>> searchPathLocations =
() -> new ModulePathIterator();
return () -> Iterators.createCompoundIterator(Arrays.asList(explicitLocationsList,
searchPathLocations),
Iterable::iterator);
}

@Override
Expand Down Expand Up @@ -1159,6 +1178,7 @@ void setPathsForModule(String name, Iterable<? extends Path> paths) throws IOExc
l.searchPath = checkedPaths;
moduleTable.updatePaths(l);
}
l.explicit = true;
}

private List<Path> checkPaths(Iterable<? extends Path> paths) throws IOException {
Expand Down
61 changes: 58 additions & 3 deletions test/langtools/tools/javac/file/SetLocationForModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

/**
* @test
* @bug 8173914
* @bug 8173914 8188035
* @summary JavaFileManager.setLocationForModule
* @modules jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.main
Expand All @@ -42,8 +42,10 @@
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import javax.tools.JavaCompiler;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileManager.Location;
import javax.tools.StandardJavaFileManager;
import javax.tools.StandardLocation;
Expand Down Expand Up @@ -112,14 +114,35 @@ public void testModulePath(Path base) throws IOException {
checkEqual("override setting 1",
fm.getLocationAsPaths(m), override1);

checkEqual("override setting 1b",
fm.getLocationAsPaths(fm.listLocationsForModules(locn).iterator().next().iterator().next()),
override1);

try (StandardJavaFileManager fm2 = comp.getStandardFileManager(null, null, null)) {
fm2.setLocationForModule(locn, "m", List.of(override1));
checkEqual("override setting 2",
fm2.getLocationAsPaths(m), override1);

checkEqual("override setting 2b",
fm2.getLocationAsPaths(fm2.listLocationsForModules(locn).iterator().next().iterator().next()),
override1);
}

Path override2 = Files.createDirectories(base.resolve("override2"));
fm.setLocationFromPaths(m, List.of(override2));
checkEqual("override setting 2",
checkEqual("override setting 3",
fm.getLocationAsPaths(m), override2);

Path modules2 = Files.createDirectories(base.resolve("modules2"));
new JavacTask(tb)
.outdir(modules2)
.options("--module-source-path", src.toString())
.files(tb.findJavaFiles(src))
.run();
fm.setLocationFromPaths(locn, List.of(modules2));

m = fm.getLocationForModule(locn, "m");

checkEqual("updated setting",
fm.getLocationAsPaths(m), modules2.resolve("m"));
}
Expand Down Expand Up @@ -147,6 +170,10 @@ public void testModuleSourcePath(Path base) throws IOException {
checkEqual("override setting 1",
fm.getLocationAsPaths(m), override1);

checkEqual("override setting 1b",
fm.getLocationAsPaths(fm.listLocationsForModules(locn).iterator().next().iterator().next()),
override1);

Path override2 = Files.createDirectories(base.resolve("override2"));
tb.writeJavaFiles(override2, "module m { }");
fm.setLocationFromPaths(m, List.of(override2));
Expand All @@ -159,6 +186,8 @@ public void testModuleSourcePath(Path base) throws IOException {
// fm.setLocationFromPaths(locn, List.of(src2));
fm.handleOption("--module-source-path", List.of(src2.toString()).iterator());

m = fm.getLocationForModule(locn, "m");

checkEqual("updated setting",
fm.getLocationAsPaths(m), src2.resolve("m"));
}
Expand All @@ -181,6 +210,10 @@ public void testOutput(Path base) throws IOException {
checkEqual("override setting 1",
fm.getLocationAsPaths(m), override1);

checkEqual("override setting 1b",
fm.getLocationAsPaths(fm.listLocationsForModules(locn).iterator().next().iterator().next()),
override1);

Path override2 = Files.createDirectories(base.resolve("override2"));
fm.setLocationFromPaths(m, List.of(override2));
checkEqual("override setting 2",
Expand All @@ -189,6 +222,8 @@ public void testOutput(Path base) throws IOException {
Path out2 = Files.createDirectories(base.resolve("out2"));
fm.setLocationFromPaths(locn, List.of(out2));

m = fm.getLocationForModule(locn, "m");

checkEqual("updated setting",
fm.getLocationAsPaths(m), out2.resolve("m"));
}
Expand Down Expand Up @@ -259,13 +294,33 @@ public void testSystemModules(Path base) throws IOException {
checkEqual("override setting 1",
fm.getLocationAsPaths(javaCompiler), override1);

checkEqual("override setting 1b",
fm.getLocationAsPaths(findLocation(fm, fm.listLocationsForModules(locn), "java.compiler")),
override1);

Path override2 = Files.createDirectories(base.resolve("override2"));
fm.setLocationFromPaths(javaCompiler, List.of(override2));
checkEqual("override setting 2",
fm.getLocationAsPaths(javaCompiler), override2);
}
}

private Location findLocation(JavaFileManager fm, Iterable<Set<Location>> locations, String moduleName) {
for (Set<Location> locs : locations) {
for (Location loc : locs) {
try {
if (moduleName.equals(fm.inferModuleName(loc))) {
return loc;
}
} catch (IOException ex) {
throw new IllegalStateException(ex);
}
}
}

throw new IllegalStateException();
}

@Test
public void testTemplate(Path base) {
// set a top default
Expand Down Expand Up @@ -302,7 +357,7 @@ void checkException(String message,
void checkEqual(String message, Iterable<? extends Path> found, Path... expect) {
List<Path> fList = asList(found);
List<Path> eList = List.of(expect);
if (!Objects.equals(fList, fList)) {
if (!Objects.equals(fList, eList)) {
error(message + ": lists not equal\n"
+ "expect: " + eList + "\n"
+ " found: " + fList);
Expand Down

0 comments on commit 3978bff

Please sign in to comment.