< prev index next >
src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java
Print this page
rev 3947 : imported patch xmodule-to-patch-module
*** 143,153 ****
private final Source source;
private final boolean allowModules;
public final boolean multiModuleMode;
! private final String moduleOverride;
private final Name java_se;
private final Name java_;
ModuleSymbol defaultModule;
--- 143,153 ----
private final Source source;
private final boolean allowModules;
public final boolean multiModuleMode;
! private final String legacyModuleOverride;
private final Name java_se;
private final Name java_;
ModuleSymbol defaultModule;
*** 190,200 ****
allowModules = source.allowModules();
Options options = Options.instance(context);
lintOptions = options.isUnset(Option.XLINT_CUSTOM, "-" + LintCategory.OPTIONS.option);
! moduleOverride = options.get(Option.XMODULE);
multiModuleMode = fileManager.hasLocation(StandardLocation.MODULE_SOURCE_PATH);
ClassWriter classWriter = ClassWriter.instance(context);
classWriter.multiModuleMode = multiModuleMode;
JNIWriter jniWriter = JNIWriter.instance(context);
--- 190,200 ----
allowModules = source.allowModules();
Options options = Options.instance(context);
lintOptions = options.isUnset(Option.XLINT_CUSTOM, "-" + LintCategory.OPTIONS.option);
! legacyModuleOverride = options.get(Option.XMODULE);
multiModuleMode = fileManager.hasLocation(StandardLocation.MODULE_SOURCE_PATH);
ClassWriter classWriter = ClassWriter.instance(context);
classWriter.multiModuleMode = multiModuleMode;
JNIWriter jniWriter = JNIWriter.instance(context);
*** 364,376 ****
continue;
}
JavaFileObject prev = log.useSource(tree.sourcefile);
try {
! Location locn = getModuleLocation(tree);
! if (locn != null) {
! Name name = names.fromString(fileManager.inferModuleName(locn));
ModuleSymbol msym;
JCModuleDecl decl = tree.getModuleDecl();
if (decl != null) {
msym = decl.sym;
if (msym.name != name) {
--- 364,384 ----
continue;
}
JavaFileObject prev = log.useSource(tree.sourcefile);
try {
! Location msplocn = getModuleLocation(tree);
! Location plocn = fileManager.hasLocation(StandardLocation.PATCH_MODULE_PATH) ?
! fileManager.getLocationForModule(StandardLocation.PATCH_MODULE_PATH,
! tree.sourcefile, getPackageName(tree)) :
! null;
!
! if (msplocn != null) {
! if (plocn != null) {
! log.error(tree.pos(), Errors.FilePatchedAndMsp);
! }
! Name name = names.fromString(fileManager.inferModuleName(msplocn));
ModuleSymbol msym;
JCModuleDecl decl = tree.getModuleDecl();
if (decl != null) {
msym = decl.sym;
if (msym.name != name) {
*** 381,398 ****
log.error(tree.pos(), Errors.UnnamedPkgNotAllowedNamedModules);
}
msym = syms.enterModule(name);
}
if (msym.sourceLocation == null) {
! msym.sourceLocation = locn;
if (fileManager.hasLocation(StandardLocation.CLASS_OUTPUT)) {
msym.classLocation = fileManager.getLocationForModule(
StandardLocation.CLASS_OUTPUT, msym.name.toString());
}
}
tree.modle = msym;
rootModules.add(msym);
} else if (c != null && c.packge().modle == syms.unnamedModule) {
tree.modle = syms.unnamedModule;
} else {
if (tree.getModuleDecl() != null) {
log.error(tree.pos(), Errors.ModuleNotFoundOnModuleSourcePath);
--- 389,411 ----
log.error(tree.pos(), Errors.UnnamedPkgNotAllowedNamedModules);
}
msym = syms.enterModule(name);
}
if (msym.sourceLocation == null) {
! msym.sourceLocation = msplocn;
if (fileManager.hasLocation(StandardLocation.CLASS_OUTPUT)) {
msym.classLocation = fileManager.getLocationForModule(
StandardLocation.CLASS_OUTPUT, msym.name.toString());
}
}
tree.modle = msym;
rootModules.add(msym);
+ } else if (plocn != null) {
+ Name name = names.fromString(fileManager.inferModuleName(plocn));
+ ModuleSymbol msym = moduleFinder.findModule(name);
+ tree.modle = msym;
+ rootModules.add(msym);
} else if (c != null && c.packge().modle == syms.unnamedModule) {
tree.modle = syms.unnamedModule;
} else {
if (tree.getModuleDecl() != null) {
log.error(tree.pos(), Errors.ModuleNotFoundOnModuleSourcePath);
*** 412,472 ****
syms.unnamedModule.sourceLocation = StandardLocation.SOURCE_PATH;
syms.unnamedModule.classLocation = StandardLocation.CLASS_PATH;
}
defaultModule = syms.unnamedModule;
} else {
if (defaultModule == null) {
switch (rootModules.size()) {
case 0:
defaultModule = moduleFinder.findSingleModule();
if (defaultModule == syms.unnamedModule) {
if (moduleOverride != null) {
checkNoAllModulePath();
defaultModule = moduleFinder.findModule(names.fromString(moduleOverride));
defaultModule.sourceLocation = StandardLocation.SOURCE_PATH;
} else {
// Question: why not do findAllModules and initVisiblePackages here?
// i.e. body of unnamedModuleCompleter
defaultModule.completer = getUnnamedModuleCompleter();
defaultModule.classLocation = StandardLocation.CLASS_PATH;
}
} else {
! checkSpecifiedModule(trees, Errors.ModuleInfoWithXmoduleClasspath);
checkNoAllModulePath();
defaultModule.complete();
// Question: why not do completeModule here?
defaultModule.completer = sym -> completeModule((ModuleSymbol) sym);
}
rootModules.add(defaultModule);
break;
case 1:
! checkSpecifiedModule(trees, Errors.ModuleInfoWithXmoduleSourcepath);
checkNoAllModulePath();
defaultModule = rootModules.iterator().next();
defaultModule.classLocation = StandardLocation.CLASS_OUTPUT;
break;
default:
Assert.error("too many modules");
}
- defaultModule.sourceLocation = StandardLocation.SOURCE_PATH;
} else if (rootModules.size() == 1 && defaultModule == rootModules.iterator().next()) {
defaultModule.complete();
defaultModule.completer = sym -> completeModule((ModuleSymbol) sym);
} else {
Assert.check(rootModules.isEmpty());
! rootModules.add(defaultModule);
}
if (defaultModule != syms.unnamedModule) {
syms.unnamedModule.completer = getUnnamedModuleCompleter();
syms.unnamedModule.classLocation = StandardLocation.CLASS_PATH;
}
for (JCCompilationUnit tree: trees) {
! tree.modle = defaultModule;
}
}
}
/**
* Determine the location for the module on the module source path
* or source output directory which contains a given CompilationUnit.
--- 425,545 ----
syms.unnamedModule.sourceLocation = StandardLocation.SOURCE_PATH;
syms.unnamedModule.classLocation = StandardLocation.CLASS_PATH;
}
defaultModule = syms.unnamedModule;
} else {
+ ModuleSymbol module = null;
if (defaultModule == null) {
+ String moduleOverride = singleModuleOverride(trees);
switch (rootModules.size()) {
case 0:
defaultModule = moduleFinder.findSingleModule();
if (defaultModule == syms.unnamedModule) {
if (moduleOverride != null) {
checkNoAllModulePath();
defaultModule = moduleFinder.findModule(names.fromString(moduleOverride));
+ if (legacyModuleOverride != null) {
defaultModule.sourceLocation = StandardLocation.SOURCE_PATH;
+ }
} else {
// Question: why not do findAllModules and initVisiblePackages here?
// i.e. body of unnamedModuleCompleter
defaultModule.completer = getUnnamedModuleCompleter();
+ defaultModule.sourceLocation = StandardLocation.SOURCE_PATH;
defaultModule.classLocation = StandardLocation.CLASS_PATH;
}
} else {
! checkSpecifiedModule(trees, moduleOverride, Errors.ModuleInfoWithPatchedModuleClasspath);
checkNoAllModulePath();
defaultModule.complete();
// Question: why not do completeModule here?
defaultModule.completer = sym -> completeModule((ModuleSymbol) sym);
+ defaultModule.sourceLocation = StandardLocation.SOURCE_PATH;
}
rootModules.add(defaultModule);
break;
case 1:
! checkSpecifiedModule(trees, moduleOverride, Errors.ModuleInfoWithPatchedModuleSourcepath);
checkNoAllModulePath();
defaultModule = rootModules.iterator().next();
+ defaultModule.sourceLocation = StandardLocation.SOURCE_PATH;
defaultModule.classLocation = StandardLocation.CLASS_OUTPUT;
break;
default:
Assert.error("too many modules");
}
} else if (rootModules.size() == 1 && defaultModule == rootModules.iterator().next()) {
defaultModule.complete();
defaultModule.completer = sym -> completeModule((ModuleSymbol) sym);
} else {
Assert.check(rootModules.isEmpty());
! String moduleOverride = singleModuleOverride(trees);
! if (moduleOverride != null) {
! module = moduleFinder.findModule(names.fromString(moduleOverride));
! } else {
! module = defaultModule;
! }
! rootModules.add(module);
}
if (defaultModule != syms.unnamedModule) {
syms.unnamedModule.completer = getUnnamedModuleCompleter();
syms.unnamedModule.classLocation = StandardLocation.CLASS_PATH;
}
+ if (module == null) {
+ module = defaultModule;
+ }
+
for (JCCompilationUnit tree: trees) {
! tree.modle = module;
! }
! }
! }
!
! private String singleModuleOverride(List<JCCompilationUnit> trees) {
! if (!fileManager.hasLocation(StandardLocation.PATCH_MODULE_PATH)) {
! return legacyModuleOverride;
! }
!
! Set<String> override = new LinkedHashSet<>();
! for (JCCompilationUnit tree : trees) {
! JavaFileObject fo = tree.sourcefile;
!
! try {
! Location loc =
! fileManager.getLocationForModule(StandardLocation.PATCH_MODULE_PATH,
! fo, getPackageName(tree));
!
! if (loc != null) {
! try {
! override.add(fileManager.inferModuleName(loc));
! } catch (IOException ex) {
! throw new Error(ex);
}
}
+ } catch (IOException ex) {
+ ex.printStackTrace();
+ }
+ }
+
+ switch (override.size()) {
+ case 0: return legacyModuleOverride;
+ case 1: return override.iterator().next();
+ default:
+ log.error(Errors.TooManyPatchedModules(override));
+ return null;
+ }
+ }
+
+ 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.
*** 476,511 ****
* @param tree the compilation unit tree
* @return the location for the enclosing module
* @throws IOException if there is a problem while searching for the module.
*/
private Location getModuleLocation(JCCompilationUnit tree) throws IOException {
! Name pkgName;
! if (tree.getModuleDecl() != null) {
! pkgName = null;
! } else {
! JCPackageDecl pkg = tree.getPackage();
! pkgName = (pkg == null) ? names.empty : TreeInfo.fullName(pkg.pid);
! }
!
JavaFileObject fo = tree.sourcefile;
- // For now, just check module source path.
- // We may want to check source path as well.
Location loc =
fileManager.getLocationForModule(StandardLocation.MODULE_SOURCE_PATH,
! fo, (pkgName == null) ? null : pkgName.toString());
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.toString());
}
return loc;
}
! private void checkSpecifiedModule(List<JCCompilationUnit> trees, JCDiagnostic.Error error) {
if (moduleOverride != null) {
JavaFileObject prev = log.useSource(trees.head.sourcefile);
try {
log.error(trees.head.pos(), error);
} finally {
--- 549,575 ----
* @param tree the compilation unit tree
* @return the location for the enclosing module
* @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);
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);
}
return loc;
}
! private void checkSpecifiedModule(List<JCCompilationUnit> trees, String moduleOverride, JCDiagnostic.Error error) {
if (moduleOverride != null) {
JavaFileObject prev = log.useSource(trees.head.sourcefile);
try {
log.error(trees.head.pos(), error);
} finally {
*** 1598,1607 ****
String toString(JavaFileObject fo) {
return (fo == null) ? "--" : fo.getName();
}
public void newRound() {
- rootModules = null;
allModules = null;
warnedMissing.clear();
}
}
--- 1662,1671 ----
String toString(JavaFileObject fo) {
return (fo == null) ? "--" : fo.getName();
}
public void newRound() {
allModules = null;
+ rootModules = null;
warnedMissing.clear();
}
}
< prev index next >