@Component role attribute is now of type Class<?>

git-svn-id: https://svn.apache.org/repos/asf/maven/plugin-tools/trunk@1340709 13f79535-47bb-0310-9956-ffa450edef68
master
Olivier Lamy 2012-05-20 13:20:25 +00:00
parent e647661af9
commit 8569bbeb56
12 changed files with 55 additions and 26 deletions

View File

@ -42,7 +42,7 @@ public @interface Component
* role of the component to inject. * role of the component to inject.
* @return the role * @return the role
*/ */
String role() default ""; Class<?> role() default Object.class;
/** /**
* role-hint of the component to inject. * role-hint of the component to inject.

View File

@ -27,6 +27,7 @@ import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.tools.plugin.annotations.FooMojo; import org.apache.maven.tools.plugin.annotations.FooMojo;
import org.apache.maven.project.MavenProjectHelper;
/** /**
* Touches a test file. * Touches a test file.
@ -46,7 +47,7 @@ public class FirstMojo
@Parameter( alias = "alias" ) @Parameter( alias = "alias" )
private String aliasedParam; private String aliasedParam;
@Component( role = "org.apache.maven.project.MavenProjectHelper" )// , roleHint = "default" @Component( role = MavenProjectHelper.class )// , roleHint = "default"
private Object projectHelper; private Object projectHelper;
public void execute() public void execute()

View File

@ -58,7 +58,7 @@ public abstract class AbstractFirstMojo
/** /**
* *
*/ */
@Component( role = "org.apache.maven.artifact.metadata.ArtifactMetadataSource", roleHint = "maven" ) @Component( role = ArtifactMetadataSource.class, roleHint = "maven" )
protected ArtifactMetadataSource artifactMetadataSource; protected ArtifactMetadataSource artifactMetadataSource;
} }

View File

@ -27,6 +27,7 @@ import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.AbstractFirstMojo; import org.apache.maven.plugins.AbstractFirstMojo;
import org.apache.maven.project.MavenProjectHelper;
/** /**
* Touches a test file. * Touches a test file.
@ -47,7 +48,7 @@ public class FirstMojo
@Parameter( alias = "alias" ) @Parameter( alias = "alias" )
private String aliasedParam; private String aliasedParam;
@Component( role = "org.apache.maven.project.MavenProjectHelper" ) @Component( role = MavenProjectHelper.class )
private Object projectHelper; private Object projectHelper;
public void execute() public void execute()
@ -69,6 +70,10 @@ public class FirstMojo
{ {
throw new MojoExecutionException( "compilerManager == null" ); throw new MojoExecutionException( "compilerManager == null" );
} }
if (! ( projectHelper instanceof MavenProjectHelper ))
{
throw new MojoExecutionException( "! projectHelper instanceof MavenProjectHelper" );
}
} }

View File

@ -58,7 +58,7 @@ public abstract class AbstractFirstMojo
/** /**
* *
*/ */
@Component( role = "org.apache.maven.artifact.metadata.ArtifactMetadataSource", roleHint = "maven" ) @Component( role = ArtifactMetadataSource.class, roleHint = "maven" )
protected ArtifactMetadataSource artifactMetadataSource; protected ArtifactMetadataSource artifactMetadataSource;
} }

View File

@ -27,6 +27,7 @@ import org.apache.maven.plugins.annotations.Execute;
import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProjectHelper;
import java.util.Set; import java.util.Set;
@ -50,7 +51,7 @@ public class FirstMojo
@Parameter( alias = "alias" ) @Parameter( alias = "alias" )
private String aliasedParam; private String aliasedParam;
@Component( role = "org.apache.maven.project.MavenProjectHelper" )//, roleHint = "default" @Component( role = MavenProjectHelper.class )//, roleHint = "default"
private Object projectHelper; private Object projectHelper;
@Parameter( defaultValue = "${project.artifacts}", required = true, readonly = true ) @Parameter( defaultValue = "${project.artifacts}", required = true, readonly = true )

View File

@ -44,11 +44,15 @@ under the License.
<artifactId>maven-plugin-api</artifactId> <artifactId>maven-plugin-api</artifactId>
<version>2.0</version> <version>2.0</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>2.0</version>
</dependency>
<dependency> <dependency>
<groupId>org.codehaus.plexus</groupId> <groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId> <artifactId>plexus-utils</artifactId>
<version>3.0.1</version> <version>3.0.1</version>
<scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.maven.plugin-tools</groupId> <groupId>org.apache.maven.plugin-tools</groupId>

View File

@ -27,6 +27,7 @@ import org.apache.maven.plugins.annotations.Execute;
import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProjectHelper;
import java.io.File; import java.io.File;
@ -59,7 +60,7 @@ public class FirstMojo
@Parameter( alias = "alias" ) @Parameter( alias = "alias" )
private String aliasedParam; private String aliasedParam;
@Component( role = "org.apache.maven.project.MavenProjectHelper", roleHint = "test" ) @Component( role = MavenProjectHelper.class, roleHint = "test" )
private Object projectHelper; private Object projectHelper;
public void execute() public void execute()

View File

