Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ public JavaModuleDescriptor getModuleDescriptor(Path modulePath) throws IOExcept
public JavaModuleDescriptor getModuleDescriptor(Path modulePath, JavaVersion jdkVersion) throws IOException {
JavaModuleDescriptor descriptor;
if (Files.isDirectory(modulePath)) {
try (InputStream in = Files.newInputStream(modulePath.resolve("module-info.class"))) {
Path moduleInfo = modulePath.resolve("module-info.class");
if (!Files.exists(moduleInfo)) {
Path versionedModuleInfo = findVersionedModuleInfo(modulePath, jdkVersion);
if (versionedModuleInfo != null) {
moduleInfo = versionedModuleInfo;
}
}
try (InputStream in = Files.newInputStream(moduleInfo)) {
descriptor = parse(in);
}
} else {
Expand Down Expand Up @@ -82,5 +89,25 @@ public JavaModuleDescriptor getModuleDescriptor(Path modulePath, JavaVersion jdk
return descriptor;
}

/**
* Finds the module descriptor of a multi-release output directory, which has no {@code module-info.class} at its
* root but under {@code META-INF/versions/<N>}, the way a multi-release jar does.
*
* @param directory the output directory
* @param jdkVersion the highest version to consider
* @return the path of the highest applicable versioned descriptor, or {@code null} if there is none
*/
static Path findVersionedModuleInfo(Path directory, JavaVersion jdkVersion) {
int javaVersion = Integer.parseInt(jdkVersion.asMajor().getValue(1));

for (int version = javaVersion; version >= 9; version--) {
Path moduleInfo = directory.resolve("META-INF/versions/" + version + "/module-info.class");
if (Files.exists(moduleInfo)) {
return moduleInfo;
}
}
return null;
}

abstract JavaModuleDescriptor parse(InputStream in) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.Set;

import org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor.JavaProvides;
import org.codehaus.plexus.languages.java.version.JavaVersion;

/**
* Maps artifacts to modules and analyzes the type of required modules
Expand Down Expand Up @@ -323,7 +324,10 @@ private ResolvePathResult resolvePath(
"'" + path + "' not allowed on the path, only outputDirectories and jars are accepted");
}

if (Files.isRegularFile(path) || Files.exists(path.resolve("module-info.class"))) {
if (Files.isRegularFile(path)
|| Files.exists(path.resolve("module-info.class"))
|| AbstractBinaryModuleInfoParser.findVersionedModuleInfo(path, JavaVersion.JAVA_SPECIFICATION_VERSION)
!= null) {
moduleDescriptor = binaryParser.getModuleDescriptor(path);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ void classicJar() throws Exception {
assertNull(descriptor);
}

@Test
void multiReleaseOutputDirectoryDescriptor() throws Exception {
JavaModuleDescriptor descriptor = parser.getModuleDescriptor(
Paths.get("src/test/test-data/dir.mr.descriptor/out"), JavaVersion.parse("17"));

assertNotNull(descriptor);
assertEquals("org.codehaus.plexus.languages.java.demo", descriptor.name());
assertFalse(descriptor.isAutomatic());
}

@Test
void outputDirectoryDescriptor() throws Exception {
JavaModuleDescriptor descriptor =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor.JavaRequires.JavaModifier;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnJre;
import org.junit.jupiter.api.condition.JRE;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
Expand Down Expand Up @@ -106,6 +108,28 @@ void manifestWithReflectRequires() throws Exception {
assertThat(result.getPathExceptions()).isEmpty();
}

@Test
@DisabledOnJre(value = JRE.JAVA_8, disabledReason = "Versioned descriptors are only read on Java 9+")
void multiReleaseDirDescriptor() throws Exception {
Path abc = Paths.get("src/test/test-data/dir.mr.descriptor/out");
JavaModuleDescriptor descriptor = JavaModuleDescriptor.newModule("base")
.requires("dir.mr.descriptor")
.build();
when(sourceParser.fromSourcePath(any(Path.class))).thenReturn(descriptor);
ResolvePathsRequest<Path> request =
ResolvePathsRequest.ofPaths(Collections.singletonList(abc)).setMainModuleDescriptor(mockModuleInfoJava);

when(asmParser.getModuleDescriptor(abc))
.thenReturn(JavaModuleDescriptor.newModule("dir.mr.descriptor").build());

ResolvePathsResult<Path> result = locationManager.resolvePaths(request);

assertThat(result.getModulepathElements()).hasSize(1);
assertThat(result.getModulepathElements().get(abc)).isEqualTo(ModuleNameSource.MODULEDESCRIPTOR);
assertThat(result.getClasspathElements()).isEmpty();
assertThat(result.getPathExceptions()).isEmpty();
}

@Test
void dirDescriptorWithReflectRequires() throws Exception {
Path abc = Paths.get("src/test/test-data/dir.descriptor/out");
Expand Down
Binary file not shown.
Loading