Skip to content

Commit

Permalink
#64 BinaryModuleInfoParser.parse does not take toolchain into account
Browse files Browse the repository at this point in the history
Update ASM to 9.1
  • Loading branch information
rfscholte committed Feb 15, 2021
1 parent a1b61e0 commit 249f8bd
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 103 deletions.
2 changes: 1 addition & 1 deletion plexus-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>9.0</version>
<version>9.1</version>
</dependency>
<dependency>
<groupId>com.thoughtworks.qdox</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package org.codehaus.plexus.languages.java.jpms;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ModuleVisitor;
import org.objectweb.asm.Opcodes;

/**
* Extract information from module with ASM
*
*
* @author Robert Scholte
* @since 1.0.0
*/
class AsmModuleInfoParser extends AbstractBinaryModuleInfoParser
{
@Override
JavaModuleDescriptor parse( InputStream in )
throws IOException
{
final JavaModuleDescriptorWrapper wrapper = new JavaModuleDescriptorWrapper();

ClassReader reader = new ClassReader( in );
reader.accept( new ClassVisitor( Opcodes.ASM9 )
{
@Override
public ModuleVisitor visitModule( String name, int arg1, String arg2 )
{
wrapper.builder = JavaModuleDescriptor.newModule( name );

return new ModuleVisitor( Opcodes.ASM9 )
{
@Override
public void visitRequire( String module, int access, String version )
{
if ( ( access & Opcodes.ACC_STATIC_PHASE ) != 0 )
{
wrapper.builder.requires​( Collections.singleton( JavaModuleDescriptor.JavaRequires.JavaModifier.STATIC ),
module );
}
else
{
wrapper.builder.requires( module );
}
}

@Override
public void visitExport( String pn, int ms, String... targets )
{
if ( targets == null || targets.length == 0 )
{
wrapper.builder.exports( pn.replace( '/', '.' ) );
}
else
{
wrapper.builder.exports( pn.replace( '/', '.' ), new HashSet<>( Arrays.asList( targets ) ) );
}
}

@Override
public void visitUse( String service )
{
wrapper.builder.uses( service.replace( '/', '.' ) );
}

@Override
public void visitProvide( String service, String... providers )
{
List<String> renamedProvides = new ArrayList<>( providers.length );
for ( String provider : providers )
{
renamedProvides.add( provider.replace( '/', '.' ) );
}
wrapper.builder.provides​( service.replace( '/', '.' ), renamedProvides );
}
};
}
}, 0 );
return wrapper.builder.build();
}

private static class JavaModuleDescriptorWrapper
{
private JavaModuleDescriptor.Builder builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,96 +18,7 @@
* specific language governing permissions and limitations
* under the License.
*/

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ModuleVisitor;
import org.objectweb.asm.Opcodes;

/**
* Extract information from module with ASM
*
*
* @author Robert Scholte
* @since 1.0.0
*/
class BinaryModuleInfoParser extends AbstractBinaryModuleInfoParser
class BinaryModuleInfoParser extends AsmModuleInfoParser
{
@Override
JavaModuleDescriptor parse( InputStream in )
throws IOException
{
final JavaModuleDescriptorWrapper wrapper = new JavaModuleDescriptorWrapper();

ClassReader reader = new ClassReader( in );
reader.accept( new ClassVisitor( Opcodes.ASM6 )
{
@Override
public ModuleVisitor visitModule( String name, int arg1, String arg2 )
{
wrapper.builder = JavaModuleDescriptor.newModule( name );

return new ModuleVisitor( Opcodes.ASM6 )
{
@Override
public void visitRequire( String module, int access, String version )
{
if ( ( access & Opcodes.ACC_STATIC_PHASE ) != 0 )
{
wrapper.builder.requires​( Collections.singleton( JavaModuleDescriptor.JavaRequires.JavaModifier.STATIC ),
module );
}
else
{
wrapper.builder.requires( module );
}
}

@Override
public void visitExport( String pn, int ms, String... targets )
{
if ( targets == null || targets.length == 0 )
{
wrapper.builder.exports( pn.replace( '/', '.' ) );
}
else
{
wrapper.builder.exports( pn.replace( '/', '.' ), new HashSet<>( Arrays.asList( targets ) ) );
}
}

@Override
public void visitUse( String service )
{
wrapper.builder.uses( service.replace( '/', '.' ) );
}

@Override
public void visitProvide( String service, String... providers )
{
List<String> renamedProvides = new ArrayList<>( providers.length );
for ( String provider : providers )
{
renamedProvides.add( provider.replace( '/', '.' ) );
}
wrapper.builder.provides​( service.replace( '/', '.' ), renamedProvides );
}
};
}
}, 0 );
return wrapper.builder.build();
}

private static class JavaModuleDescriptorWrapper
{
private JavaModuleDescriptor.Builder builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,18 @@
@Singleton
public class LocationManager
{
private ModuleInfoParser binaryParser;

private SourceModuleInfoParser sourceParser;

private ManifestModuleNameExtractor manifestModuleNameExtractor;

public LocationManager()
{
this.binaryParser = new BinaryModuleInfoParser();
this.sourceParser = new SourceModuleInfoParser();
this.manifestModuleNameExtractor = new ManifestModuleNameExtractor();
}

LocationManager( ModuleInfoParser binaryParser, SourceModuleInfoParser sourceParser )
LocationManager( SourceModuleInfoParser sourceParser )
{
this.binaryParser = binaryParser;
this.sourceParser = sourceParser;
this.manifestModuleNameExtractor = new ManifestModuleNameExtractor();
}
Expand Down Expand Up @@ -136,7 +132,7 @@ public String extract( Path file )
}
};

return resolvePath( request.toPath( request.getPathElement() ), filenameExtractor );
return resolvePath( request.toPath( request.getPathElement() ), filenameExtractor, getBinaryModuleInfoParser( request.getJdkHome() ) );
}

/**
Expand All @@ -154,7 +150,9 @@ public <T> ResolvePathsResult<T> resolvePaths( final ResolvePathsRequest<T> requ

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

JavaModuleDescriptor mainModuleDescriptor = getMainModuleDescriptor( request );
final ModuleInfoParser binaryParser = getBinaryModuleInfoParser( request.getJdkHome() );

JavaModuleDescriptor mainModuleDescriptor = getMainModuleDescriptor( request, binaryParser );

result.setMainModuleDescriptor( mainModuleDescriptor );

Expand Down Expand Up @@ -198,7 +196,7 @@ public String extract( Path path )

try
{
ResolvePathResult resolvedPath = resolvePath( request.toPath( t ), nameExtractor );
ResolvePathResult resolvedPath = resolvePath( request.toPath( t ), nameExtractor, binaryParser );

moduleDescriptor = resolvedPath.getModuleDescriptor();

Expand Down Expand Up @@ -294,7 +292,28 @@ public String extract( Path path )
return result;
}

private <T> JavaModuleDescriptor getMainModuleDescriptor( final ResolvePathsRequest<T> request )
/**
* If the jdkHome is specified, its version it considered higher than the runtime java version.
* In that case ASM must be used to read the module descriptor
*
* @param jdkHome
* @return
*/
ModuleInfoParser getBinaryModuleInfoParser( final Path jdkHome )
{
final ModuleInfoParser binaryParser;
if ( jdkHome == null )
{
binaryParser = new BinaryModuleInfoParser();
}
else
{
binaryParser = new AsmModuleInfoParser();
}
return binaryParser;
}

private <T> JavaModuleDescriptor getMainModuleDescriptor( final ResolvePathsRequest<T> request, ModuleInfoParser binaryParser )
throws IOException
{
JavaModuleDescriptor mainModuleDescriptor;
Expand Down Expand Up @@ -323,7 +342,7 @@ else if ( descriptorPath.endsWith( "module-info.class" ) )
return mainModuleDescriptor;
}

private ResolvePathResult resolvePath( Path path, ModuleNameExtractor fileModulenameExtractor ) throws IOException
private ResolvePathResult resolvePath( Path path, ModuleNameExtractor fileModulenameExtractor, ModuleInfoParser binaryParser ) throws IOException
{
ResolvePathResult result = new ResolvePathResult();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,14 @@ public static void assume()
@Before
public void onSetup()
{
locationManager = new LocationManager( asmParser, qdoxParser );
locationManager = new LocationManager( qdoxParser )
{
@Override
ModuleInfoParser getBinaryModuleInfoParser( Path jdkHome )
{
return asmParser;
}
};
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,14 @@ public class LocationManagerTest
@Before
public void onSetup()
{
locationManager = new LocationManager( asmParser, qdoxParser );
locationManager = new LocationManager( qdoxParser )
{
@Override
ModuleInfoParser getBinaryModuleInfoParser( Path jdkHome )
{
return asmParser;
}
};
}

@Test
Expand Down

0 comments on commit 249f8bd

Please sign in to comment.