Skip to content

Commit 29a4d80

Browse files
committed
Introduce CmdModuleNameExtractor, which uses Java 9 code instead of reflection
1 parent 01d3f9f commit 29a4d80

File tree

8 files changed

+159
-43
lines changed

8 files changed

+159
-43
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@
2929
*/
3030
public class CmdModuleNameExtractor
3131
{
32+
public static void main( String[] args )
33+
{
34+
System.err.println( "Use at least Java 9 to execute this class" );
35+
36+
System.exit( -1 );
37+
}
38+
3239
/**
3340
*
3441
* @param modulePath

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

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,14 @@ public LocationManager()
6464
this.manifestModuleNameExtractor = new ManifestModuleNameExtractor();
6565
}
6666

67-
public <T> ResolvePathResult resolvePath( final ResolvePathRequest<T> request ) throws Exception
67+
/**
68+
* Resolve a single jar
69+
*
70+
* @param request the request
71+
* @return the {@link ResolvePathResult}, containing the name and optional module descriptor
72+
* @throws IOException if any occurs
73+
*/
74+
public <T> ResolvePathResult resolvePath( final ResolvePathRequest<T> request ) throws IOException
6875
{
6976
ModuleNameExtractor filenameExtractor = new ModuleNameExtractor()
7077
{
@@ -103,37 +110,14 @@ public <T> ResolvePathsResult<T> resolvePaths( final ResolvePathsRequest<T> requ
103110

104111
Map<T, JavaModuleDescriptor> pathElements = new LinkedHashMap<>( request.getPathElements().size() );
105112

106-
JavaModuleDescriptor mainModuleDescriptor;
107-
108-
Path descriptorPath = request.getMainModuleDescriptor();
109-
110-
if ( descriptorPath != null )
111-
{
112-
if ( descriptorPath.endsWith( "module-info.java" ) )
113-
{
114-
mainModuleDescriptor = sourceParser.fromSourcePath( descriptorPath );
115-
}
116-
else if ( descriptorPath.endsWith( "module-info.class" ) )
117-
{
118-
mainModuleDescriptor = binaryParser.getModuleDescriptor( descriptorPath.getParent() );
119-
}
120-
else
121-
{
122-
throw new IOException( "Invalid path to module descriptor: " + descriptorPath );
123-
}
124-
}
125-
else
126-
{
127-
mainModuleDescriptor = null;
128-
}
113+
JavaModuleDescriptor mainModuleDescriptor = getMainModuleDescriptor( request );
114+
115+
result.setMainModuleDescriptor( mainModuleDescriptor );
129116

130117
Map<String, JavaModuleDescriptor> availableNamedModules = new HashMap<>();
131118

132119
Map<String, ModuleNameSource> moduleNameSources = new HashMap<>();
133120

134-
// start from root
135-
result.setMainModuleDescriptor( mainModuleDescriptor );
136-
137121
final Map<T, Path> filenameAutoModules = new HashMap<>();
138122

139123
// collect all modules from path
@@ -242,7 +226,36 @@ public String extract( Path path )
242226
return result;
243227
}
244228

245-
private ResolvePathResult resolvePath( Path path, ModuleNameExtractor fileModulenameExtractor ) throws Exception
229+
private <T> JavaModuleDescriptor getMainModuleDescriptor( final ResolvePathsRequest<T> request )
230+
throws IOException
231+
{
232+
JavaModuleDescriptor mainModuleDescriptor;
233+
234+
Path descriptorPath = request.getMainModuleDescriptor();
235+
236+
if ( descriptorPath != null )
237+
{
238+
if ( descriptorPath.endsWith( "module-info.java" ) )
239+
{
240+
mainModuleDescriptor = sourceParser.fromSourcePath( descriptorPath );
241+
}
242+
else if ( descriptorPath.endsWith( "module-info.class" ) )
243+
{
244+
mainModuleDescriptor = binaryParser.getModuleDescriptor( descriptorPath.getParent() );
245+
}
246+
else
247+
{
248+
throw new IOException( "Invalid path to module descriptor: " + descriptorPath );
249+
}
250+
}
251+
else
252+
{
253+
mainModuleDescriptor = null;
254+
}
255+
return mainModuleDescriptor;
256+
}
257+
258+
private ResolvePathResult resolvePath( Path path, ModuleNameExtractor fileModulenameExtractor ) throws IOException
246259
{
247260
ResolvePathResult result = new ResolvePathResult();
248261
JavaModuleDescriptor moduleDescriptor = null;

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.nio.file.FileVisitResult;
2727
import java.nio.file.Files;
2828
import java.nio.file.Path;
29-
import java.nio.file.Paths;
3029
import java.nio.file.SimpleFileVisitor;
3130
import java.nio.file.attribute.BasicFileAttributes;
3231
import java.util.HashMap;

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
package org.codehaus.plexus.languages.java.jpms;
22

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+
322
public enum ModuleNameSource
423
{
524
FILENAME, MANIFEST, MODULEDESCRIPTOR

plexus-java/src/main/java9/org/codehaus/plexus/languages/java/jpms/CmdModuleNameExtractor.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
package org.codehaus.plexus.languages.java.jpms;
22

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+
322
import java.io.IOException;
423
import java.lang.module.FindException;
524
import java.lang.module.ModuleFinder;
@@ -11,6 +30,18 @@
1130
import java.util.Set;
1231

1332
/**
33+
* Usage:
34+
* <code>
35+
* java -classpath &lt;cp&gt; org.codehaus.plexus.languages.java.jpms.CmdModuleNameExtractor &lt;args&gt;
36+
* </code>
37+
* <p>
38+
* Where &lt;cp&gt; is the jar or directory containing the {@code o.c.p.l.j.j.CmdModuleNameExtractor} and
39+
* where &lt;args&gt; are paths to jars.
40+
* </p>
41+
* <p>
42+
* The result is a properties-file written ot the StdOut, having the jar path as key and the module name as value.<br>
43+
* Any exception is written to the StdErr.
44+
* </p>
1445
*
1546
* @author Robert Scholte
1647
* @since 1.0.0
@@ -47,6 +78,13 @@ public static void main( String[] args )
4778
}
4879
}
4980

81+
/**
82+
* Get the name of the module, using Java 9 code without reflection
83+
*
84+
* @param modulePath the module path
85+
* @return the module name
86+
* @throws FindException If an error occurs finding the module
87+
*/
5088
public static String getModuleName( Path modulePath ) throws FindException
5189
{
5290
Set<ModuleReference> moduleReferences = ModuleFinder.of( modulePath ).findAll();
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 static org.junit.Assert.assertEquals;
23+
24+
import java.nio.file.Path;
25+
26+
import org.junit.Test;
27+
28+
public class CmdModuleNameExtractorTest
29+
{
30+
@Test
31+
public void testMethodCount() throws Exception
32+
{
33+
// ensure that both implementations are in sync
34+
assertEquals( 2, CmdModuleNameExtractor.class.getDeclaredMethods().length );
35+
36+
// if these don't exist, a NoSuchMethodException is thrown
37+
CmdModuleNameExtractor.class.getDeclaredMethod( "main", String[].class );
38+
CmdModuleNameExtractor.class.getDeclaredMethod( "getModuleName", Path.class );
39+
}
40+
}

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.io.IOException;
1212
import java.nio.file.Path;
1313
import java.nio.file.Paths;
14+
import java.util.Arrays;
1415
import java.util.Collections;
1516

1617
import org.junit.Before;
@@ -88,5 +89,17 @@ public void testResolvePathWithException() throws Exception
8889

8990
locationManager.resolvePath( request );
9091
}
91-
92+
93+
@Test
94+
public void testClassicJarNameStartsWithNumber() throws Exception
95+
{
96+
assumeThat( "Requires at least Java 9", System.getProperty( "java.version" ), not( startsWith( "1." ) ) );
97+
98+
Path p = Paths.get( "src/test/resources/jar.empty.invalid.name/101-1.0.0-SNAPSHOT.jar" );
99+
ResolvePathsRequest<Path> request = ResolvePathsRequest.ofPaths( Arrays.asList( p ) ).setMainModuleDescriptor( mockModuleInfoJava );
100+
101+
ResolvePathsResult<Path> result = locationManager.resolvePaths( request );
102+
103+
assertThat( result.getPathExceptions().size(), is( 1 ) );
104+
}
92105
}

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -167,19 +167,6 @@ public void testIdenticalModuleNames() throws Exception
167167
assertThat( result.getPathExceptions().size(), is( 0 ) );
168168
}
169169

170-
@Test
171-
public void testClassicJarNameStartsWithNumber() throws Exception
172-
{
173-
assumeThat( "Requires at least Java 9", System.getProperty( "java.version" ), not( startsWith( "1." ) ) );
174-
175-
Path p = Paths.get( "src/test/resources/jar.empty.invalid.name/101-1.0.0-SNAPSHOT.jar" );
176-
ResolvePathsRequest<Path> request = ResolvePathsRequest.ofPaths( Arrays.asList( p ) ).setMainModuleDescriptor( mockModuleInfoJava );
177-
178-
ResolvePathsResult<Path> result = locationManager.resolvePaths( request );
179-
180-
assertThat( result.getPathExceptions().size(), is( 1 ) );
181-
}
182-
183170
@Test
184171
public void testNonJar() throws Exception
185172
{

0 commit comments

Comments
 (0)