@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.
* @return the role
*/
String role() default "";
Class<?> role() default Object.class;
/**
* 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.Parameter;
import org.apache.maven.tools.plugin.annotations.FooMojo;
import org.apache.maven.project.MavenProjectHelper;
/**
* Touches a test file.
@ -46,7 +47,7 @@ public class FirstMojo
@Parameter( alias = "alias" )
private String aliasedParam;
@Component( role = "org.apache.maven.project.MavenProjectHelper" )// , roleHint = "default"
@Component( role = MavenProjectHelper.class )// , roleHint = "default"
private Object projectHelper;
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;
}

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.Parameter;
import org.apache.maven.plugins.AbstractFirstMojo;
import org.apache.maven.project.MavenProjectHelper;
/**
* Touches a test file.
@ -47,7 +48,7 @@ public class FirstMojo
@Parameter( alias = "alias" )
private String aliasedParam;
@Component( role = "org.apache.maven.project.MavenProjectHelper" )
@Component( role = MavenProjectHelper.class )
private Object projectHelper;
public void execute()
@ -69,6 +70,10 @@ public class FirstMojo
{
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;
}

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

View File

@ -44,11 +44,15 @@ under the License.
<artifactId>maven-plugin-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<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.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProjectHelper;
import java.io.File;
@ -59,7 +60,7 @@ public class FirstMojo
@Parameter( alias = "alias" )
private String aliasedParam;
@Component( role = "org.apache.maven.project.MavenProjectHelper", roleHint = "test" )
@Component( role = MavenProjectHelper.class, roleHint = "test" )
private Object projectHelper;
public void execute()

View File

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

View File

@ -31,7 +31,7 @@ public class ComponentAnnotationContent
extends AnnotatedField
implements Component
{
private String role;
private String roleClassName;
private String roleHint;
@ -43,18 +43,24 @@ public class ComponentAnnotationContent
public ComponentAnnotationContent( String fieldName, String role, String roleHint )
{
this( fieldName );
this.role = role;
this.roleClassName = role;
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()
@ -78,7 +84,7 @@ public class ComponentAnnotationContent
final StringBuilder sb = new StringBuilder();
sb.append( super.toString() );
sb.append( "ComponentAnnotationContent" );
sb.append( "{role='" ).append( role ).append( '\'' );
sb.append( "{role='" ).append( roleClassName ).append( '\'' );
sb.append( ", roleHint='" ).append( roleHint ).append( '\'' );
sb.append( '}' );
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.reflection.Reflector;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.Type;
import java.io.BufferedInputStream;
import java.io.File;
@ -302,12 +303,21 @@ public class DefaultMojoAnnotationsScanner
{
for ( Map.Entry<String, Object> entry : mojoFieldVisitor.getMojoAnnotationVisitor().getAnnotationValues().entrySet() )
{
reflector.invoke( componentAnnotationContent, entry.getKey(),
new Object[]{ entry.getValue() } );
String methodName = entry.getKey();
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(),

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;
public void execute()