Skip to content

Commit 2d78c7e

Browse files
authored
#5 Use the Java 9 API to parse module descriptors (#13)
This closes #5
1 parent 616a5ee commit 2d78c7e

14 files changed

+373
-109
lines changed

plexus-java/pom.xml

+78-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<plugin>
2323
<groupId>org.apache.maven.plugins</groupId>
2424
<artifactId>maven-surefire-plugin</artifactId>
25-
<version>2.20</version>
25+
<version>2.21.0</version>
2626
</plugin>
2727
</plugins>
2828
</pluginManagement>
@@ -31,6 +31,30 @@
3131
<groupId>org.codehaus.plexus</groupId>
3232
<artifactId>plexus-component-metadata</artifactId>
3333
<version>1.7.1</version>
34+
<configuration>
35+
<extractors>
36+
<extractor>source</extractor>
37+
</extractors>
38+
</configuration>
39+
</plugin>
40+
<!-- Rerun unittests with the multirelease jar, cannot be done with exploded directory of classes -->
41+
<plugin>
42+
<groupId>org.apache.maven.plugins</groupId>
43+
<artifactId>maven-failsafe-plugin</artifactId>
44+
<version>2.21.0</version>
45+
<executions>
46+
<execution>
47+
<goals>
48+
<goal>integration-test</goal>
49+
<goal>verify</goal>
50+
</goals>
51+
</execution>
52+
</executions>
53+
<configuration>
54+
<includes>
55+
<include>**/*Test.java</include>
56+
</includes>
57+
</configuration>
3458
</plugin>
3559
</plugins>
3660
</build>
@@ -44,7 +68,7 @@
4468
<dependency>
4569
<groupId>com.thoughtworks.qdox</groupId>
4670
<artifactId>qdox</artifactId>
47-
<version>2.0-M7</version>
71+
<version>2.0-M8</version>
4872
</dependency>
4973

5074
<dependency>
@@ -69,7 +93,7 @@
6993
<dependency>
7094
<groupId>org.mockito</groupId>
7195
<artifactId>mockito-core</artifactId>
72-
<version>2.15.0</version>
96+
<version>2.18.3</version>
7397
<scope>test</scope>
7498
</dependency>
7599
<dependency>
@@ -80,4 +104,55 @@
80104
</dependency>
81105
</dependencies>
82106

107+
<profiles>
108+
<profile>
109+
<id>jdk9</id>
110+
<activation>
111+
<jdk>[9,)</jdk>
112+
</activation>
113+
<build>
114+
<pluginManagement>
115+
<plugins>
116+
<plugin>
117+
<groupId>org.apache.maven.plugins</groupId>
118+
<artifactId>maven-compiler-plugin</artifactId>
119+
<version>3.7.0</version>
120+
<executions>
121+
<execution>
122+
<id>jdk9</id>
123+
<goals>
124+
<goal>compile</goal>
125+
</goals>
126+
<configuration>
127+
<release>9</release>
128+
<compileSourceRoots>
129+
<compileSourceRoot>${project.basedir}/src/main/java9</compileSourceRoot>
130+
</compileSourceRoots>
131+
<outputDirectory>${project.build.outputDirectory}/META-INF/versions/9</outputDirectory>
132+
</configuration>
133+
</execution>
134+
</executions>
135+
</plugin>
136+
<plugin>
137+
<groupId>org.apache.maven.plugins</groupId>
138+
<artifactId>maven-jar-plugin</artifactId>
139+
<executions>
140+
<execution>
141+
<id>default-jar</id>
142+
<configuration>
143+
<archive>
144+
<manifestEntries>
145+
<Multi-Release>true</Multi-Release>
146+
</manifestEntries>
147+
</archive>
148+
</configuration>
149+
</execution>
150+
</executions>
151+
</plugin>
152+
</plugins>
153+
</pluginManagement>
154+
</build>
155+
</profile>
156+
</profiles>
157+
83158
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package org.codehaus.plexus.languages.java.jpms;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.nio.file.Files;
6+
import java.nio.file.Path;
7+
import java.util.Enumeration;
8+
import java.util.jar.JarEntry;
9+
import java.util.jar.JarFile;
10+
import java.util.jar.Manifest;
11+
import java.util.regex.Pattern;
12+
13+
public abstract class AbstractBinaryModuleInfoParser implements ModuleInfoParser
14+
{
15+
private static final Pattern MRJAR_DESCRIPTOR = Pattern.compile( "META-INF/versions/[^/]+/module-info.class" );
16+
17+
@Override
18+
public JavaModuleDescriptor getModuleDescriptor( Path modulePath )
19+
throws IOException
20+
{
21+
JavaModuleDescriptor descriptor;
22+
if ( Files.isDirectory( modulePath ) )
23+
{
24+
try ( InputStream in = Files.newInputStream( modulePath.resolve( "module-info.class" ) ) )
25+
{
26+
descriptor = parse( in );
27+
}
28+
}
29+
else
30+
{
31+
try ( JarFile jarFile = new JarFile( modulePath.toFile() ) )
32+
{
33+
JarEntry moduleInfo;
34+
if ( modulePath.toString().toLowerCase().endsWith( ".jmod" ) )
35+
{
36+
moduleInfo = jarFile.getJarEntry( "classes/module-info.class" );
37+
}
38+
else
39+
{
40+
moduleInfo = jarFile.getJarEntry( "module-info.class" );
41+
42+
if ( moduleInfo == null )
43+
{
44+
Manifest manifest = jarFile.getManifest();
45+
46+
if ( manifest != null && "true".equalsIgnoreCase( manifest.getMainAttributes().getValue( "Multi-Release" ) ) )
47+
{
48+
// look for multirelease descriptor
49+
Enumeration<JarEntry> entryIter = jarFile.entries();
50+
while ( entryIter.hasMoreElements() )
51+
{
52+
JarEntry entry = entryIter.nextElement();
53+
if ( MRJAR_DESCRIPTOR.matcher( entry.getName() ).matches() )
54+
{
55+
moduleInfo = entry;
56+
break;
57+
}
58+
}
59+
}
60+
}
61+
}
62+
63+
if ( moduleInfo != null )
64+
{
65+
descriptor = parse( jarFile.getInputStream( moduleInfo ) );
66+
}
67+
else
68+
{
69+
descriptor = null;
70+
}
71+
}
72+
}
73+
return descriptor;
74+
}
75+
76+
abstract JavaModuleDescriptor parse( InputStream in ) throws IOException;
77+
}

plexus-java/src/main/java/org/codehaus/plexus/languages/java/jpms/AsmModuleInfoParser.java renamed to plexus-java/src/main/java/org/codehaus/plexus/languages/java/jpms/BinaryModuleInfoParser.java

+3-70
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,9 @@
2121

2222
import java.io.IOException;
2323
import java.io.InputStream;
24-
import java.nio.file.Files;
25-
import java.nio.file.Path;
2624
import java.util.Arrays;
2725
import java.util.Collections;
28-
import java.util.Enumeration;
2926
import java.util.HashSet;
30-
import java.util.jar.JarEntry;
31-
import java.util.jar.JarFile;
32-
import java.util.jar.Manifest;
33-
import java.util.regex.Pattern;
3427

3528
import org.objectweb.asm.ClassReader;
3629
import org.objectweb.asm.ClassVisitor;
@@ -40,74 +33,14 @@
4033
/**
4134
* Extract information from module with ASM
4235
*
36+
*
4337
* @author Robert Scholte
4438
* @since 1.0.0
4539
*/
46-
public class AsmModuleInfoParser
47-
implements ModuleInfoParser
40+
class BinaryModuleInfoParser extends AbstractBinaryModuleInfoParser
4841
{
49-
private static final Pattern MRJAR_DESCRIPTOR = Pattern.compile( "META-INF/versions/[^/]+/module-info.class" );
50-
5142
@Override
52-
public JavaModuleDescriptor getModuleDescriptor( Path modulePath )
53-
throws IOException
54-
{
55-
JavaModuleDescriptor descriptor;
56-
if ( Files.isDirectory( modulePath ) )
57-
{
58-
try ( InputStream in = Files.newInputStream( modulePath.resolve( "module-info.class" ) ) )
59-
{
60-
descriptor = parse( in );
61-
}
62-
}
63-
else
64-
{
65-
try ( JarFile jarFile = new JarFile( modulePath.toFile() ) )
66-
{
67-
JarEntry moduleInfo;
68-
if ( modulePath.toString().toLowerCase().endsWith( ".jmod" ) )
69-
{
70-
moduleInfo = jarFile.getJarEntry( "classes/module-info.class" );
71-
}
72-
else
73-
{
74-
moduleInfo = jarFile.getJarEntry( "module-info.class" );
75-
76-
if ( moduleInfo == null )
77-
{
78-
Manifest manifest = jarFile.getManifest();
79-
80-
if ( manifest != null && "true".equalsIgnoreCase( manifest.getMainAttributes().getValue( "Multi-Release" ) ) )
81-
{
82-
// look for multirelease descriptor
83-
Enumeration<JarEntry> entryIter = jarFile.entries();
84-
while ( entryIter.hasMoreElements() )
85-
{
86-
JarEntry entry = entryIter.nextElement();
87-
if ( MRJAR_DESCRIPTOR.matcher( entry.getName() ).matches() )
88-
{
89-
moduleInfo = entry;
90-
break;
91-
}
92-
}
93-
}
94-
}
95-
}
96-
97-
if ( moduleInfo != null )
98-
{
99-
descriptor = parse( jarFile.getInputStream( moduleInfo ) );
100-
}
101-
else
102-
{
103-
descriptor = null;
104-
}
105-
}
106-
}
107-
return descriptor;
108-
}
109-
110-
private JavaModuleDescriptor parse( InputStream in )
43+
JavaModuleDescriptor parse( InputStream in )
11144
throws IOException
11245
{
11346
final JavaModuleDescriptorWrapper wrapper = new JavaModuleDescriptorWrapper();

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

+71
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.util.Collections;
2323
import java.util.LinkedHashSet;
24+
import java.util.Objects;
2425
import java.util.Set;
2526

2627
/**
@@ -171,6 +172,40 @@ public static enum JavaModifier
171172
{
172173
STATIC
173174
}
175+
176+
@Override
177+
public int hashCode()
178+
{
179+
return Objects.hash( modifiers, name );
180+
}
181+
182+
@Override
183+
public boolean equals( Object obj )
184+
{
185+
if ( this == obj )
186+
{
187+
return true;
188+
}
189+
if ( obj == null )
190+
{
191+
return false;
192+
}
193+
if ( getClass() != obj.getClass() )
194+
{
195+
return false;
196+
}
197+
198+
JavaRequires other = (JavaRequires) obj;
199+
if ( !Objects.equals( modifiers, other.modifiers ) )
200+
{
201+
return false;
202+
}
203+
if ( !Objects.equals( name, other.name ) )
204+
{
205+
return false;
206+
}
207+
return true;
208+
}
174209
}
175210

176211
/**
@@ -206,5 +241,41 @@ public Set<String> targets()
206241
{
207242
return targets;
208243
}
244+
245+
@Override
246+
public int hashCode()
247+
{
248+
return Objects.hash( source, targets );
249+
}
250+
251+
@Override
252+
public boolean equals( Object obj )
253+
{
254+
if ( this == obj )
255+
{
256+
return true;
257+
}
258+
if ( obj == null )
259+
{
260+
return false;
261+
}
262+
if ( getClass() != obj.getClass() )
263+
{
264+
return false;
265+
}
266+
267+
JavaExports other = (JavaExports) obj;
268+
if ( !Objects.equals( source, other.source ) )
269+
{
270+
return false;
271+
}
272+
if ( !Objects.equals( targets, other.targets ) )
273+
{
274+
return false;
275+
}
276+
return true;
277+
}
278+
279+
209280
}
210281
}

0 commit comments

Comments
 (0)