@ -525,8 +525,9 @@ public class JavaAnnotationsMojoDescriptorExtractor
if ( StringUtils.contains( property, '$' ) || StringUtils.contains( property, '{' ) if ( StringUtils.contains( property, '$' ) || StringUtils.contains( property, '{' )
|| StringUtils.contains( property, '}' ) ) || StringUtils.contains( property, '}' ) )
{ {
throw new InvalidParameterException( "Invalid property for parameter '" + parameter.getName() + "', " throw new InvalidParameterException(
+ "forbidden characters ${}: " + property, null ); "Invalid property for parameter '" + parameter.getName() + "', " + "forbidden characters ${}: "
+ property, null );
} }
parameter.setExpression( StringUtils.isEmpty( property ) ? "" : "${" + property + "}" ); parameter.setExpression( StringUtils.isEmpty( property ) ? "" : "${" + property + "}" );
parameter.setType( parameterAnnotationContent.getClassName() ); parameter.setType( parameterAnnotationContent.getClassName() );
@ -546,8 +547,8 @@ public class JavaAnnotationsMojoDescriptorExtractor
org.apache.maven.plugin.descriptor.Parameter parameter = org.apache.maven.plugin.descriptor.Parameter parameter =
new org.apache.maven.plugin.descriptor.Parameter(); new org.apache.maven.plugin.descriptor.Parameter();
parameter.setName( componentAnnotationContent.getFieldName() ); parameter.setName( componentAnnotationContent.getFieldName() );
parameter.setRequirement( parameter.setRequirement( new Requirement( componentAnnotationContent.getRoleClassName(),
new Requirement( componentAnnotationContent.role(), componentAnnotationContent.roleHint() ) ); componentAnnotationContent.roleHint() ) );
parameter.setDeprecated( componentAnnotationContent.getDeprecated() ); parameter.setDeprecated( componentAnnotationContent.getDeprecated() );
parameter.setSince( componentAnnotationContent.getSince() ); parameter.setSince( componentAnnotationContent.getSince() );
@ -671,8 +672,8 @@ public class JavaAnnotationsMojoDescriptorExtractor
{ {
return null; return null;
} }
@SuppressWarnings( "unchecked" ) @SuppressWarnings( "unchecked" ) Collection<MavenProject> mavenProjects =
Collection<MavenProject> mavenProjects = project.getProjectReferences().values(); project.getProjectReferences().values();
for ( MavenProject mavenProject : mavenProjects ) for ( MavenProject mavenProject : mavenProjects )
{ {
if ( StringUtils.equals( mavenProject.getId(), artifact.getId() ) ) if ( StringUtils.equals( mavenProject.getId(), artifact.getId() ) )

View File

@ -31,7 +31,7 @@ public class ComponentAnnotationContent
extends AnnotatedField extends AnnotatedField
implements Component implements Component
{ {
private String role; private String roleClassName;
private String roleHint; private String roleHint;
@ -43,18 +43,24 @@ public class ComponentAnnotationContent
public ComponentAnnotationContent( String fieldName, String role, String roleHint ) public ComponentAnnotationContent( String fieldName, String role, String roleHint )
{ {
this( fieldName ); this( fieldName );
this.role = role; this.roleClassName = role;
this.roleHint = roleHint; this.roleHint = roleHint;
} }
public String role() public Class<?> role()
{ {
return role == null ? "" : role; // not used
return null;
} }
public void role( String role ) public void setRoleClassName( String roleClassName )
{ {
this.role = role; this.roleClassName = roleClassName;
}
public String getRoleClassName()
{
return roleClassName;
} }
public String roleHint() public String roleHint()
@ -78,7 +84,7 @@ public class ComponentAnnotationContent
final StringBuilder sb = new StringBuilder(); final StringBuilder sb = new StringBuilder();
sb.append( super.toString() ); sb.append( super.toString() );
sb.append( "ComponentAnnotationContent" ); sb.append( "ComponentAnnotationContent" );
sb.append( "{role='" ).append( role ).append( '\'' ); sb.append( "{role='" ).append( roleClassName ).append( '\'' );
sb.append( ", roleHint='" ).append( roleHint ).append( '\'' ); sb.append( ", roleHint='" ).append( roleHint ).append( '\'' );
sb.append( '}' ); sb.append( '}' );
return sb.toString(); return sb.toString();

View File

@ -38,6 +38,7 @@ 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.objectweb.asm.ClassReader; import org.objectweb.asm.ClassReader;
import org.objectweb.asm.Type;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.File; import java.io.File;
@ -302,12 +303,21 @@ public class DefaultMojoAnnotationsScanner
{ {
for ( Map.Entry<String, Object> entry : mojoFieldVisitor.getMojoAnnotationVisitor().getAnnotationValues().entrySet() ) for ( Map.Entry<String, Object> entry : mojoFieldVisitor.getMojoAnnotationVisitor().getAnnotationValues().entrySet() )
{ {
reflector.invoke( componentAnnotationContent, entry.getKey(), String methodName = entry.getKey();
new Object[]{ entry.getValue() } ); if ( StringUtils.equals( "role", methodName ) )
{
Type type = (Type) entry.getValue();
componentAnnotationContent.setRoleClassName( type.getClassName() );
}
else
{
reflector.invoke( componentAnnotationContent, entry.getKey(),
new Object[]{ entry.getValue() } );
}
} }
if ( StringUtils.isEmpty( componentAnnotationContent.role() ) ) if ( StringUtils.isEmpty( componentAnnotationContent.getRoleClassName() ) )
{ {
componentAnnotationContent.role( mojoFieldVisitor.getClassName() ); componentAnnotationContent.setRoleClassName( mojoFieldVisitor.getClassName() );
} }
} }
mojoClassVisitor.getMojoAnnotatedClass().getComponents().put( componentAnnotationContent.getFieldName(), mojoClassVisitor.getMojoAnnotatedClass().getComponents().put( componentAnnotationContent.getFieldName(),

View File

@ -61,7 +61,7 @@ public class FooMojo
/** /**
* *
*/ */
@Component( role = "org.apache.maven.artifact.metadata.ArtifactMetadataSource", roleHint = "maven" ) @Component( role = ArtifactMetadataSource.class, roleHint = "maven" )
protected ArtifactMetadataSource artifactMetadataSource; protected ArtifactMetadataSource artifactMetadataSource;
public void execute() public void execute()