Skip to content

Commit 8b095c1

Browse files
committed
#5 Use the Java 9 API to parse module descriptors
1 parent 616a5ee commit 8b095c1

File tree

9 files changed

+132
-22
lines changed

9 files changed

+132
-22
lines changed

plexus-java/pom.xml

+57-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
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>
3439
</plugin>
3540
</plugins>
3641
</build>
@@ -44,7 +49,7 @@
4449
<dependency>
4550
<groupId>com.thoughtworks.qdox</groupId>
4651
<artifactId>qdox</artifactId>
47-
<version>2.0-M7</version>
52+
<version>2.0-M8</version>
4853
</dependency>
4954

5055
<dependency>
@@ -80,4 +85,55 @@
8085
</dependency>
8186
</dependencies>
8287

88+
<profiles>
89+
<profile>
90+
<id>jdk9</id>
91+
<activation>
92+
<jdk>[9,)</jdk>
93+
</activation>
94+
<build>
95+
<pluginManagement>
96+
<plugins>
97+
<plugin>
98+
<groupId>org.apache.maven.plugins</groupId>
99+
<artifactId>maven-compiler-plugin</artifactId>
100+
<version>3.7.0</version>
101+
<executions>
102+
<execution>
103+
<id>jdk9</id>
104+
<goals>
105+
<goal>compile</goal>
106+
</goals>
107+
<configuration>
108+
<release>9</release>
109+
<compileSourceRoots>
110+
<compileSourceRoot>${project.basedir}/src/main/java9</compileSourceRoot>
111+
</compileSourceRoots>
112+
<outputDirectory>${project.build.outputDirectory}/META-INF/versions/9</outputDirectory>
113+
</configuration>
114+
</execution>
115+
</executions>
116+
</plugin>
117+
<plugin>
118+
<groupId>org.apache.maven.plugins</groupId>
119+
<artifactId>maven-jar-plugin</artifactId>
120+
<executions>
121+
<execution>
122+
<id>default-jar</id>
123+
<configuration>
124+
<archive>
125+
<manifestEntries>
126+
<Multi-Release>true</Multi-Release>
127+
</manifestEntries>
128+
</archive>
129+
</configuration>
130+
</execution>
131+
</executions>
132+
</plugin>
133+
</plugins>
134+
</pluginManagement>
135+
</build>
136+
</profile>
137+
</profiles>
138+
83139
</project>

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@
4040
/**
4141
* Extract information from module with ASM
4242
*
43+
*
4344
* @author Robert Scholte
4445
* @since 1.0.0
4546
*/
46-
public class AsmModuleInfoParser
47-
implements ModuleInfoParser
47+
class BinaryModuleInfoParser implements ModuleInfoParser
4848
{
4949
private static final Pattern MRJAR_DESCRIPTOR = Pattern.compile( "META-INF/versions/[^/]+/module-info.class" );
5050

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

+10-10
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,20 @@
4545
@Component( role = LocationManager.class )
4646
public class LocationManager
4747
{
48-
private ModuleInfoParser asmParser;
48+
private ModuleInfoParser binaryParser;
4949

50-
private QDoxModuleInfoParser qdoxParser;
50+
private SourceModuleInfoParser sourceParser;
5151

5252
public LocationManager()
5353
{
54-
this.asmParser = new AsmModuleInfoParser();
55-
this.qdoxParser = new QDoxModuleInfoParser();
54+
this.binaryParser = new BinaryModuleInfoParser();
55+
this.sourceParser = new SourceModuleInfoParser();
5656
}
5757

58-
LocationManager( ModuleInfoParser asmParser, QDoxModuleInfoParser qdoxParser )
58+
LocationManager( ModuleInfoParser binaryParser, SourceModuleInfoParser sourceParser )
5959
{
60-
this.asmParser = asmParser;
61-
this.qdoxParser = qdoxParser;
60+
this.binaryParser = binaryParser;
61+
this.sourceParser = sourceParser;
6262
}
6363

6464
/**
@@ -84,11 +84,11 @@ public <T> ResolvePathsResult<T> resolvePaths( ResolvePathsRequest<T> request )
8484
{
8585
if ( descriptorPath.endsWith( "module-info.java" ) )
8686
{
87-
mainModuleDescriptor = qdoxParser.fromSourcePath( descriptorPath );
87+
mainModuleDescriptor = sourceParser.fromSourcePath( descriptorPath );
8888
}
8989
else if ( descriptorPath.endsWith( "module-info.class" ) )
9090
{
91-
mainModuleDescriptor = asmParser.getModuleDescriptor( descriptorPath.getParent() );
91+
mainModuleDescriptor = binaryParser.getModuleDescriptor( descriptorPath.getParent() );
9292
}
9393
else
9494
{
@@ -124,7 +124,7 @@ else if ( descriptorPath.endsWith( "module-info.class" ) )
124124
{
125125
try
126126
{
127-
moduleDescriptor = asmParser.getModuleDescriptor( path );
127+
moduleDescriptor = binaryParser.getModuleDescriptor( path );
128128
}
129129
catch( IOException e )
130130
{

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

-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
*/
3131
public interface ModuleInfoParser
3232
{
33-
34-
3533
/**
3634
* Extracts the name from the module-info file
3735
*

plexus-java/src/main/java/org/codehaus/plexus/languages/java/jpms/QDoxModuleInfoParser.java renamed to plexus-java/src/main/java/org/codehaus/plexus/languages/java/jpms/SourceModuleInfoParser.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* @author Robert Scholte
3737
* @since 1.0.0
3838
*/
39-
public class QDoxModuleInfoParser
39+
class SourceModuleInfoParser
4040
{
4141

4242
public org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor fromSourcePath( Path modulePath )
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.codehaus.plexus.languages.java.jpms;
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
import java.lang.module.ModuleDescriptor;
23+
import java.lang.module.ModuleFinder;
24+
import java.lang.module.ModuleReference;
25+
import java.nio.file.Path;
26+
import java.util.Collections;
27+
28+
import org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor.Builder;
29+
30+
class BinaryModuleInfoParser implements ModuleInfoParser
31+
{
32+
@Override
33+
public JavaModuleDescriptor getModuleDescriptor( Path modulePath )
34+
{
35+
ModuleReference moduleReference = ModuleFinder.of( modulePath ).findAll().iterator().next();
36+
37+
ModuleDescriptor descriptor = moduleReference.descriptor();
38+
39+
Builder builder = JavaModuleDescriptor.newModule( descriptor.name() );
40+
41+
for ( ModuleDescriptor.Requires requires : descriptor.requires() )
42+
{
43+
if ( requires.modifiers().contains( ModuleDescriptor.Requires.Modifier.STATIC ) )
44+
{
45+
builder.requires​( Collections.singleton( org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor.JavaRequires.JavaModifier.STATIC ),
46+
requires.name() );
47+
}
48+
else
49+
{
50+
builder.requires( requires.name() );
51+
}
52+
}
53+
54+
return builder.build();
55+
}
56+
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@
3333
import org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor.JavaRequires;
3434
import org.junit.Test;
3535

36-
public class AsmModuleInfoParserTest
36+
public class BinaryModuleInfoParserTest
3737
{
38-
private ModuleInfoParser parser = new AsmModuleInfoParser();
38+
private BinaryModuleInfoParser parser = new BinaryModuleInfoParser();
3939

4040
@Test
4141
public void testJarDescriptor() throws Exception

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@
4545
public class LocationManagerTest
4646
{
4747
@Mock
48-
private ModuleInfoParser asmParser;
48+
private BinaryModuleInfoParser asmParser;
4949

5050
@Mock
51-
private QDoxModuleInfoParser qdoxParser;
51+
private SourceModuleInfoParser qdoxParser;
5252

5353
private LocationManager locationManager;
5454

plexus-java/src/test/java/org/codehaus/plexus/languages/java/jpms/QDoxModuleInfoParserTest.java renamed to plexus-java/src/test/java/org/codehaus/plexus/languages/java/jpms/SourceModuleInfoParserTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
import org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor.JavaRequires;
3333
import org.junit.Test;
3434

35-
public class QDoxModuleInfoParserTest
35+
public class SourceModuleInfoParserTest
3636
{
37-
private QDoxModuleInfoParser parser = new QDoxModuleInfoParser();
37+
private SourceModuleInfoParser parser = new SourceModuleInfoParser();
3838

3939
@Test
4040
public void test() throws Exception

0 commit comments

Comments
 (0)