Skip to content

Commit 020a7f2

Browse files
committed
read of JPMS multi-release jars fails if a newer version than supported is present
1 parent 22d1bfb commit 020a7f2

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

plexus-java/src/main/java/org/codehaus/plexus/languages/java/jpms/AbstractBinaryModuleInfoParser.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,24 @@
2323
import java.io.InputStream;
2424
import java.nio.file.Files;
2525
import java.nio.file.Path;
26-
import java.util.Enumeration;
2726
import java.util.jar.JarEntry;
2827
import java.util.jar.JarFile;
2928
import java.util.jar.Manifest;
30-
import java.util.regex.Pattern;
29+
30+
import org.codehaus.plexus.languages.java.version.JavaVersion;
3131

3232
abstract class AbstractBinaryModuleInfoParser implements ModuleInfoParser
3333
{
34-
private static final Pattern MRJAR_DESCRIPTOR = Pattern.compile( "META-INF/versions/[^/]+/module-info.class" );
35-
3634
@Override
3735
public JavaModuleDescriptor getModuleDescriptor( Path modulePath )
3836
throws IOException
37+
{
38+
return getModuleDescriptor( modulePath, JavaVersion.JAVA_SPECIFICATION_VERSION );
39+
}
40+
41+
@Override
42+
public JavaModuleDescriptor getModuleDescriptor( Path modulePath, JavaVersion jdkVersion )
43+
throws IOException
3944
{
4045
JavaModuleDescriptor descriptor;
4146
if ( Files.isDirectory( modulePath ) )
@@ -64,12 +69,13 @@ public JavaModuleDescriptor getModuleDescriptor( Path modulePath )
6469

6570
if ( manifest != null && "true".equalsIgnoreCase( manifest.getMainAttributes().getValue( "Multi-Release" ) ) )
6671
{
67-
// look for multirelease descriptor
68-
Enumeration<JarEntry> entryIter = jarFile.entries();
69-
while ( entryIter.hasMoreElements() )
72+
int javaVersion = Integer.valueOf( jdkVersion.asMajor().getValue( 1 ) );
73+
74+
for ( int version = javaVersion; version >= 9; version-- )
7075
{
71-
JarEntry entry = entryIter.nextElement();
72-
if ( MRJAR_DESCRIPTOR.matcher( entry.getName() ).matches() )
76+
String resource = "META-INF/versions/" + version + "/module-info.class";
77+
JarEntry entry = jarFile.getJarEntry( resource );
78+
if ( entry != null )
7379
{
7480
moduleInfo = entry;
7581
break;

plexus-java/src/main/java/org/codehaus/plexus/languages/java/jpms/ModuleInfoParser.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.io.IOException;
2323
import java.nio.file.Path;
2424

25+
import org.codehaus.plexus.languages.java.version.JavaVersion;
26+
2527
/**
2628
* Extract information from the module-info file
2729
*
@@ -37,6 +39,16 @@ interface ModuleInfoParser
3739
* @return the module descriptor
3840
* @throws IOException when the file could not be parsed
3941
*/
40-
JavaModuleDescriptor getModuleDescriptor( Path modulePath )
42+
JavaModuleDescriptor getModuleDescriptor( Path modulePath ) throws IOException;
43+
44+
/**
45+
* Extracts the name from the module-info file
46+
*
47+
* @param modulePath the path to the {@code module-info.class}
48+
* @param javaVersion the java version in case of a multirelease jar
49+
* @return the module descriptor
50+
* @throws IOException when the file could not be parsed
51+
*/
52+
JavaModuleDescriptor getModuleDescriptor( Path modulePath, JavaVersion javaVersion )
4153
throws IOException;
4254
}

plexus-java/src/test/java/org/codehaus/plexus/languages/java/jpms/BinaryModuleInfoParserTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor.JavaExports;
3939
import org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor.JavaProvides;
4040
import org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor.JavaRequires;
41+
import org.codehaus.plexus.languages.java.version.JavaVersion;
4142
import org.junit.Test;
4243

4344
public class BinaryModuleInfoParserTest
@@ -67,7 +68,7 @@ public void testJarDescriptor() throws Exception
6768
@Test
6869
public void testMultiReleaseJarDescriptor() throws Exception
6970
{
70-
JavaModuleDescriptor descriptor = parser.getModuleDescriptor( Paths.get( "src/test/resources/jar.mr.descriptor/jloadr-1.0-SNAPSHOT.jar" ) );
71+
JavaModuleDescriptor descriptor = parser.getModuleDescriptor( Paths.get( "src/test/resources/jar.mr.descriptor/jloadr-1.0-SNAPSHOT.jar" ), JavaVersion.parse( "17" ) );
7172

7273
assertNotNull( descriptor);
7374
assertEquals( "de.adito.jloadr", descriptor.name() );

0 commit comments

Comments
 (0)