Skip to content

Commit

Permalink
[batch compiler] Unrecognized option : --patch-module (eclipse-jdt#2544)
Browse files Browse the repository at this point in the history
+ implement parsing of the --patch-module option
+ associate affected CUs to the given module
+ detect errors: syntax, duplicates, unresolved module
+ add entry to the -help command line help

Required regression fix :
+ remove project info when removing module-info (JavaModel)

fixes eclipse-jdt#1895
  • Loading branch information
stephan-herrmann authored Jun 13, 2024
1 parent 5a6e0aa commit 470db4d
Show file tree
Hide file tree
Showing 8 changed files with 441 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -838,8 +838,19 @@ public ModuleBinding module(LookupEnvironment environment) {
}
if (this.compilationResult != null) {
ICompilationUnit compilationUnit = this.compilationResult.compilationUnit;
if (compilationUnit != null)
return compilationUnit.module(environment);
if (compilationUnit != null) {
ModuleBinding module = compilationUnit.module(environment);
if (module == null) {
ReferenceContext save = environment.problemReporter.referenceContext;
try {
environment.problemReporter.referenceContext = this;
environment.problemReporter.moduleNotFound(this, compilationUnit.getModuleName());
} finally {
environment.problemReporter.referenceContext = save;
}
}
return module;
}
}
return environment.module;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2018 IBM Corporation and others.
* Copyright (c) 2000, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -133,10 +133,7 @@ public char[] getModuleName() {
public ModuleBinding module(LookupEnvironment rootEnvironment) {
if (this.moduleBinding != null)
return this.moduleBinding;
this.moduleBinding = rootEnvironment.getModule(this.module);
if (this.moduleBinding == null)
throw new IllegalStateException("Module should be known"); //$NON-NLS-1$
return this.moduleBinding;
return this.moduleBinding = rootEnvironment.getModule(this.module);
}
@Override
public String getDestinationPath() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2023 IBM Corporation and others.
* Copyright (c) 2000, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -1368,6 +1368,7 @@ public static synchronized ResourceBundle getBundle(Locale locale) {
private List<String> addonReads = Collections.EMPTY_LIST;
public Set<String> rootModules = Collections.EMPTY_SET;
public Set<String> limitedModules;
private Map<String,String> patchModules; // <module>=<file>(<pathsep><file>)*

public Locale compilerLocale;
public CompilerOptions compilerOptions; // read-only
Expand Down Expand Up @@ -1840,6 +1841,7 @@ public void configure(String[] argv) {
final int INSIDE_RELEASE = 30;
final int INSIDE_LIMIT_MODULES = 31;
final int INSIDE_MODULE_VERSION = 32;
final int INSIDE_PATCH_MODULE = 33;

final int DEFAULT = 0;
ArrayList<String> bootclasspaths = new ArrayList<>(DEFAULT_SIZE_CLASSPATH);
Expand Down Expand Up @@ -2149,6 +2151,10 @@ public void configure(String[] argv) {
mode = INSIDE_LIMIT_MODULES;
continue;
}
if (currentArg.equals("--patch-module")) { //$NON-NLS-1$
mode = INSIDE_PATCH_MODULE;
continue;
}
if (currentArg.equals("--module-version")) { //$NON-NLS-1$
mode = INSIDE_MODULE_VERSION;
continue;
Expand Down Expand Up @@ -2785,6 +2791,20 @@ public void configure(String[] argv) {
this.limitedModules.add(tokenizer.nextToken().trim());
}
continue;
case INSIDE_PATCH_MODULE:
mode = DEFAULT;
String[] toks = currentArg.split("="); //$NON-NLS-1$
if (toks.length == 2) {
if (this.patchModules == null) {
this.patchModules = new HashMap<>();
}
if (this.patchModules.put(toks[0].trim(), toks[1].trim()) != null) {
throw new IllegalArgumentException(this.bind("configure.duplicatePatchModule", toks[0].trim())); //$NON-NLS-1$
}
} else {
throw new IllegalArgumentException(this.bind("configure.invalidSyntaxPatchModule", currentArg)); //$NON-NLS-1$
}
continue;
case INSIDE_MODULE_VERSION:
mode = DEFAULT;
this.moduleVersion = validateModuleVersion(currentArg);
Expand Down Expand Up @@ -3536,6 +3556,33 @@ private void processAddonModuleOptions(FileSystem env) {
}
}
}
/** Associate patching source files to their modules. */
protected void handlePatchModule() {
if (this.patchModules != null) {
Map<String, String> location2patchedModule = new HashMap<>();
for (Entry<String, String> entry : this.patchModules.entrySet()) {
StringTokenizer tokenizer = new StringTokenizer(entry.getValue(), File.pathSeparator);
while (tokenizer.hasMoreTokens()) {
String location = tokenizer.nextToken();
if (location2patchedModule.put(location, entry.getKey()) != null) {
throw new IllegalArgumentException(this.bind("configure.duplicateLocationPatchModule", location)); //$NON-NLS-1$
}
}
}
for(int i = 0; i < this.filenames.length; i++) {
if (this.modNames[i] == null) {
// does this source file patch an existing module?
for (Entry<String, String> entry : location2patchedModule.entrySet()) {
if (this.filenames[i].startsWith(entry.getKey()+File.separator)) {
this.modNames[i] = entry.getValue();
break;
}
}
}
}
}
}

protected ArrayList<FileSystem.Classpath> handleModulepath(String arg) {
ArrayList<String> modulePaths = processModulePathEntries(arg);
ArrayList<Classpath> result = new ArrayList<>();
Expand Down Expand Up @@ -4645,7 +4692,17 @@ public void outputClassFiles(CompilationResult unitResult) {
boolean generateClasspathStructure = false;
CompilationUnit compilationUnit =
(CompilationUnit) unitResult.compilationUnit;
if (compilationUnit.destinationPath == null) {
findDestination: if (compilationUnit.destinationPath == null) {
if (compilationUnit.module != null) {
// correlate patch-module to the corresponding destination path
for (Classpath classpath : this.checkedClasspaths) {
if (classpath.servesModule(compilationUnit.module)) {
currentDestinationPath = classpath.getDestinationPath();
generateClasspathStructure = true;
break findDestination;
}
}
}
if (this.destinationPath == null) {
currentDestinationPath =
extractDestinationPathFromSourceFile(unitResult);
Expand Down Expand Up @@ -5231,6 +5288,8 @@ protected void setPaths(ArrayList<String> bootclasspaths,

List<FileSystem.Classpath> cp = handleClasspath(classpaths, customEncoding);

handlePatchModule();

List<FileSystem.Classpath> mp = handleModulepath(modulePath);

List<FileSystem.Classpath> msp = handleModuleSourcepath(moduleSourcepath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ configure.duplicateModuleSourcepath = duplicate source module path specification
configure.invalidModuleDescriptor = cannot open the module descriptor from {0}
configure.invalidModuleOption = incorrectly formatted option: {0}
configure.duplicateExport = can specify a package in a module only once with --add-export
configure.duplicatePatchModule = duplicate module in --patch-module: {0}
configure.duplicateLocationPatchModule = location {0} is specified more than once in --patch-module
configure.invalidSyntaxPatchModule = invalid syntax for --patch-module: {0}
configure.OneOfModuleOrSourcePath = cannot specify both -source-path and --module-source-path
configure.duplicateBootClasspath = duplicate bootclasspath specification: {0}
configure.duplicateExtDirs = duplicate extdirs specification: {0}
Expand Down Expand Up @@ -240,6 +243,8 @@ misc.usage = {1} {2}\n\
\ resolved to be root modules\n\
\ --limit-modules <module>(,<module>)*\n\
\ specify the observable module names\n\
\ --patch-modules <module>=<directories separated by {0}>\n\
\ specify source locations for patching the given module\n\
\ --release <release> compile for a specific VM version\n\
\ \n\
\ Compliance options:\n\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11591,6 +11591,11 @@ public void invalidModule(ModuleReference ref) {
String[] args = new String[] { CharOperation.charToString(ref.moduleName) };
this.handle(IProblem.UndefinedModule, args, args, ref.sourceStart, ref.sourceEnd);
}
public void moduleNotFound(CompilationUnitDeclaration compilationUnitDeclaration, char[] moduleName) {
String[] args = new String[] { String.valueOf(moduleName) };
ASTNode location = (compilationUnitDeclaration.currentPackage != null) ? compilationUnitDeclaration.currentPackage : compilationUnitDeclaration;
handle(IProblem.UndefinedModule, args, args, location.sourceStart, location.sourceEnd);
}
public void missingModuleAddReads(char[] requiredModuleName) {
String[] args = new String[] { new String(requiredModuleName) };
this.handle(IProblem.UndefinedModuleAddReads, args, args, 0, 0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2023 IBM Corporation and others.
* Copyright (c) 2000, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -668,6 +668,8 @@ public void test012(){
" resolved to be root modules\n" +
" --limit-modules <module>(,<module>)*\n" +
" specify the observable module names\n" +
" --patch-modules <module>=<directories separated by " + File.pathSeparator + ">\n" +
" specify source locations for patching the given module\n" +
" --release <release> compile for a specific VM version\n" +
" \n" +
" Compliance options:\n" +
Expand Down
Loading

0 comments on commit 470db4d

Please sign in to comment.