-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix package path computation in ClasspathScanner
Prior to this commit no trailing `/` character was appended to the computed package path. Now, except for the default package `""`, a `/` is appended to package path. This leads to corrected and documented behavior even if two modules start with the same name elements. Fixes #2500
- Loading branch information
Showing
9 changed files
with
54 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ | |
import java.util.function.Predicate; | ||
import java.util.logging.Level; | ||
import java.util.logging.LogRecord; | ||
import java.util.spi.ToolProvider; | ||
import java.util.stream.Collectors; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
@@ -180,22 +181,45 @@ void scanForClassesInPackage() { | |
|
||
@Test | ||
// #2500 | ||
void scanForClassesInPackageWithinModuleSharingNames() throws Exception { | ||
var jarfile = getClass().getResource("/[email protected]"); | ||
void scanForClassesInPackageWithinModulesSharingNamePrefix(@TempDir Path temp) throws Exception { | ||
var moduleSourcePath = Path.of(getClass().getResource("/modules-2500/").toURI()).toString(); | ||
run("javac", "--module", "foo,foo.bar", "--module-source-path", moduleSourcePath, "-d", temp.toString()); | ||
|
||
var module = "com.greetings"; | ||
checkModules2500(ModuleFinder.of(temp)); // exploded modules | ||
|
||
var foo = temp.resolve("foo.jar"); | ||
var bar = temp.resolve("foo.bar.jar"); | ||
run("jar", "--create", "--file", foo.toString(), "-C", temp.resolve("foo").toString(), "."); | ||
run("jar", "--create", "--file", bar.toString(), "-C", temp.resolve("foo.bar").toString(), "."); | ||
|
||
checkModules2500(ModuleFinder.of(foo, bar)); // jarred modules | ||
|
||
System.gc(); // required on Windows in order to release JAR file handles | ||
} | ||
|
||
private static int run(String tool, String... args) { | ||
return ToolProvider.findFirst(tool).orElseThrow().run(System.out, System.err, args); | ||
} | ||
|
||
private void checkModules2500(ModuleFinder finder) { | ||
var root = "foo.bar"; | ||
var before = ModuleFinder.of(); | ||
var finder = ModuleFinder.of(Path.of(jarfile.toURI())); | ||
var boot = ModuleLayer.boot(); | ||
var configuration = boot.configuration().resolveAndBind(before, finder, Set.of(module)); | ||
var configuration = boot.configuration().resolve(before, finder, Set.of(root)); | ||
var parent = ClassLoader.getPlatformClassLoader(); | ||
var layer = ModuleLayer.defineModulesWithOneLoader(configuration, List.of(boot), parent).layer(); | ||
|
||
var classpathScanner = new ClasspathScanner(() -> layer.findLoader(module), ReflectionUtils::tryToLoadClass); | ||
|
||
var classes = classpathScanner.scanForClassesInPackage("com.greetings", allClasses); | ||
var classNames = classes.stream().map(Class::getName).collect(Collectors.toList()); | ||
assertThat(classNames).hasSize(1).contains("com.greetings.Main"); | ||
var classpathScanner = new ClasspathScanner(() -> layer.findLoader(root), ReflectionUtils::tryToLoadClass); | ||
{ | ||
var classes = classpathScanner.scanForClassesInPackage("foo", allClasses); | ||
var classNames = classes.stream().map(Class::getName).collect(Collectors.toList()); | ||
assertThat(classNames).hasSize(2).contains("foo.Foo", "foo.bar.FooBar"); | ||
} | ||
{ | ||
var classes = classpathScanner.scanForClassesInPackage("foo.bar", allClasses); | ||
var classNames = classes.stream().map(Class::getName).collect(Collectors.toList()); | ||
assertThat(classNames).hasSize(1).contains("foo.bar.FooBar"); | ||
} | ||
} | ||
|
||
@Test | ||
|
Binary file not shown.
3 changes: 3 additions & 0 deletions
3
platform-tests/src/test/resources/modules-2500/foo.bar/FooBar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package foo.bar; | ||
|
||
public class FooBar {} |
3 changes: 3 additions & 0 deletions
3
platform-tests/src/test/resources/modules-2500/foo.bar/module-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
open module foo.bar { | ||
requires foo; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package foo; | ||
|
||
public class Foo {} |
3 changes: 3 additions & 0 deletions
3
platform-tests/src/test/resources/modules-2500/foo/module-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module foo { | ||
exports foo; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters