Skip to content

Commit 0d4450b

Browse files
authored
Eclipse compiler ignores the module source path set through file manager #3766 (#4544)
* Do not report multiple module-info being present in the list of CUs when MODULE_SOURCE_PATH is set on the file manager #4544
1 parent 49983b0 commit 0d4450b

File tree

13 files changed

+396
-10
lines changed

13 files changed

+396
-10
lines changed
11.5 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024 IBM Corporation.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* IBM Corporation - initial API and implementation
13+
*******************************************************************************/
14+
15+
package org.eclipse.jdt.compiler.apt.tests.processors.elements;
16+
17+
import java.io.IOException;
18+
import java.lang.reflect.InvocationTargetException;
19+
import java.lang.reflect.Method;
20+
import java.util.LinkedHashSet;
21+
import java.util.Map;
22+
import java.util.Set;
23+
import java.util.TreeSet;
24+
import javax.annotation.processing.ProcessingEnvironment;
25+
import javax.annotation.processing.RoundEnvironment;
26+
import javax.annotation.processing.SupportedAnnotationTypes;
27+
import javax.annotation.processing.SupportedSourceVersion;
28+
import javax.lang.model.SourceVersion;
29+
import javax.lang.model.element.Element;
30+
import javax.lang.model.element.ModuleElement;
31+
import javax.lang.model.element.TypeElement;
32+
import org.eclipse.jdt.compiler.apt.tests.processors.base.BaseProcessor;
33+
34+
/**
35+
* A processor that explores the module import feature and related aspects.
36+
* To enable this processor, add
37+
* -Aorg.eclipse.jdt.compiler.apt.tests.processors.elements.ModuleImportProcessor to the command line.
38+
*/
39+
@SupportedAnnotationTypes("*")
40+
@SupportedSourceVersion(SourceVersion.RELEASE_8)
41+
public class ModuleImportProcessor extends BaseProcessor {
42+
boolean reportSuccessAlready = true;
43+
RoundEnvironment roundEnv = null;
44+
String mode;
45+
@Override
46+
public synchronized void init(ProcessingEnvironment processingEnv) {
47+
super.init(processingEnv);
48+
_elementUtils = processingEnv.getElementUtils();
49+
}
50+
// Always return false from this processor, because it supports "*".
51+
// The return value does not signify success or failure!
52+
@Override
53+
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
54+
if (roundEnv.processingOver()) {
55+
return false;
56+
}
57+
58+
this.roundEnv = roundEnv;
59+
Map<String, String> options = processingEnv.getOptions();
60+
if (!options.containsKey(this.getClass().getName())) {
61+
// Disable this processor unless we are intentionally performing the test.
62+
return false;
63+
} else {
64+
try {
65+
if (!invokeTestMethods(options)) {
66+
testAll();
67+
}
68+
if (this.reportSuccessAlready) {
69+
super.reportSuccess();
70+
}
71+
} catch (AssertionFailedError e) {
72+
super.reportError(getExceptionStackTrace(e));
73+
} catch (Throwable e) {
74+
e.printStackTrace();
75+
}
76+
}
77+
return false;
78+
}
79+
80+
private boolean invokeTestMethods(Map<String, String> options) throws Throwable {
81+
Method testMethod = null;
82+
Set<String> keys = options.keySet();
83+
boolean testsFound = false;
84+
for (String option : keys) {
85+
if (option.startsWith("test")) {
86+
try {
87+
testMethod = this.getClass().getDeclaredMethod(option, new Class[0]);
88+
if (testMethod != null) {
89+
testsFound = true;
90+
testMethod.invoke(this, new Object[0]);
91+
}
92+
super.reportSuccess();
93+
} catch (InvocationTargetException e) {
94+
throw e.getCause();
95+
} catch (Exception e) {
96+
super.reportError(getExceptionStackTrace(e));
97+
}
98+
}
99+
}
100+
return testsFound;
101+
}
102+
103+
public void testAll() throws AssertionFailedError, IOException {
104+
}
105+
106+
public void test001() throws IOException {
107+
Set<ModuleElement> rootModules = getRootModules();
108+
assertEquals("Incorrect number of root modules", 3, rootModules.size());
109+
}
110+
public void test002() throws IOException {
111+
Set<ModuleElement> rootModules = getRootModules();
112+
assertEquals("Incorrect number of root modules", 1, rootModules.size());
113+
}
114+
public void test003() throws IOException {
115+
Set<ModuleElement> rootModules = getRootModules();
116+
assertEquals("Incorrect number of root modules", 1, rootModules.size());
117+
}
118+
private Set<ModuleElement> getRootModules() {
119+
Set<? extends Element> rootElements = this.roundEnv.getRootElements();
120+
Set<ModuleElement> rootModules = new TreeSet<>((o1, o2) -> o1.getQualifiedName().toString().compareTo(o2.getQualifiedName().toString()));
121+
for (Element root : rootElements) {
122+
Element cur = root;
123+
LinkedHashSet<Element> stack = new LinkedHashSet<>();
124+
stack.add(root);
125+
126+
for (;;) {
127+
Element parent = cur.getEnclosingElement();
128+
129+
if (stack.contains(parent)) {
130+
break;
131+
}
132+
133+
stack.add(parent);
134+
if (parent != null) {
135+
cur = parent;
136+
} else {
137+
rootModules.add((ModuleElement) cur);
138+
break;
139+
}
140+
}
141+
}
142+
return rootModules;
143+
}
144+
145+
@Override
146+
public void reportError(String msg) {
147+
throw new AssertionFailedError(msg);
148+
}
149+
private String getExceptionStackTrace(Throwable t) {
150+
StringBuilder buf = new StringBuilder(t.getMessage());
151+
StackTraceElement[] traces = t.getStackTrace();
152+
for (int i = 0; i < traces.length; i++) {
153+
StackTraceElement trace = traces[i];
154+
buf.append("\n\tat " + trace);
155+
if (i == 12)
156+
break; // Don't dump all stacks
157+
}
158+
return buf.toString();
159+
}
160+
public void assertEquals(String message, int expected, int actual) {
161+
if (expected == actual) {
162+
return;
163+
} else {
164+
reportError(message + ", expected " + expected + " but was " + actual);
165+
}
166+
}
167+
private static class AssertionFailedError extends Error {
168+
private static final long serialVersionUID = 1L;
169+
170+
public AssertionFailedError(String msg) {
171+
super(msg);
172+
}
173+
}
174+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module mod001A.mod001A.mod001A {
2+
requires transitive mod001A;
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package mod001A;
2+
3+
public class mod001A {
4+
public static int a = 1;
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module mod001A {
2+
exports mod001A;
3+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module module.mod001mod {
2+
exports x.y.mod001;
3+
requires transitive mod001A.mod001A.mod001A;
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package x.y.mod001;
2+
3+
public class mod001 {
4+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package mod001A;
2+
3+
public class mod001A {
4+
public static int a = 1;
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module mod001A {
2+
exports mod001A;
3+
}

org.eclipse.jdt.compiler.apt.tests/src/org/eclipse/jdt/compiler/apt/tests/BatchTestUtils.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public class BatchTestUtils {
6767

6868
private static String _tmpSrcFolderName;
6969
private static File _tmpSrcDir;
70-
private static String _tmpBinFolderName;
70+
static String _tmpBinFolderName;
7171
private static File _tmpBinDir;
7272
public static String _tmpGenFolderName;
7373
private static File _tmpGenDir;
@@ -152,16 +152,16 @@ public static void compileTree(JavaCompiler compiler, List<String> options, File
152152
}
153153

154154
public static void compileInModuleMode(JavaCompiler compiler, List<String> options, String processor,
155-
File targetFolder, DiagnosticListener<? super JavaFileObject> listener, boolean multiModule) throws IOException {
156-
compileInModuleMode(compiler, options, processor, targetFolder, listener, multiModule, true);
155+
File moduleSrcFolder, DiagnosticListener<? super JavaFileObject> listener, boolean multiModule) throws IOException {
156+
compileInModuleMode(compiler, options, processor, moduleSrcFolder, listener, multiModule, true);
157157
}
158158
public static void compileInModuleMode(JavaCompiler compiler, List<String> options, String processor,
159-
File targetFolder, DiagnosticListener<? super JavaFileObject> listener, boolean multiModule, boolean processBinariesAgain) throws IOException {
159+
File moduleSrcFolder, DiagnosticListener<? super JavaFileObject> listener, boolean multiModule, boolean processBinariesAgain) throws IOException {
160160
StandardJavaFileManager manager = compiler.getStandardFileManager(null, Locale.getDefault(), Charset.defaultCharset());
161161
Iterable<? extends File> location = manager.getLocation(StandardLocation.CLASS_PATH);
162162
// create new list containing inputfile
163163
List<File> files = new ArrayList<>();
164-
findFilesUnder(targetFolder, files);
164+
findFilesUnder(moduleSrcFolder, files);
165165
files.sort(new Comparator<File>() {
166166
@Override
167167
public int compare(File f1, File f2) {
@@ -182,7 +182,7 @@ public int compare(File f1, File f2) {
182182
copyOptions.add(_tmpBinFolderName);
183183
copyOptions.add("-s");
184184
copyOptions.add(_tmpGenFolderName);
185-
addModuleProcessorPath(copyOptions, targetFolder.getAbsolutePath(), multiModule);
185+
addModuleProcessorPath(copyOptions, moduleSrcFolder.getAbsolutePath(), multiModule);
186186
copyOptions.add("-XprintRounds");
187187
CompilationTask task = compiler.getTask(printWriter, manager, listener, copyOptions, null, units);
188188
Boolean result = task.call();
@@ -527,7 +527,7 @@ public static void init()
527527
junit.framework.TestCase.assertNotNull("No Eclipse compiler found", _eclipseCompiler);
528528
}
529529

530-
private static void addProcessorPaths(List<String> options, boolean useJLS8Processors, boolean addToNormalClasspath) {
530+
public static void addProcessorPaths(List<String> options, boolean useJLS8Processors, boolean addToNormalClasspath) {
531531
String path = useJLS8Processors ? _jls8ProcessorJarPath : _processorJarPath;
532532
if (addToNormalClasspath) {
533533
options.add("-cp");
@@ -536,7 +536,7 @@ private static void addProcessorPaths(List<String> options, boolean useJLS8Proce
536536
options.add("-processorpath");
537537
options.add(path);
538538
}
539-
private static void addModuleProcessorPath(List<String> options, String srcFolderName, boolean multiModule) {
539+
public static void addModuleProcessorPath(List<String> options, String srcFolderName, boolean multiModule) {
540540
options.add("--processor-module-path");
541541
options.add(_jls8ProcessorJarPath);
542542
options.add("--module-path");

0 commit comments

Comments
 (0)