Skip to content
Closed
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 @@ -23,19 +23,24 @@
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.regex.Pattern;

import org.codehaus.plexus.languages.java.version.JavaVersion;

abstract class AbstractBinaryModuleInfoParser implements ModuleInfoParser
{
private static final Pattern MRJAR_DESCRIPTOR = Pattern.compile( "META-INF/versions/[^/]+/module-info.class" );

@Override
public JavaModuleDescriptor getModuleDescriptor( Path modulePath )
throws IOException
{
return getModuleDescriptor( modulePath, JavaVersion.JAVA_SPECIFICATION_VERSION );
}

@Override
public JavaModuleDescriptor getModuleDescriptor( Path modulePath, JavaVersion jdkVersion )
throws IOException
{
JavaModuleDescriptor descriptor;
if ( Files.isDirectory( modulePath ) )
Expand Down Expand Up @@ -64,12 +69,13 @@ public JavaModuleDescriptor getModuleDescriptor( Path modulePath )

if ( manifest != null && "true".equalsIgnoreCase( manifest.getMainAttributes().getValue( "Multi-Release" ) ) )
{
// look for multirelease descriptor
Enumeration<JarEntry> entryIter = jarFile.entries();
while ( entryIter.hasMoreElements() )
int javaVersion = Integer.valueOf( jdkVersion.asMajor().getValue( 1 ) );

for ( int version = javaVersion; version >= 9; version-- )
{
JarEntry entry = entryIter.nextElement();
if ( MRJAR_DESCRIPTOR.matcher( entry.getName() ).matches() )
String resource = "META-INF/versions/" + version + "/module-info.class";
JarEntry entry = jarFile.getJarEntry( resource );
if ( entry != null )
{
moduleInfo = entry;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.io.IOException;
import java.nio.file.Path;

import org.codehaus.plexus.languages.java.version.JavaVersion;

/**
* Extract information from the module-info file
*
Expand All @@ -37,6 +39,16 @@ interface ModuleInfoParser
* @return the module descriptor
* @throws IOException when the file could not be parsed
*/
JavaModuleDescriptor getModuleDescriptor( Path modulePath )
JavaModuleDescriptor getModuleDescriptor( Path modulePath ) throws IOException;

/**
* Extracts the name from the module-info file
*
* @param modulePath the path to the {@code module-info.class}
* @param javaVersion the java version in case of a multirelease jar
* @return the module descriptor
* @throws IOException when the file could not be parsed
*/
JavaModuleDescriptor getModuleDescriptor( Path modulePath, JavaVersion javaVersion )
throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor.JavaExports;
import org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor.JavaProvides;
import org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor.JavaRequires;
import org.codehaus.plexus.languages.java.version.JavaVersion;
import org.junit.Test;

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

assertNotNull( descriptor);
assertEquals( "de.adito.jloadr", descriptor.name() );
Expand Down