code simplification

git-svn-id: https://svn.apache.org/repos/asf/maven/plugin-tools/trunk@1591999 13f79535-47bb-0310-9956-ffa450edef68
master
Herve Boutemy 2014-05-02 18:29:41 +00:00
parent ba103c598f
commit a6eab7460b
1 changed files with 93 additions and 108 deletions

View File

@ -37,6 +37,7 @@ import org.codehaus.plexus.util.DirectoryScanner;
import org.codehaus.plexus.util.IOUtil; import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.reflection.Reflector; import org.codehaus.plexus.util.reflection.Reflector;
import org.codehaus.plexus.util.reflection.ReflectorException;
import org.objectweb.asm.ClassReader; import org.objectweb.asm.ClassReader;
import org.objectweb.asm.Type; import org.objectweb.asm.Type;
@ -45,7 +46,6 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -67,63 +67,63 @@ public class DefaultMojoAnnotationsScanner
throws ExtractionException throws ExtractionException
{ {
Map<String, MojoAnnotatedClass> mojoAnnotatedClasses = new HashMap<String, MojoAnnotatedClass>(); Map<String, MojoAnnotatedClass> mojoAnnotatedClasses = new HashMap<String, MojoAnnotatedClass>();
try try
{ {
for ( Artifact dependency : request.getDependencies() ) for ( Artifact dependency : request.getDependencies() )
{ {
File dependencyFile = dependency.getFile(); scan( mojoAnnotatedClasses, dependency.getFile(), request.getIncludePatterns(), dependency, true );
if ( dependencyFile != null && dependencyFile.exists() )
{
if ( dependencyFile.isDirectory() )
{
mojoAnnotatedClasses.putAll(
scanDirectory( dependencyFile, request.getIncludePatterns(), dependency, true ) );
}
else
{
mojoAnnotatedClasses.putAll(
scanFile( dependencyFile, request.getIncludePatterns(), dependency, true ) );
}
}
} }
for ( File classDirectory : request.getClassesDirectories() ) for ( File classDirectory : request.getClassesDirectories() )
{ {
if ( classDirectory.exists() && classDirectory.isDirectory() ) scan( mojoAnnotatedClasses, classDirectory, request.getIncludePatterns(),
{ request.getProject().getArtifact(), false );
mojoAnnotatedClasses.putAll(
scanDirectory( classDirectory, request.getIncludePatterns(), request.getProject().getArtifact(),
false ) );
} }
} }
return mojoAnnotatedClasses;
}
catch ( IOException e ) catch ( IOException e )
{ {
throw new ExtractionException( e.getMessage(), e ); throw new ExtractionException( e.getMessage(), e );
} }
return mojoAnnotatedClasses;
}
protected void scan( Map<String, MojoAnnotatedClass> mojoAnnotatedClasses, File source,
List<String> includePatterns, Artifact artifact, boolean excludeMojo )
throws IOException, ExtractionException
{
if ( source == null || ! source.exists() )
{
return;
}
Map<String, MojoAnnotatedClass> scanResult;
if ( source.isDirectory() )
{
scanResult = scanDirectory( source, includePatterns, artifact, excludeMojo );
}
else
{
scanResult = scanArchive( source, artifact, excludeMojo );
}
mojoAnnotatedClasses.putAll( scanResult );
} }
/** /**
* @param archiveFile * @param archiveFile
* @param includePatterns
* @param artifact * @param artifact
* @param excludeMojo for dependencies we exclude Mojo annotations found * @param excludeMojo for dependencies, we exclude Mojo annotations found
* @return * @return
* @throws IOException * @throws IOException
* @throws ExtractionException * @throws ExtractionException
*/ */
protected Map<String, MojoAnnotatedClass> scanFile( File archiveFile, List<String> includePatterns, protected Map<String, MojoAnnotatedClass> scanArchive( File archiveFile, Artifact artifact, boolean excludeMojo )
Artifact artifact, boolean excludeMojo )
throws IOException, ExtractionException throws IOException, ExtractionException
{ {
if ( !archiveFile.exists() )
{
return Collections.emptyMap();
}
Map<String, MojoAnnotatedClass> mojoAnnotatedClasses = new HashMap<String, MojoAnnotatedClass>(); Map<String, MojoAnnotatedClass> mojoAnnotatedClasses = new HashMap<String, MojoAnnotatedClass>();
ZipInputStream archiveStream = new ZipInputStream( new FileInputStream( archiveFile ) ); ZipInputStream archiveStream = new ZipInputStream( new FileInputStream( archiveFile ) );
try try
@ -131,31 +131,12 @@ public class DefaultMojoAnnotationsScanner
for ( ZipEntry zipEntry = archiveStream.getNextEntry(); zipEntry != null; for ( ZipEntry zipEntry = archiveStream.getNextEntry(); zipEntry != null;
zipEntry = archiveStream.getNextEntry() ) zipEntry = archiveStream.getNextEntry() )
{ {
if ( zipEntry.getName().endsWith( ".class" ) ) if ( !zipEntry.getName().endsWith( ".class" ) )
{ {
MojoClassVisitor mojoClassVisitor = new MojoClassVisitor( getLogger() ); continue;
}
ClassReader rdr = new ClassReader( archiveStream ); analyzeClassStream( mojoAnnotatedClasses, archiveStream, artifact, excludeMojo );
rdr.accept( mojoClassVisitor,
ClassReader.SKIP_FRAMES | ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG );
analyzeVisitors( mojoClassVisitor );
if ( excludeMojo )
{
mojoClassVisitor.getMojoAnnotatedClass().setMojo( null );
}
if ( isStoreClass( mojoClassVisitor.getMojoAnnotatedClass() ) != null )
{
if ( getLogger().isDebugEnabled() )
{
getLogger().debug( "found MojoAnnotatedClass:"
+ mojoClassVisitor.getMojoAnnotatedClass().getClassName() + ":"
+ mojoClassVisitor.getMojoAnnotatedClass() );
}
mojoClassVisitor.getMojoAnnotatedClass().setArtifact( artifact );
mojoAnnotatedClasses.put( mojoClassVisitor.getMojoAnnotatedClass().getClassName(),
mojoClassVisitor.getMojoAnnotatedClass() );
}
}
} }
} }
finally finally
@ -169,7 +150,7 @@ public class DefaultMojoAnnotationsScanner
* @param classDirectory * @param classDirectory
* @param includePatterns * @param includePatterns
* @param artifact * @param artifact
* @param excludeMojo for dependencies we exclude Mojo annotations found * @param excludeMojo for dependencies, we exclude Mojo annotations found
* @return * @return
* @throws IOException * @throws IOException
* @throws ExtractionException * @throws ExtractionException
@ -178,11 +159,8 @@ public class DefaultMojoAnnotationsScanner
Artifact artifact, boolean excludeMojo ) Artifact artifact, boolean excludeMojo )
throws IOException, ExtractionException throws IOException, ExtractionException
{ {
if ( !classDirectory.exists() )
{
return Collections.emptyMap();
}
Map<String, MojoAnnotatedClass> mojoAnnotatedClasses = new HashMap<String, MojoAnnotatedClass>(); Map<String, MojoAnnotatedClass> mojoAnnotatedClasses = new HashMap<String, MojoAnnotatedClass>();
DirectoryScanner scanner = new DirectoryScanner(); DirectoryScanner scanner = new DirectoryScanner();
scanner.setBasedir( classDirectory ); scanner.setBasedir( classDirectory );
scanner.addDefaultExcludes(); scanner.addDefaultExcludes();
@ -202,11 +180,26 @@ public class DefaultMojoAnnotationsScanner
InputStream is = new BufferedInputStream( new FileInputStream( new File( classDirectory, classFile ) ) ); InputStream is = new BufferedInputStream( new FileInputStream( new File( classDirectory, classFile ) ) );
try try
{
analyzeClassStream( mojoAnnotatedClasses, is, artifact, excludeMojo );
}
finally
{
IOUtil.close( is );
}
}
return mojoAnnotatedClasses;
}
private void analyzeClassStream( Map<String, MojoAnnotatedClass> mojoAnnotatedClasses, InputStream is,
Artifact artifact, boolean excludeMojo )
throws IOException, ExtractionException
{ {
MojoClassVisitor mojoClassVisitor = new MojoClassVisitor( getLogger() ); MojoClassVisitor mojoClassVisitor = new MojoClassVisitor( getLogger() );
ClassReader rdr = new ClassReader( is ); ClassReader rdr = new ClassReader( is );
rdr.accept( mojoClassVisitor, rdr.accept( mojoClassVisitor, ClassReader.SKIP_FRAMES | ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG );
ClassReader.SKIP_FRAMES | ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG );
analyzeVisitors( mojoClassVisitor ); analyzeVisitors( mojoClassVisitor );
MojoAnnotatedClass mojoAnnotatedClass = mojoClassVisitor.getMojoAnnotatedClass(); MojoAnnotatedClass mojoAnnotatedClass = mojoClassVisitor.getMojoAnnotatedClass();
@ -227,13 +220,6 @@ public class DefaultMojoAnnotationsScanner
mojoAnnotatedClasses.put( mojoAnnotatedClass.getClassName(), mojoAnnotatedClass ); mojoAnnotatedClasses.put( mojoAnnotatedClass.getClassName(), mojoAnnotatedClass );
} }
} }
finally
{
IOUtil.close( is );
}
}
return mojoAnnotatedClasses;
}
private MojoAnnotatedClass isStoreClass( MojoAnnotatedClass mojoAnnotatedClass ) private MojoAnnotatedClass isStoreClass( MojoAnnotatedClass mojoAnnotatedClass )
{ {
@ -253,59 +239,58 @@ public class DefaultMojoAnnotationsScanner
**/ **/
} }
protected void populateAnnotationContent( Object content, MojoAnnotationVisitor mojoAnnotationVisitor )
throws ReflectorException
{
for ( Map.Entry<String, Object> entry : mojoAnnotationVisitor.getAnnotationValues().entrySet() )
{
reflector.invoke( content, entry.getKey(), new Object[] { entry.getValue() } );
}
}
protected void analyzeVisitors( MojoClassVisitor mojoClassVisitor ) protected void analyzeVisitors( MojoClassVisitor mojoClassVisitor )
throws ExtractionException throws ExtractionException
{ {
final MojoAnnotatedClass mojoAnnotatedClass = mojoClassVisitor.getMojoAnnotatedClass();
try try
{ {
// @Mojo annotations
MojoAnnotationVisitor mojoAnnotationVisitor = MojoAnnotationVisitor mojoAnnotationVisitor =
mojoClassVisitor.getAnnotationVisitorMap().get( Mojo.class.getName() ); mojoClassVisitor.getAnnotationVisitorMap().get( Mojo.class.getName() );
if ( mojoAnnotationVisitor != null ) if ( mojoAnnotationVisitor != null )
{ {
MojoAnnotationContent mojoAnnotationContent = new MojoAnnotationContent(); MojoAnnotationContent mojoAnnotationContent = new MojoAnnotationContent();
for ( Map.Entry<String, Object> entry : mojoAnnotationVisitor.getAnnotationValues().entrySet() ) populateAnnotationContent( mojoAnnotationContent, mojoAnnotationVisitor );
{ mojoAnnotatedClass.setMojo( mojoAnnotationContent );
reflector.invoke( mojoAnnotationContent, entry.getKey(), new Object[]{ entry.getValue() } );
}
mojoClassVisitor.getMojoAnnotatedClass().setMojo( mojoAnnotationContent );
} }
// @Execute annotations
mojoAnnotationVisitor = mojoClassVisitor.getAnnotationVisitorMap().get( Execute.class.getName() ); mojoAnnotationVisitor = mojoClassVisitor.getAnnotationVisitorMap().get( Execute.class.getName() );
if ( mojoAnnotationVisitor != null ) if ( mojoAnnotationVisitor != null )
{ {
ExecuteAnnotationContent executeAnnotationContent = new ExecuteAnnotationContent(); ExecuteAnnotationContent executeAnnotationContent = new ExecuteAnnotationContent();
populateAnnotationContent( executeAnnotationContent, mojoAnnotationVisitor );
for ( Map.Entry<String, Object> entry : mojoAnnotationVisitor.getAnnotationValues().entrySet() ) mojoAnnotatedClass.setExecute( executeAnnotationContent );
{
reflector.invoke( executeAnnotationContent, entry.getKey(), new Object[]{ entry.getValue() } );
}
mojoClassVisitor.getMojoAnnotatedClass().setExecute( executeAnnotationContent );
} }
// @Parameter annotations
List<MojoFieldVisitor> mojoFieldVisitors = List<MojoFieldVisitor> mojoFieldVisitors =
mojoClassVisitor.findFieldWithAnnotationClass( Parameter.class.getName() ); mojoClassVisitor.findFieldWithAnnotationClass( Parameter.class.getName() );
for ( MojoFieldVisitor mojoFieldVisitor : mojoFieldVisitors ) for ( MojoFieldVisitor mojoFieldVisitor : mojoFieldVisitors )
{ {
ParameterAnnotationContent parameterAnnotationContent = ParameterAnnotationContent parameterAnnotationContent =
new ParameterAnnotationContent( mojoFieldVisitor.getFieldName(), mojoFieldVisitor.getClassName() ); new ParameterAnnotationContent( mojoFieldVisitor.getFieldName(), mojoFieldVisitor.getClassName() );
if ( mojoFieldVisitor.getMojoAnnotationVisitor() != null ) if ( mojoFieldVisitor.getMojoAnnotationVisitor() != null )
{ {
for ( Map.Entry<String, Object> entry : mojoFieldVisitor.getMojoAnnotationVisitor().getAnnotationValues().entrySet() ) populateAnnotationContent( parameterAnnotationContent, mojoFieldVisitor.getMojoAnnotationVisitor() );
{
reflector.invoke( parameterAnnotationContent, entry.getKey(),
new Object[]{ entry.getValue() } );
} }
mojoAnnotatedClass.getParameters().put( parameterAnnotationContent.getFieldName(),
}
mojoClassVisitor.getMojoAnnotatedClass().getParameters().put( parameterAnnotationContent.getFieldName(),
parameterAnnotationContent ); parameterAnnotationContent );
} }
// @Component annotations
mojoFieldVisitors = mojoClassVisitor.findFieldWithAnnotationClass( Component.class.getName() ); mojoFieldVisitors = mojoClassVisitor.findFieldWithAnnotationClass( Component.class.getName() );
for ( MojoFieldVisitor mojoFieldVisitor : mojoFieldVisitors ) for ( MojoFieldVisitor mojoFieldVisitor : mojoFieldVisitors )
{ {
ComponentAnnotationContent componentAnnotationContent = ComponentAnnotationContent componentAnnotationContent =
@ -327,17 +312,17 @@ public class DefaultMojoAnnotationsScanner
new Object[]{ entry.getValue() } ); new Object[]{ entry.getValue() } );
} }
} }
if ( StringUtils.isEmpty( componentAnnotationContent.getRoleClassName() ) ) if ( StringUtils.isEmpty( componentAnnotationContent.getRoleClassName() ) )
{ {
componentAnnotationContent.setRoleClassName( mojoFieldVisitor.getClassName() ); componentAnnotationContent.setRoleClassName( mojoFieldVisitor.getClassName() );
} }
} }
mojoClassVisitor.getMojoAnnotatedClass().getComponents().put( componentAnnotationContent.getFieldName(), mojoAnnotatedClass.getComponents().put( componentAnnotationContent.getFieldName(),
componentAnnotationContent ); componentAnnotationContent );
} }
} }
catch ( Exception e ) catch ( ReflectorException e )
{ {
throw new ExtractionException( e.getMessage(), e ); throw new ExtractionException( e.getMessage(), e );
} }