Skip to content

#5 Use the Java 9 API to parse module descriptors #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 23, 2018
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
81 changes: 78 additions & 3 deletions plexus-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<version>2.21.0</version>
</plugin>
</plugins>
</pluginManagement>
Expand All @@ -31,6 +31,30 @@
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-component-metadata</artifactId>
<version>1.7.1</version>
<configuration>
<extractors>
<extractor>source</extractor>
</extractors>
</configuration>
</plugin>
<!-- Rerun unittests with the multirelease jar, cannot be done with exploded directory of classes -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.21.0</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>**/*Test.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
Expand All @@ -44,7 +68,7 @@
<dependency>
<groupId>com.thoughtworks.qdox</groupId>
<artifactId>qdox</artifactId>
<version>2.0-M7</version>
<version>2.0-M8</version>
</dependency>

<dependency>
Expand All @@ -69,7 +93,7 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.15.0</version>
<version>2.18.3</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -80,4 +104,55 @@
</dependency>
</dependencies>

<profiles>
<profile>
<id>jdk9</id>
<activation>
<jdk>[9,)</jdk>
</activation>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<executions>
<execution>
<id>jdk9</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<release>9</release>
<compileSourceRoots>
<compileSourceRoot>${project.basedir}/src/main/java9</compileSourceRoot>
</compileSourceRoots>
<outputDirectory>${project.build.outputDirectory}/META-INF/versions/9</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>default-jar</id>
<configuration>
<archive>
<manifestEntries>
<Multi-Release>true</Multi-Release>
</manifestEntries>
</archive>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
</profiles>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package org.codehaus.plexus.languages.java.jpms;

import java.io.IOException;
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;

public 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
{
JavaModuleDescriptor descriptor;
if ( Files.isDirectory( modulePath ) )
{
try ( InputStream in = Files.newInputStream( modulePath.resolve( "module-info.class" ) ) )
{
descriptor = parse( in );
}
}
else
{
try ( JarFile jarFile = new JarFile( modulePath.toFile() ) )
{
JarEntry moduleInfo;
if ( modulePath.toString().toLowerCase().endsWith( ".jmod" ) )
{
moduleInfo = jarFile.getJarEntry( "classes/module-info.class" );
}
else
{
moduleInfo = jarFile.getJarEntry( "module-info.class" );

if ( moduleInfo == null )
{
Manifest manifest = jarFile.getManifest();

if ( manifest != null && "true".equalsIgnoreCase( manifest.getMainAttributes().getValue( "Multi-Release" ) ) )
{
// look for multirelease descriptor
Enumeration<JarEntry> entryIter = jarFile.entries();
while ( entryIter.hasMoreElements() )
{
JarEntry entry = entryIter.nextElement();
if ( MRJAR_DESCRIPTOR.matcher( entry.getName() ).matches() )
{
moduleInfo = entry;
break;
}
}
}
}
}

if ( moduleInfo != null )
{
descriptor = parse( jarFile.getInputStream( moduleInfo ) );
}
else
{
descriptor = null;
}
}
}
return descriptor;
}

abstract JavaModuleDescriptor parse( InputStream in ) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,9 @@

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.regex.Pattern;

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
Expand All @@ -40,74 +33,14 @@
/**
* Extract information from module with ASM
*
*
* @author Robert Scholte
* @since 1.0.0
*/
public class AsmModuleInfoParser
implements ModuleInfoParser
class BinaryModuleInfoParser extends AbstractBinaryModuleInfoParser
{
private static final Pattern MRJAR_DESCRIPTOR = Pattern.compile( "META-INF/versions/[^/]+/module-info.class" );

@Override
public JavaModuleDescriptor getModuleDescriptor( Path modulePath )
throws IOException
{
JavaModuleDescriptor descriptor;
if ( Files.isDirectory( modulePath ) )
{
try ( InputStream in = Files.newInputStream( modulePath.resolve( "module-info.class" ) ) )
{
descriptor = parse( in );
}
}
else
{
try ( JarFile jarFile = new JarFile( modulePath.toFile() ) )
{
JarEntry moduleInfo;
if ( modulePath.toString().toLowerCase().endsWith( ".jmod" ) )
{
moduleInfo = jarFile.getJarEntry( "classes/module-info.class" );
}
else
{
moduleInfo = jarFile.getJarEntry( "module-info.class" );

if ( moduleInfo == null )
{
Manifest manifest = jarFile.getManifest();

if ( manifest != null && "true".equalsIgnoreCase( manifest.getMainAttributes().getValue( "Multi-Release" ) ) )
{
// look for multirelease descriptor
Enumeration<JarEntry> entryIter = jarFile.entries();
while ( entryIter.hasMoreElements() )
{
JarEntry entry = entryIter.nextElement();
if ( MRJAR_DESCRIPTOR.matcher( entry.getName() ).matches() )
{
moduleInfo = entry;
break;
}
}
}
}
}

if ( moduleInfo != null )
{
descriptor = parse( jarFile.getInputStream( moduleInfo ) );
}
else
{
descriptor = null;
}
}
}
return descriptor;
}

private JavaModuleDescriptor parse( InputStream in )
JavaModuleDescriptor parse( InputStream in )
throws IOException
{
final JavaModuleDescriptorWrapper wrapper = new JavaModuleDescriptorWrapper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Set;

/**
Expand Down Expand Up @@ -171,6 +172,40 @@ public static enum JavaModifier
{
STATIC
}

@Override
public int hashCode()
{
return Objects.hash( modifiers, name );
}

@Override
public boolean equals( Object obj )
{
if ( this == obj )
{
return true;
}
if ( obj == null )
{
return false;
}
if ( getClass() != obj.getClass() )
{
return false;
}

JavaRequires other = (JavaRequires) obj;
if ( !Objects.equals( modifiers, other.modifiers ) )
{
return false;
}
if ( !Objects.equals( name, other.name ) )
{
return false;
}
return true;
}
}

/**
Expand Down Expand Up @@ -206,5 +241,41 @@ public Set<String> targets()
{
return targets;
}

@Override
public int hashCode()
{
return Objects.hash( source, targets );
}

@Override
public boolean equals( Object obj )
{
if ( this == obj )
{
return true;
}
if ( obj == null )
{
return false;
}
if ( getClass() != obj.getClass() )
{
return false;
}

JavaExports other = (JavaExports) obj;
if ( !Objects.equals( source, other.source ) )
{
return false;
}
if ( !Objects.equals( targets, other.targets ) )
{
return false;
}
return true;
}


}
}
Loading