8175560: Drop String pkgName from javax.tools.JavaFileManager.getLocationForModule(Location location, JavaFileObject fo, String pkgName)

Reviewed-by: jjg
This commit is contained in:
Jan Lahoda 2017-03-06 13:17:33 +01:00
parent e1d9daf27f
commit 2d911bad19
12 changed files with 101 additions and 90 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 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
@ -177,8 +177,8 @@ public class ForwardingJavaFileManager<M extends JavaFileManager> implements Jav
* @since 9
* @spec JPMS
*/
public Location getLocationForModule(Location location, JavaFileObject fo, String pkgName) throws IOException {
return fileManager.getLocationForModule(location, fo, pkgName);
public Location getLocationForModule(Location location, JavaFileObject fo) throws IOException {
return fileManager.getLocationForModule(location, fo);
}
/**

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 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
@ -480,20 +480,16 @@ public interface JavaFileManager extends Closeable, Flushable, OptionChecker {
}
/**
* Gets a location for the module containing a specific file representing a Java
* source or class, to be found within a location, which may be either
* Gets a location for the module containing a specific file
* to be found within a location, which may be either
* 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
* within the <em>module/package/class</em> hierarchy identified by by the location.
*
* @implSpec This implementation throws {@code UnsupportedOperationException}.
*
* @param location the module-oriented location
* @param fo the file
* @param pkgName the package name for the class(es) defined in this file
* @return the module containing the file
*
* @throws IOException if an I/O error occurred
@ -503,7 +499,7 @@ public interface JavaFileManager extends Closeable, Flushable, OptionChecker {
* @since 9
* @spec JPMS
*/
default Location getLocationForModule(Location location, JavaFileObject fo, String pkgName) throws IOException {
default Location getLocationForModule(Location location, JavaFileObject fo) throws IOException {
throw new UnsupportedOperationException();
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 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
@ -359,9 +359,9 @@ public class ClientCodeWrapper {
}
@Override @DefinedBy(Api.COMPILER)
public Location getLocationForModule(Location location, JavaFileObject fo, String pkgName) throws IOException {
public Location getLocationForModule(Location location, JavaFileObject fo) throws IOException {
try {
return clientJavaFileManager.getLocationForModule(location, unwrap(fo), pkgName);
return clientJavaFileManager.getLocationForModule(location, unwrap(fo));
} catch (ClientCodeException e) {
throw e;
} catch (RuntimeException | Error e) {

View File

@ -369,7 +369,7 @@ public class Modules extends JCTree.Visitor {
Location msplocn = getModuleLocation(tree);
Location plocn = fileManager.hasLocation(StandardLocation.PATCH_MODULE_PATH) ?
fileManager.getLocationForModule(StandardLocation.PATCH_MODULE_PATH,
tree.sourcefile, getPackageName(tree)) :
tree.sourcefile) :
null;
if (plocn != null) {
@ -385,6 +385,13 @@ public class Modules extends JCTree.Visitor {
}
}
} else if (msplocn != null) {
if (tree.getModuleDecl() != null) {
JavaFileObject canonical =
fileManager.getJavaFileForInput(msplocn, "module-info", Kind.SOURCE);
if (canonical == null || !fileManager.isSameFile(canonical, tree.sourcefile)) {
log.error(tree.pos(), Errors.ModuleNotFoundOnModuleSourcePath);
}
}
Name name = names.fromString(fileManager.inferModuleName(msplocn));
ModuleSymbol msym;
JCModuleDecl decl = tree.getModuleDecl();
@ -512,8 +519,7 @@ public class Modules extends JCTree.Visitor {
try {
Location loc =
fileManager.getLocationForModule(StandardLocation.PATCH_MODULE_PATH,
fo, getPackageName(tree));
fileManager.getLocationForModule(StandardLocation.PATCH_MODULE_PATH, fo);
if (loc != null) {
override.add(fileManager.inferModuleName(loc));
@ -532,15 +538,6 @@ public class Modules extends JCTree.Visitor {
}
}
private String getPackageName(JCCompilationUnit tree) {
if (tree.getModuleDecl() != null) {
return null;
} else {
JCPackageDecl pkg = tree.getPackage();
return (pkg == null) ? "" : TreeInfo.fullName(pkg.pid).toString();
}
}
/**
* Determine the location for the module on the module source path
* or source output directory which contains a given CompilationUnit.
@ -552,18 +549,15 @@ public class Modules extends JCTree.Visitor {
* @throws IOException if there is a problem while searching for the module.
*/
private Location getModuleLocation(JCCompilationUnit tree) throws IOException {
String pkgName = getPackageName(tree);
JavaFileObject fo = tree.sourcefile;
Location loc =
fileManager.getLocationForModule(StandardLocation.MODULE_SOURCE_PATH,
fo, (pkgName == null) ? null : pkgName);
fileManager.getLocationForModule(StandardLocation.MODULE_SOURCE_PATH, fo);
if (loc == null) {
Location sourceOutput = fileManager.hasLocation(StandardLocation.SOURCE_OUTPUT) ?
StandardLocation.SOURCE_OUTPUT : StandardLocation.CLASS_OUTPUT;
loc =
fileManager.getLocationForModule(sourceOutput,
fo, (pkgName == null) ? null : pkgName);
fileManager.getLocationForModule(sourceOutput, fo);
}
return loc;
}

View File

@ -977,31 +977,13 @@ public class JavacFileManager extends BaseFileManager implements StandardJavaFil
}
@Override @DefinedBy(Api.COMPILER)
public Location getLocationForModule(Location location, JavaFileObject fo, String pkgName) throws IOException {
public Location getLocationForModule(Location location, JavaFileObject fo) throws IOException {
checkModuleOrientedOrOutputLocation(location);
if (!(fo instanceof PathFileObject))
return null;
int depth = 1; // allow 1 for filename
if (pkgName != null && !pkgName.isEmpty()) {
depth += 1;
for (int i = 0; i < pkgName.length(); i++) {
switch (pkgName.charAt(i)) {
case '/': case '.':
depth++;
}
}
}
Path p = Locations.normalize(((PathFileObject) fo).path);
int fc = p.getNameCount();
if (depth < fc) {
Path root = p.getRoot();
Path subpath = p.subpath(0, fc - depth);
Path dir = (root == null) ? subpath : root.resolve(subpath);
// need to find dir in location
return locations.getLocationForModule(location, dir);
} else {
return null;
}
// need to find p in location
return locations.getLocationForModule(location, p);
}
@Override @DefinedBy(Api.COMPILER)

View File

@ -60,6 +60,7 @@ import java.util.Map;
import java.util.Objects;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@ -438,7 +439,7 @@ public class Locations {
/**
* @see JavaFileManager#getLocationForModule(Location, JavaFileObject, String)
*/
Location getLocationForModule(Path dir) throws IOException {
Location getLocationForModule(Path file) throws IOException {
return null;
}
@ -588,8 +589,8 @@ public class Locations {
}
@Override
Location getLocationForModule(Path dir) {
return (moduleTable == null) ? null : moduleTable.get(dir);
Location getLocationForModule(Path file) {
return (moduleTable == null) ? null : moduleTable.get(file);
}
private boolean listed;
@ -986,7 +987,16 @@ public class Locations {
}
ModuleLocationHandler get(Path path) {
return pathMap.get(path);
while (path != null) {
ModuleLocationHandler l = pathMap.get(path);
if (l != null)
return l;
path = path.getParent();
}
return null;
}
void clear() {
@ -1385,12 +1395,19 @@ public class Locations {
moduleTable = new ModuleTable();
map.forEach((modName, modPath) -> {
String locnName = location.getName() + "[" + modName + "]";
ModuleLocationHandler l = new ModuleLocationHandler(this, locnName, modName,
modPath, false);
moduleTable.add(l);
boolean hasModuleInfo = modPath.stream().anyMatch(checkModuleInfo);
if (hasModuleInfo) {
String locnName = location.getName() + "[" + modName + "]";
ModuleLocationHandler l = new ModuleLocationHandler(this, locnName, modName,
modPath, false);
moduleTable.add(l);
}
});
}
//where:
private final Predicate<Path> checkModuleInfo =
p -> Files.exists(p.resolve("module-info.java"));
private boolean isSeparator(char ch) {
// allow both separators on Windows
@ -1537,8 +1554,8 @@ public class Locations {
}
@Override
Location getLocationForModule(Path dir) {
return (moduleTable == null) ? null : moduleTable.get(dir);
Location getLocationForModule(Path file) {
return (moduleTable == null) ? null : moduleTable.get(file);
}
@Override
@ -1644,9 +1661,9 @@ public class Locations {
}
@Override
Location getLocationForModule(Path dir) throws IOException {
Location getLocationForModule(Path file) throws IOException {
initSystemModules();
return moduleTable.get(dir);
return moduleTable.get(file);
}
@Override
@ -1724,6 +1741,8 @@ public class Locations {
return false;
}
moduleTable.clear();
// Allow an extended syntax for --patch-module consisting of a series
// of values separated by NULL characters. This is to facilitate
// supporting deferred file manager options on the command line.
@ -1775,8 +1794,8 @@ public class Locations {
}
@Override
Location getLocationForModule(Path dir) throws IOException {
return moduleTable.get(dir);
Location getLocationForModule(Path file) throws IOException {
return moduleTable.get(file);
}
@Override
@ -1857,9 +1876,9 @@ public class Locations {
return (h == null ? null : h.getLocationForModule(name));
}
Location getLocationForModule(Location location, Path dir) throws IOException {
Location getLocationForModule(Location location, Path file) throws IOException {
LocationHandler h = getHandler(location);
return (h == null ? null : h.getLocationForModule(dir));
return (h == null ? null : h.getLocationForModule(file));
}
void setLocationForModule(Location location, String moduleName,

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 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
@ -204,8 +204,8 @@ public class SmartFileManager extends ForwardingJavaFileManager<JavaFileManager>
}
@Override @DefinedBy(Api.COMPILER)
public Location getLocationForModule(Location location, JavaFileObject fo, String pkgName) throws IOException {
return super.getLocationForModule(location, locUnwrap(fo), pkgName);
public Location getLocationForModule(Location location, JavaFileObject fo) throws IOException {
return super.getLocationForModule(location, locUnwrap(fo));
}
private static String packageNameFromFileName(String fn) {

View File

@ -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
@ -686,11 +686,10 @@ public abstract class JavadocHelper implements AutoCloseable {
@Override @DefinedBy(Api.COMPILER)
public Location getLocationForModule(Location location,
JavaFileObject fo,
String pkgName) throws IOException {
JavaFileObject fo) throws IOException {
return fo == file
? PATCH_LOCATION
: super.getLocationForModule(location, fo, pkgName);
: super.getLocationForModule(location, fo);
}
@Override @DefinedBy(Api.COMPILER)

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 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
@ -545,8 +545,8 @@ class MemoryFileManager implements JavaFileManager {
}
@Override
public Location getLocationForModule(Location location, JavaFileObject fo, String pkgName) throws IOException {
return stdFileManager.getLocationForModule(location, fo, pkgName);
public Location getLocationForModule(Location location, JavaFileObject fo) throws IOException {
return stdFileManager.getLocationForModule(location, fo);
}
@Override

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 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
@ -407,9 +407,9 @@ public class TestClientCodeWrapper extends JavacTestingAbstractProcessor {
}
@Override
public Location getLocationForModule(Location location, JavaFileObject fo, String pkgName) throws IOException {
public Location getLocationForModule(Location location, JavaFileObject fo) throws IOException {
throwUserExceptionIfNeeded(fileManagerMethod, "getLocationForModule");
return super.getLocationForModule(location, fo, pkgName);
return super.getLocationForModule(location, fo);
}
@Override

View File

@ -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
@ -23,7 +23,7 @@
/**
* @test
* @bug 8171005
* @bug 8171005 8175560
* @summary Verify behavior of JavaFileManager methods w.r.t. module/package oriented locations
* @library /tools/lib
* @modules java.compiler
@ -99,7 +99,7 @@ public class ModuleAndPackageLocations extends TestRunner {
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");
Location cOutput2 = fm.getLocationForModule(StandardLocation.SOURCE_OUTPUT, testFO);
if (cOutput != cOutput2) {
throw new AssertionError("Unexpected location: " + cOutput2 + ", expected: " +cOutput);
@ -117,7 +117,7 @@ public class ModuleAndPackageLocations extends TestRunner {
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.getLocationForModule(StandardLocation.SOURCE_PATH, out));
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));
@ -131,10 +131,10 @@ public class ModuleAndPackageLocations extends TestRunner {
Path msp1 = msp.resolve("1");
Path msp2 = msp.resolve("2");
Files.createDirectories(msp1.resolve("a"));
touch(msp1.resolve("a/module-info.java"));
Files.createDirectories(msp1.resolve("b"));
Files.createDirectories(msp2.resolve("b"));
Files.createDirectories(msp2.resolve("c"));
touch(msp2.resolve("b/module-info.java"));
touch(msp2.resolve("c/module-info.java"));
Path mp = base.resolve("mp");
Path mp1 = mp.resolve("1");
@ -210,4 +210,4 @@ public class ModuleAndPackageLocations extends TestRunner {
}
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
}
}

View File

@ -23,7 +23,7 @@
/**
* @test
* @bug 8165102
* @bug 8165102 8175560
* @summary incorrect message from javac
* @library /tools/lib
* @modules
@ -48,6 +48,27 @@ public class ModulesAndModuleSourcePathTest extends ModuleTestBase {
@Test
public void testModuleNotInModuleSrcPath(Path base) throws Exception {
Path src = base.resolve("src");
Path m = src.resolve("m");
Files.createDirectories(m);
Path extra = base.resolve("m");
tb.writeJavaFiles(extra, "module m {}");
Path classes = base.resolve("classes");
Files.createDirectories(classes);
String log = new JavacTask(tb)
.options("-XDrawDiagnostics", "--module-source-path", src.toString())
.outdir(classes)
.files(findJavaFiles(extra))
.run(Task.Expect.FAIL)
.writeAll()
.getOutput(Task.OutputKind.DIRECT);
if (!log.contains("module-info.java:1:1: compiler.err.module.not.found.on.module.source.path"))
throw new Exception("expected output not found");
}
@Test
public void testModuleNotInPackageHierarchy(Path base) throws Exception {
Path src = base.resolve("src");
Path m = src.resolve("m");
Path extra = m.resolve("extra");