From 235fcd8b6fdd59c7c5955b47272910f10def8609 Mon Sep 17 00:00:00 2001 From: Stephan Herrmann Date: Sun, 9 Jun 2024 23:38:00 +0200 Subject: [PATCH] [batch compiler] Unrecognized option : --patch-module fixes #1895 --- .../jdt/internal/compiler/batch/Main.java | 35 +++++++++++- .../compiler/batch/messages.properties | 2 + .../regression/ModuleCompilationTests.java | 54 +++++++++++++++++++ 3 files changed, 90 insertions(+), 1 deletion(-) diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/batch/Main.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/batch/Main.java index cd1e843ec6b..d5c465fbb38 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/batch/Main.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/batch/Main.java @@ -1368,6 +1368,7 @@ public static synchronized ResourceBundle getBundle(Locale locale) { private List addonReads = Collections.EMPTY_LIST; public Set rootModules = Collections.EMPTY_SET; public Set limitedModules; + private Map patchModules; // =()* public Locale compilerLocale; public CompilerOptions compilerOptions; // read-only @@ -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 bootclasspaths = new ArrayList<>(DEFAULT_SIZE_CLASSPATH); @@ -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; @@ -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); @@ -3402,9 +3422,22 @@ public CompilationUnit[] getCompilationUnits() { return null; }; } + String modName = this.modNames[i]; + if (modName == null && this.patchModules != null) { + // does this source file patch an existing module? + patchEntries: for (Entry entry : this.patchModules.entrySet()) { + StringTokenizer tokenizer = new StringTokenizer(entry.getValue(), File.pathSeparator); + while (tokenizer.hasMoreTokens()) { + if (fileName.startsWith(tokenizer.nextToken()+File.separator)) { + modName = entry.getKey(); + break patchEntries; + } + } + } + } units[i] = new CompilationUnit(null, fileName, encoding, this.destinationPaths[i], shouldIgnoreOptionalProblems(this.ignoreOptionalProblemsFromFolders, fileName.toCharArray()), - this.modNames[i], annotationPathProvider); + modName, annotationPathProvider); } } } diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/batch/messages.properties b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/batch/messages.properties index ca945c719a8..6047ccdf18f 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/batch/messages.properties +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/batch/messages.properties @@ -81,6 +81,8 @@ 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.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} diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ModuleCompilationTests.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ModuleCompilationTests.java index 82142625007..848dbe9c7c8 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ModuleCompilationTests.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ModuleCompilationTests.java @@ -5968,4 +5968,58 @@ public void testIssue2357_001() throws Exception { "}"; checkDisassembledClassFile(OUTPUT_DIR + File.separator + out + File.separator + "module-info.class", "module-info", expectedOutput); } + + public void testPatchModuleSingle() { + File outputDirectory = new File(OUTPUT_DIR); + Util.flushDirectoryContent(outputDirectory); + String out1 = OUTPUT_DIR + File.separator + "bin1"; + String src1 = OUTPUT_DIR + File.separator + "src1"; + List files = new ArrayList<>(); + writeFileCollecting(files, src1, + "module-info.java", + "module mod.one { \n" + // no exports! + "}"); + writeFileCollecting(files, src1 + File.separator + "test1", + "A.java", + "package test1;\n" + + "public class A {}"); + StringBuilder buffer = new StringBuilder(); + buffer.append("-d " + out1 ) + .append(" -9 ") + .append(" -proc:none ") + .append(" -classpath \"") + .append(Util.getJavaClassLibsAsString()) + .append("\" "); + runConformModuleTest( + files, + buffer, + "", + "", + false); + + files.clear(); + String out2 = "bin2"; + String src2 = OUTPUT_DIR + File.separator + "src2"; + writeFileCollecting(files, src2 + File.separator + "test2", + "B.java", + "package test2;\n" + + "import test1.A;\n" + + "class B extends A {}"); + buffer = new StringBuilder(); + buffer.append("-d " + OUTPUT_DIR + File.separator + out2 ) + .append(" -9 ") + .append(" -proc:none ") + .append(" --patch-module mod.one=\"").append(src2).append("\" ") + .append(" --module-path \"") + .append(Util.getJavaClassLibsAsString()) + .append(File.pathSeparatorChar) + .append(out1) + .append("\" "); + runConformModuleTest( + files, + buffer, + "", + "", + false); + } }