diff --git a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/JavaAnnotationsMojoDescriptorExtractor.java b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/JavaAnnotationsMojoDescriptorExtractor.java index b70b91d3..facfe4a4 100644 --- a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/JavaAnnotationsMojoDescriptorExtractor.java +++ b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/JavaAnnotationsMojoDescriptorExtractor.java @@ -288,7 +288,8 @@ protected void populateDataFromJavadoc( JavaProjectBuilder javaProjectBuilder, } } - Map elementMap = extractParameterAnnotations( javaClass, javaClassesMap ); + Map fieldsMap = extractFieldsAnnotations( javaClass, javaClassesMap ); + Map methodsMap = extractMethodsAnnotations( javaClass, javaClassesMap ); // populate parameters Map parameters = @@ -296,7 +297,16 @@ protected void populateDataFromJavadoc( JavaProjectBuilder javaProjectBuilder, parameters = new TreeMap<>( parameters ); for ( Map.Entry parameter : parameters.entrySet() ) { - JavaAnnotatedElement element = elementMap.get( parameter.getKey() ); + JavaAnnotatedElement element; + if ( parameter.getValue().isAnnotationOnMethod() ) + { + element = methodsMap.get( parameter.getKey() ); + } + else + { + element = fieldsMap.get( parameter.getKey() ); + } + if ( element == null ) { continue; @@ -327,7 +337,7 @@ protected void populateDataFromJavadoc( JavaProjectBuilder javaProjectBuilder, Map components = entry.getValue().getComponents(); for ( Map.Entry component : components.entrySet() ) { - JavaAnnotatedElement element = elementMap.get( component.getKey() ); + JavaAnnotatedElement element = fieldsMap.get( component.getKey() ); if ( element == null ) { continue; @@ -423,13 +433,12 @@ private DocletTag findInClassHierarchy( JavaClass javaClass, String tagName ) /** * extract fields that are either parameters or components. - * Also extract methods that are parameters * * @param javaClass not null * @return map with Mojo parameters names as keys */ - private Map extractParameterAnnotations( JavaClass javaClass, - Map javaClassesMap ) + private Map extractFieldsAnnotations( JavaClass javaClass, + Map javaClassesMap ) { try { @@ -441,15 +450,15 @@ private Map extractParameterAnnotations( JavaClass if ( superClass != null ) { - if ( !superClass.getFields().isEmpty() || !superClass.getMethods().isEmpty() ) + if ( !superClass.getFields().isEmpty() ) { - rawParams = extractParameterAnnotations( superClass, javaClassesMap ); + rawParams = extractFieldsAnnotations( superClass, javaClassesMap ); } // maybe sources comes from scan of sources artifact superClass = javaClassesMap.get( superClass.getFullyQualifiedName() ); - if ( superClass != null && ( !superClass.getFields().isEmpty() || !superClass.getMethods().isEmpty() ) ) + if ( superClass != null && !superClass.getFields().isEmpty() ) { - rawParams = extractParameterAnnotations( superClass, javaClassesMap ); + rawParams = extractFieldsAnnotations( superClass, javaClassesMap ); } } else @@ -463,6 +472,51 @@ private Map extractParameterAnnotations( JavaClass rawParams.put( field.getName(), field ); } + return rawParams; + } + catch ( NoClassDefFoundError e ) + { + getLogger().warn( "Failed extracting parameters from " + javaClass ); + throw e; + } + } + + /** + * extract methods that are parameters. + * + * @param javaClass not null + * @return map with Mojo parameters names as keys + */ + private Map extractMethodsAnnotations( JavaClass javaClass, + Map javaClassesMap ) + { + try + { + Map rawParams = new TreeMap<>(); + + // we have to add the parent methods first, so that they will be overwritten by the local methods if + // that actually happens... + JavaClass superClass = javaClass.getSuperJavaClass(); + + if ( superClass != null ) + { + if ( !superClass.getMethods().isEmpty() ) + { + rawParams = extractMethodsAnnotations( superClass, javaClassesMap ); + } + // maybe sources comes from scan of sources artifact + superClass = javaClassesMap.get( superClass.getFullyQualifiedName() ); + if ( superClass != null && !superClass.getMethods().isEmpty() ) + { + rawParams = extractMethodsAnnotations( superClass, javaClassesMap ); + } + } + else + { + + rawParams = new TreeMap<>(); + } + for ( JavaMethod method : javaClass.getMethods() ) { if ( isPublicSetterMethod( method ) ) @@ -476,7 +530,7 @@ private Map extractParameterAnnotations( JavaClass } catch ( NoClassDefFoundError e ) { - getLogger().warn( "Failed extracting parameters from " + javaClass ); + getLogger().warn( "Failed extracting methods from " + javaClass ); throw e; } } diff --git a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/datamodel/ParameterAnnotationContent.java b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/datamodel/ParameterAnnotationContent.java index 82b125dc..99f3c47b 100644 --- a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/datamodel/ParameterAnnotationContent.java +++ b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/datamodel/ParameterAnnotationContent.java @@ -48,20 +48,24 @@ public class ParameterAnnotationContent private String className; + private boolean annotationOnMethod; + private final List typeParameters; - public ParameterAnnotationContent( String fieldName, String className, List typeParameters ) + public ParameterAnnotationContent( String fieldName, String className, List typeParameters, + boolean annotationOnMethod ) { super( fieldName ); this.className = className; this.typeParameters = typeParameters; + this.annotationOnMethod = annotationOnMethod; } public ParameterAnnotationContent( String fieldName, String alias, String property, String defaultValue, boolean required, boolean readonly, String className, - List typeParameters ) + List typeParameters, boolean annotationOnMethod ) { - this( fieldName, className, typeParameters ); + this( fieldName, className, typeParameters, annotationOnMethod ); this.alias = alias; this.property = property; this.defaultValue = defaultValue; @@ -156,6 +160,11 @@ public List getTypeParameters() return typeParameters; } + public boolean isAnnotationOnMethod() + { + return annotationOnMethod; + } + @Override public String toString() { @@ -172,6 +181,7 @@ public String toString() sb.append( ", defaultValue='" ).append( defaultValue ).append( '\'' ); sb.append( ", required=" ).append( required ); sb.append( ", readonly=" ).append( readonly ); + sb.append( ", methodSource=" ).append( annotationOnMethod ); sb.append( '}' ); return sb.toString(); } @@ -199,6 +209,11 @@ public boolean equals( Object o ) return false; } + if ( annotationOnMethod != that.annotationOnMethod ) + { + return false; + } + if ( getFieldName() != null ? !getFieldName().equals( that.getFieldName() ) : that.getFieldName() != null ) { return false; @@ -233,6 +248,6 @@ public boolean equals( Object o ) public int hashCode() { return Objects.hash( alias, getFieldName(), getClassName(), typeParameters, property, defaultValue, required, - readonly ); + readonly, annotationOnMethod ); } } diff --git a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/DefaultMojoAnnotationsScanner.java b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/DefaultMojoAnnotationsScanner.java index c0e3ed88..b681f8ec 100644 --- a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/DefaultMojoAnnotationsScanner.java +++ b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/DefaultMojoAnnotationsScanner.java @@ -303,7 +303,8 @@ protected void analyzeVisitors( MojoClassVisitor mojoClassVisitor ) { ParameterAnnotationContent parameterAnnotationContent = new ParameterAnnotationContent( parameterVisitor.getFieldName(), parameterVisitor.getClassName(), - parameterVisitor.getTypeParameters() ); + parameterVisitor.getTypeParameters(), + parameterVisitor.isAnnotationOnMethod() ); Map annotationVisitorMap = parameterVisitor.getAnnotationVisitorMap(); MojoAnnotationVisitor fieldAnnotationVisitor = annotationVisitorMap.get( Parameter.class.getName() ); diff --git a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/visitors/MojoFieldVisitor.java b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/visitors/MojoFieldVisitor.java index 3e017afc..1ac43b63 100644 --- a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/visitors/MojoFieldVisitor.java +++ b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/visitors/MojoFieldVisitor.java @@ -90,4 +90,10 @@ public String getClassName() { return className; } + + @Override + public boolean isAnnotationOnMethod() + { + return false; + } } diff --git a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/visitors/MojoMethodVisitor.java b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/visitors/MojoMethodVisitor.java index dd121f72..65bcbc77 100644 --- a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/visitors/MojoMethodVisitor.java +++ b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/visitors/MojoMethodVisitor.java @@ -86,4 +86,10 @@ public Map getAnnotationVisitorMap() { return annotationVisitorMap; } + + @Override + public boolean isAnnotationOnMethod() + { + return true; + } } diff --git a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/visitors/MojoParameterVisitor.java b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/visitors/MojoParameterVisitor.java index e9ba4057..f4f9d5b0 100644 --- a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/visitors/MojoParameterVisitor.java +++ b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/visitors/MojoParameterVisitor.java @@ -36,4 +36,6 @@ public interface MojoParameterVisitor List getTypeParameters(); Map getAnnotationVisitorMap(); + + boolean isAnnotationOnMethod(); } diff --git a/maven-plugin-tools-annotations/src/test/java/org/apache/maven/tools/plugin/extractor/annotations/FooMojo.java b/maven-plugin-tools-annotations/src/test/java/org/apache/maven/tools/plugin/extractor/annotations/FooMojo.java index e7265d12..9211afa2 100644 --- a/maven-plugin-tools-annotations/src/test/java/org/apache/maven/tools/plugin/extractor/annotations/FooMojo.java +++ b/maven-plugin-tools-annotations/src/test/java/org/apache/maven/tools/plugin/extractor/annotations/FooMojo.java @@ -45,6 +45,14 @@ public class FooMojo @Parameter( property = "thebar", required = true, defaultValue = "coolbar" ) protected String bar; + /** + * Setter method for Parameter field + */ + public void setBar( String bar ) + { + this.bar = bar; + } + /** * beer for non french folks * @deprecated wine is better @@ -53,13 +61,18 @@ public class FooMojo @Parameter( property = "thebeer", defaultValue = "coolbeer" ) protected String beer; + /** + * Field for setter method + */ + private String paramFromSetter; + /** * setter as parameter. */ @Parameter( property = "props.paramFromSetter" ) public void setParamFromSetter(String value) { - // empty + this.paramFromSetter = paramFromSetter; } /** diff --git a/maven-plugin-tools-annotations/src/test/java/org/apache/maven/tools/plugin/extractor/annotations/TestAnnotationsReader.java b/maven-plugin-tools-annotations/src/test/java/org/apache/maven/tools/plugin/extractor/annotations/TestAnnotationsReader.java index 0e075c66..e1ea1543 100644 --- a/maven-plugin-tools-annotations/src/test/java/org/apache/maven/tools/plugin/extractor/annotations/TestAnnotationsReader.java +++ b/maven-plugin-tools-annotations/src/test/java/org/apache/maven/tools/plugin/extractor/annotations/TestAnnotationsReader.java @@ -98,18 +98,18 @@ void testReadMojoClass() .hasSize( 5 ) .containsExactlyInAnyOrder( new ParameterAnnotationContent( "bar", null, "thebar", "coolbar", true, false, - String.class.getName(), Collections.emptyList() ), + String.class.getName(), Collections.emptyList(), false ), new ParameterAnnotationContent( "beer", null, "thebeer", "coolbeer", false, false, - String.class.getName(), Collections.emptyList() ), + String.class.getName(), Collections.emptyList(), false ), new ParameterAnnotationContent( "paramFromSetter", null, "props.paramFromSetter", null, false, - false, String.class.getName(), Collections.emptyList() ), + false, String.class.getName(), Collections.emptyList(), true ), new ParameterAnnotationContent( "paramFromAdd", null, "props.paramFromAdd", null, false, - false, String.class.getName(), Collections.emptyList() ), + false, String.class.getName(), Collections.emptyList(), true ), new ParameterAnnotationContent( "paramFromSetterDeprecated", null, "props.paramFromSetterDeprecated", null, false, - false, List.class.getName(), Collections.singletonList("java.lang.String") ) + false, List.class.getName(), Collections.singletonList("java.lang.String"), true ) ); } }