Skip to content

Commit 01d3f9f

Browse files
committed
Support LocationManager.resolvePath for single jar
1 parent 32b5ff8 commit 01d3f9f

File tree

14 files changed

+524
-205
lines changed

14 files changed

+524
-205
lines changed

plexus-java/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
<configuration>
5656
<includes>
5757
<include>**/*Test.java</include>
58+
<include>**/*IT.java</include>
5859
</includes>
5960
</configuration>
6061
</plugin>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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.nio.file.Path;
23+
24+
/**
25+
* This is just a placeholder class
26+
*
27+
* @author Robert Scholte
28+
* @since 1.0.0
29+
*/
30+
public class CmdModuleNameExtractor
31+
{
32+
/**
33+
*
34+
* @param modulePath
35+
* @return
36+
*/
37+
public static String getModuleName( Path modulePath )
38+
{
39+
throw new UnsupportedOperationException();
40+
}
41+
}

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

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,48 @@ public static Builder newAutomaticModule( String name )
7272
return new Builder( name ).setAutomatic( true );
7373
}
7474

75+
@Override
76+
public int hashCode()
77+
{
78+
return Objects.hash( name, automatic, requires, exports );
79+
}
80+
81+
@Override
82+
public boolean equals( Object obj )
83+
{
84+
if ( this == obj )
85+
{
86+
return true;
87+
}
88+
if ( obj == null )
89+
{
90+
return false;
91+
}
92+
if ( getClass() != obj.getClass() )
93+
{
94+
return false;
95+
}
96+
97+
JavaModuleDescriptor other = (JavaModuleDescriptor) obj;
98+
if ( automatic != other.automatic )
99+
{
100+
return false;
101+
}
102+
if ( !Objects.equals( name, other.name ) )
103+
{
104+
return false;
105+
}
106+
if ( !Objects.equals( requires, other.requires ) )
107+
{
108+
return false;
109+
}
110+
if ( !Objects.equals( exports, other.exports ) )
111+
{
112+
return false;
113+
}
114+
return true;
115+
}
116+
75117
/**
76118
* A JavaModuleDescriptor Builder
77119
*
@@ -306,7 +348,5 @@ public boolean equals( Object obj )
306348
}
307349
return true;
308350
}
309-
310-
311351
}
312352
}

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

Lines changed: 99 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import javax.inject.Singleton;
3434

3535
import org.codehaus.plexus.component.annotations.Component;
36-
import org.codehaus.plexus.languages.java.jpms.ResolvePathsResult.ModuleNameSource;
3736

3837
/**
3938
* Maps artifacts to modules and analyzes the type of required modules
@@ -49,18 +48,46 @@ public class LocationManager
4948

5049
private SourceModuleInfoParser sourceParser;
5150

51+
private ManifestModuleNameExtractor manifestModuleNameExtractor;
52+
5253
public LocationManager()
5354
{
5455
this.binaryParser = new BinaryModuleInfoParser();
5556
this.sourceParser = new SourceModuleInfoParser();
57+
this.manifestModuleNameExtractor = new ManifestModuleNameExtractor();
5658
}
5759

5860
LocationManager( ModuleInfoParser binaryParser, SourceModuleInfoParser sourceParser )
5961
{
6062
this.binaryParser = binaryParser;
6163
this.sourceParser = sourceParser;
64+
this.manifestModuleNameExtractor = new ManifestModuleNameExtractor();
6265
}
6366

67+
public <T> ResolvePathResult resolvePath( final ResolvePathRequest<T> request ) throws Exception
68+
{
69+
ModuleNameExtractor filenameExtractor = new ModuleNameExtractor()
70+
{
71+
MainClassModuleNameExtractor extractor = new MainClassModuleNameExtractor( request.getJdkHome() );
72+
73+
@Override
74+
public String extract( Path file )
75+
throws IOException
76+
{
77+
if ( request.getJdkHome() != null )
78+
{
79+
return extractor.extract( Collections.singletonMap( file, file ) ).get( file );
80+
}
81+
else
82+
{
83+
return CmdModuleNameExtractor.getModuleName( file );
84+
}
85+
}
86+
};
87+
88+
return resolvePath( request.toPath( request.getPathElement() ), filenameExtractor );
89+
}
90+
6491
/**
6592
* Decide for every {@code request.getPathElements()} if it belongs to the modulePath or classPath, based on the
6693
* {@code request.getMainModuleDescriptor()}.
@@ -69,10 +96,10 @@ public LocationManager()
6996
* @return the result of the resolution
7097
* @throws IOException if a critical IOException occurs
7198
*/
72-
public <T> ResolvePathsResult<T> resolvePaths( ResolvePathsRequest<T> request )
99+
public <T> ResolvePathsResult<T> resolvePaths( final ResolvePathsRequest<T> request )
73100
throws IOException
74101
{
75-
ResolvePathsResult<T> result = request.createResult();
102+
final ResolvePathsResult<T> result = request.createResult();
76103

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

@@ -107,72 +134,44 @@ else if ( descriptorPath.endsWith( "module-info.class" ) )
107134
// start from root
108135
result.setMainModuleDescriptor( mainModuleDescriptor );
109136

110-
Map<T, Path> filenameAutoModules = new HashMap<>();
137+
final Map<T, Path> filenameAutoModules = new HashMap<>();
111138

112-
ManifestModuleNameExtractor manifestModuleNameExtractor = new ManifestModuleNameExtractor();
113-
114139
// collect all modules from path
115-
for ( T t : request.getPathElements() )
140+
for ( final T t : request.getPathElements() )
116141
{
117-
Path path = request.toPath( t );
142+
JavaModuleDescriptor moduleDescriptor;
143+
ModuleNameSource source;
118144

119-
JavaModuleDescriptor moduleDescriptor = null;
120-
ModuleNameSource source = null;
121-
122-
// either jar or outputDirectory
123-
if ( Files.isRegularFile( path ) || Files.exists( path.resolve( "module-info.class" ) ) )
145+
ModuleNameExtractor nameExtractor = new ModuleNameExtractor()
124146
{
125-
try
147+
@Override
148+
public String extract( Path path )
149+
throws IOException
126150
{
127-
moduleDescriptor = binaryParser.getModuleDescriptor( path );
128-
}
129-
catch( IOException e )
130-
{
131-
result.getPathExceptions().put( t, e );
132-
continue;
133-
}
134-
}
135-
136-
if ( moduleDescriptor != null )
137-
{
138-
source = ModuleNameSource.MODULEDESCRIPTOR;
139-
}
140-
else
141-
{
142-
String moduleName = manifestModuleNameExtractor.extract( path );
143-
144-
if ( moduleName != null )
145-
{
146-
source = ModuleNameSource.MANIFEST;
147-
}
148-
else if ( request.getJdkHome() != null )
149-
{
150-
// Will require external JVM, which is considered slow(er)
151-
// Collect first, next resolve all at once
152-
filenameAutoModules.put( t, path );
153-
}
154-
else
155-
{
156-
try
151+
if ( request.getJdkHome() != null )
157152
{
158-
moduleName = MainClassModuleNameExtractor.getModuleName( path );
153+
filenameAutoModules.put( t, path );
159154
}
160-
catch ( Exception e )
155+
else
161156
{
162-
result.getPathExceptions().put( t, e );
163-
continue;
164-
}
165-
166-
if ( moduleName != null )
167-
{
168-
source = ModuleNameSource.FILENAME;
157+
return CmdModuleNameExtractor.getModuleName( path );
169158
}
159+
return null;
170160
}
161+
};
162+
163+
try
164+
{
165+
ResolvePathResult resolvedPath = resolvePath( request.toPath( t ), nameExtractor );
166+
167+
moduleDescriptor = resolvedPath.getModuleDescriptor();
171168

172-
if ( moduleName != null )
173-
{
174-
moduleDescriptor = JavaModuleDescriptor.newAutomaticModule( moduleName ).build();
175-
}
169+
source = resolvedPath.getModuleNameSource();
170+
}
171+
catch ( Exception e )
172+
{
173+
result.getPathExceptions().put( t, e );
174+
continue;
176175
}
177176

178177
if ( moduleDescriptor != null )
@@ -243,6 +242,49 @@ else if ( request.getJdkHome() != null )
243242
return result;
244243
}
245244

245+
private ResolvePathResult resolvePath( Path path, ModuleNameExtractor fileModulenameExtractor ) throws Exception
246+
{
247+
ResolvePathResult result = new ResolvePathResult();
248+
JavaModuleDescriptor moduleDescriptor = null;
249+
250+
// either jar or outputDirectory
251+
if ( Files.isRegularFile( path ) || Files.exists( path.resolve( "module-info.class" ) ) )
252+
{
253+
moduleDescriptor = binaryParser.getModuleDescriptor( path );
254+
}
255+
256+
if ( moduleDescriptor != null )
257+
{
258+
result.setModuleNameSource( ModuleNameSource.MODULEDESCRIPTOR );
259+
}
260+
else
261+
{
262+
String moduleName = manifestModuleNameExtractor.extract( path );
263+
264+
if ( moduleName != null )
265+
{
266+
result.setModuleNameSource( ModuleNameSource.MANIFEST );
267+
}
268+
else
269+
{
270+
moduleName = fileModulenameExtractor.extract( path );
271+
272+
if ( moduleName != null )
273+
{
274+
result.setModuleNameSource( ModuleNameSource.FILENAME );
275+
}
276+
}
277+
278+
if ( moduleName != null )
279+
{
280+
moduleDescriptor = JavaModuleDescriptor.newAutomaticModule( moduleName ).build();
281+
}
282+
}
283+
284+
result.setModuleDescriptor( moduleDescriptor );
285+
return result;
286+
}
287+
246288
private void select( JavaModuleDescriptor module, Map<String, JavaModuleDescriptor> availableModules,
247289
Set<String> namedModules )
248290
{

0 commit comments

Comments
 (0)