Skip to content

Commit

Permalink
[BUG] [JSR-199] ECJ cannot resolve JPMS modules if using a user-provided
Browse files Browse the repository at this point in the history
file manager

+ change to 1 ClasspathJsr199 per module
+ test with two module dependencies (one auto, one regular)

Fixes eclipse-jdt#958
  • Loading branch information
stephan-herrmann committed Dec 15, 2024
1 parent ced60ca commit c16af25
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module GH958_2 {
requires GH958_mod;
requires org.example.adder;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.example2;

import org.example.regular.Foo;
import org.example.adder.Adder;

public class Main {
Foo foo;
public static void main(String[] args) {
int a = 123;
int b = 456;
System.out.println(new Adder().add(a, b));
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -991,15 +991,41 @@ public static JavaFileManager demote(JavaFileManager fileManager) {
);
}
}
public void testGH954() throws Exception {
public void testGH958() throws Exception {
File classOutput = new File(_tmpFolder);
JavaCompiler compiler = new EclipseCompiler();
StandardJavaFileManager standardFileManager = compiler.getStandardFileManager(null, null, null);

List<File> classPath = List.of(new File("resources/module_locations/automod_GH954.jar"));
List<File> sourcePath = List.of(new File("resources/module_locations/GH954"));
standardFileManager.setLocation(StandardLocation.CLASS_PATH, classPath);
standardFileManager.setLocation(StandardLocation.MODULE_PATH, classPath);
List<File> modulePath = List.of(new File("resources/module_locations/GH958_automod.jar"));
List<File> sourcePath = List.of(new File("resources/module_locations/GH958"));
standardFileManager.setLocation(StandardLocation.MODULE_PATH, modulePath);
standardFileManager.setLocation(StandardLocation.SOURCE_PATH, sourcePath);
standardFileManager.setLocation(StandardLocation.CLASS_OUTPUT, List.of(classOutput));

// Make ECJ think we don't inherit StandardJavaFileManager by wrapping it.
JavaFileManager demotedFileManager = DemotingFileManagerProxy.demote(standardFileManager);
Iterable<JavaFileObject> compilationUnits = demotedFileManager.list(StandardLocation.SOURCE_PATH, "", Set.of(JavaFileObject.Kind.SOURCE), true);

CompilationTask task = compiler.getTask(
null,
demotedFileManager,
null,
List.of("--release", "11", "-verbose"),
List.of(),
compilationUnits
);

assertTrue(task.call());
}
public void testGH958_2modules() throws Exception {
File classOutput = new File(_tmpFolder);
JavaCompiler compiler = new EclipseCompiler();
StandardJavaFileManager standardFileManager = compiler.getStandardFileManager(null, null, null);

List<File> modulesPath = List.of(new File("resources/module_locations/GH958_automod.jar"),
new File("resources/module_locations/GH958_mod.jar"));
List<File> sourcePath = List.of(new File("resources/module_locations/GH958_2"));
standardFileManager.setLocation(StandardLocation.MODULE_PATH, modulesPath);
standardFileManager.setLocation(StandardLocation.SOURCE_PATH, sourcePath);
standardFileManager.setLocation(StandardLocation.CLASS_OUTPUT, List.of(classOutput));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.List;
import java.util.Set;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileManager.Location;
import javax.tools.JavaFileObject;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.internal.compiler.batch.FileSystem.Classpath;
Expand Down Expand Up @@ -160,20 +159,12 @@ public char[][][] findTypeNames(String aQualifiedPackageName, String moduleName)
public void initialize() throws IOException {
if (this.jrt != null) {
this.jrt.initialize();
} else if (this.location.isModuleOrientedLocation()) {
Iterable<Set<Location>> locationsForModules = this.fileManager.listLocationsForModules(this.location);
for (Set<Location> locs: locationsForModules) {
for (Location loc : locs) {
if (loc instanceof LocationWrapper wrapper) {
for (Path locPath : wrapper.getPaths()) {
File file = locPath.toFile();
IModule mod = ModuleFinder.scanForModule(this, file, null, true, null);
if (mod != null) {
return;
}
}
}
}
} else if (this.location instanceof LocationWrapper wrapper) {
for (Path locPath : wrapper.getPaths()) {
File file = locPath.toFile();
IModule mod = ModuleFinder.scanForModule(this, file, null, true, null);
if (mod != null)
return;
}
}
}
Expand Down Expand Up @@ -294,21 +285,8 @@ public boolean hasAnnotationFileFor(String qualifiedTypeName) {
public Collection<String> getModuleNames(Collection<String> limitModules) {
if (this.jrt != null)
return this.jrt.getModuleNames(limitModules);
if (this.location.isModuleOrientedLocation()) {
Set<String> moduleNames = new HashSet<>();
try {
for (Set<Location> locs : this.fileManager.listLocationsForModules(this.location)) {
for (Location loc : locs) {
String moduleName = this.fileManager.inferModuleName(loc);
if (moduleName != null)
moduleNames.add(moduleName);
}
}
return moduleNames;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (this.module != null) {
return Collections.singletonList(String.valueOf(this.module.name()));
}
return Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;
import javax.annotation.processing.Processor;
import javax.tools.Diagnostic;
Expand Down Expand Up @@ -727,12 +728,31 @@ protected void handleLocations() {
fileSystemClasspaths.add(classpath);
}
if (this.fileManager.hasLocation(StandardLocation.MODULE_SOURCE_PATH)) {
classpath = new ClasspathJsr199(this.fileManager, StandardLocation.MODULE_SOURCE_PATH);
fileSystemClasspaths.add(classpath);
// FIXME: ClasspathJsr199 doesn't really support source files
try {
Iterable<Set<Location>> locationsForModules = this.fileManager.listLocationsForModules(StandardLocation.MODULE_SOURCE_PATH);
for (Set<Location> locs: locationsForModules) {
for (Location loc : locs) {
classpath = new ClasspathJsr199(this.fileManager, loc);
fileSystemClasspaths.add(classpath);
}
}
} catch (IOException e) {
this.logger.logException(e);
}
}
if (this.fileManager.hasLocation(StandardLocation.MODULE_PATH)) {
classpath = new ClasspathJsr199(this.fileManager, StandardLocation.MODULE_PATH);
fileSystemClasspaths.add(classpath);
try {
Iterable<Set<Location>> locationsForModules = this.fileManager.listLocationsForModules(StandardLocation.MODULE_PATH);
for (Set<Location> locs: locationsForModules) {
for (Location loc : locs) {
classpath = new ClasspathJsr199(this.fileManager, loc);
fileSystemClasspaths.add(classpath);
}
}
} catch (IOException e) {
this.logger.logException(e);
}
}
classpath = new ClasspathJsr199(this.fileManager, StandardLocation.CLASS_PATH);
fileSystemClasspaths.add(classpath);
Expand Down

0 comments on commit c16af25

Please sign in to comment.