Skip to content

Commit 344a6b9

Browse files
committed
Eclipse compiler ignores the module source path set through file manager
1 parent 89e70ce commit 344a6b9

File tree

10 files changed

+382
-8
lines changed

10 files changed

+382
-8
lines changed
6.53 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
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.Filer;
25+
import javax.annotation.processing.Messager;
26+
import javax.annotation.processing.ProcessingEnvironment;
27+
import javax.annotation.processing.RoundEnvironment;
28+
import javax.annotation.processing.SupportedAnnotationTypes;
29+
import javax.annotation.processing.SupportedSourceVersion;
30+
import javax.lang.model.SourceVersion;
31+
import javax.lang.model.element.Element;
32+
import javax.lang.model.element.TypeElement;
33+
import org.eclipse.jdt.compiler.apt.tests.processors.base.BaseProcessor;
34+
35+
/**
36+
* A processor that explores the module import feature and related aspects.
37+
* To enable this processor, add
38+
* -Aorg.eclipse.jdt.compiler.apt.tests.processors.elements.ModuleImportProcessor to the command line.
39+
*/
40+
@SupportedAnnotationTypes("*")
41+
@SupportedSourceVersion(SourceVersion.RELEASE_8)
42+
public class ModuleImportProcessor extends BaseProcessor {
43+
boolean reportSuccessAlready = true;
44+
RoundEnvironment roundEnv = null;
45+
Messager _messager = null;
46+
Filer _filer = null;
47+
boolean isBinaryMode = false;
48+
String mode;
49+
@Override
50+
public synchronized void init(ProcessingEnvironment processingEnv) {
51+
super.init(processingEnv);
52+
_elementUtils = processingEnv.getElementUtils();
53+
_messager = processingEnv.getMessager();
54+
_filer = processingEnv.getFiler();
55+
}
56+
// Always return false from this processor, because it supports "*".
57+
// The return value does not signify success or failure!
58+
@Override
59+
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
60+
if (roundEnv.processingOver()) {
61+
return false;
62+
}
63+
64+
this.roundEnv = roundEnv;
65+
Map<String, String> options = processingEnv.getOptions();
66+
if (!options.containsKey(this.getClass().getName())) {
67+
// Disable this processor unless we are intentionally performing the test.
68+
return false;
69+
} else {
70+
try {
71+
if (options.containsKey("binary")) {
72+
this.isBinaryMode = true;
73+
this.mode = "binary";
74+
} else {
75+
this.mode = "source";
76+
}
77+
if (!invokeTestMethods(options)) {
78+
testAll();
79+
}
80+
if (this.reportSuccessAlready) {
81+
super.reportSuccess();
82+
}
83+
} catch (AssertionFailedError e) {
84+
super.reportError(getExceptionStackTrace(e));
85+
} catch (Throwable e) {
86+
e.printStackTrace();
87+
}
88+
}
89+
return false;
90+
}
91+
92+
private boolean invokeTestMethods(Map<String, String> options) throws Throwable {
93+
Method testMethod = null;
94+
Set<String> keys = options.keySet();
95+
boolean testsFound = false;
96+
for (String option : keys) {
97+
if (option.startsWith("test")) {
98+
try {
99+
testMethod = this.getClass().getDeclaredMethod(option, new Class[0]);
100+
if (testMethod != null) {
101+
testsFound = true;
102+
testMethod.invoke(this, new Object[0]);
103+
}
104+
} catch (InvocationTargetException e) {
105+
throw e.getCause();
106+
} catch (Exception e) {
107+
super.reportError(getExceptionStackTrace(e));
108+
}
109+
}
110+
}
111+
return testsFound;
112+
}
113+
114+
public void testAll() throws AssertionFailedError, IOException {
115+
test001();
116+
}
117+
118+
public void test001() throws IOException {
119+
Set<? extends Element> rootElements = this.roundEnv.getRootElements();
120+
Set<Element> rootModules = new TreeSet<>((o1, o2) -> o1.getSimpleName().toString().compareTo(o2.getSimpleName().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(cur);
138+
break;
139+
}
140+
}
141+
}
142+
assertEquals("Incorrect number of root modules", 3, rootModules.size());
143+
}
144+
public void test002() throws IOException {
145+
}
146+
@Override
147+
public void reportError(String msg) {
148+
throw new AssertionFailedError(msg);
149+
}
150+
private String getExceptionStackTrace(Throwable t) {
151+
StringBuilder buf = new StringBuilder(t.getMessage());
152+
StackTraceElement[] traces = t.getStackTrace();
153+
for (int i = 0; i < traces.length; i++) {
154+
StackTraceElement trace = traces[i];
155+
buf.append("\n\tat " + trace);
156+
if (i == 12)
157+
break; // Don't dump all stacks
158+
}
159+
return buf.toString();
160+
}
161+
public void assertEquals(String message, int expected, int actual) {
162+
if (expected == actual) {
163+
return;
164+
} else {
165+
reportError(message + ", expected " + expected + " but was " + actual);
166+
}
167+
}
168+
private static class AssertionFailedError extends Error {
169+
private static final long serialVersionUID = 1L;
170+
171+
public AssertionFailedError(String msg) {
172+
super(msg);
173+
}
174+
}
175+
}
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+
}

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)