This is an automated email from the ASF dual-hosted git repository.

sjaranowski pushed a commit to branch MPLUGIN-438
in repository https://gitbox.apache.org/repos/asf/maven-plugin-tools.git

commit 1b82615447eefd494a1ac68f64d7af76a06e76ca
Author: Slawomir Jaranowski <s.jaranow...@gmail.com>
AuthorDate: Sat Oct 29 10:17:10 2022 +0200

    [MPLUGIN-438] Parameter description should be taken from annotated item
---
 .../JavaAnnotationsMojoDescriptorExtractor.java    | 76 ++++++++++++++++++----
 .../datamodel/ParameterAnnotationContent.java      | 23 +++++--
 .../scanner/DefaultMojoAnnotationsScanner.java     |  3 +-
 .../scanner/visitors/MojoFieldVisitor.java         |  6 ++
 .../scanner/visitors/MojoMethodVisitor.java        |  6 ++
 .../scanner/visitors/MojoParameterVisitor.java     |  2 +
 .../plugin/extractor/annotations/FooMojo.java      | 15 ++++-
 .../annotations/TestAnnotationsReader.java         | 10 +--
 8 files changed, 119 insertions(+), 22 deletions(-)

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..a228313c 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 @@ public class JavaAnnotationsMojoDescriptorExtractor
                 }
             }
 
-            Map<String, JavaAnnotatedElement> elementMap = 
extractParameterAnnotations( javaClass, javaClassesMap );
+            Map<String, JavaAnnotatedElement> fieldsMap = 
extractFieldsAnnotations( javaClass, javaClassesMap );
+            Map<String, JavaAnnotatedElement> methodsMap = 
extractMethodsAnnotations( javaClass, javaClassesMap );
 
             // populate parameters
             Map<String, ParameterAnnotationContent> parameters =
@@ -296,7 +297,16 @@ public class JavaAnnotationsMojoDescriptorExtractor
             parameters = new TreeMap<>( parameters );
             for ( Map.Entry<String, ParameterAnnotationContent> parameter : 
parameters.entrySet() )
             {
-                JavaAnnotatedElement element = elementMap.get( 
parameter.getKey() );
+                JavaAnnotatedElement element;
+                if ( parameter.getValue().isMethodSource() )
+                {
+                    element = methodsMap.get( parameter.getKey() );
+                }
+                else
+                {
+                    element = fieldsMap.get( parameter.getKey() );
+                }
+
                 if ( element == null )
                 {
                     continue;
@@ -327,7 +337,7 @@ public class JavaAnnotationsMojoDescriptorExtractor
             Map<String, ComponentAnnotationContent> components = 
entry.getValue().getComponents();
             for ( Map.Entry<String, ComponentAnnotationContent> component : 
components.entrySet() )
             {
-                JavaAnnotatedElement element = elementMap.get( 
component.getKey() );
+                JavaAnnotatedElement element = fieldsMap.get( 
component.getKey() );
                 if ( element == null )
                 {
                     continue;
@@ -423,13 +433,12 @@ public class JavaAnnotationsMojoDescriptorExtractor
 
     /**
      * 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<String, JavaAnnotatedElement> extractParameterAnnotations( 
JavaClass javaClass,
-                                                                 Map<String, 
JavaClass> javaClassesMap )
+    private Map<String, JavaAnnotatedElement> extractFieldsAnnotations( 
JavaClass javaClass,
+                                                                        
Map<String, JavaClass> javaClassesMap )
     {
         try
         {
@@ -441,15 +450,15 @@ public class JavaAnnotationsMojoDescriptorExtractor
 
             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 @@ public class JavaAnnotationsMojoDescriptorExtractor
                 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<String, JavaAnnotatedElement> extractMethodsAnnotations( 
JavaClass javaClass,
+                                                                        
Map<String, JavaClass> javaClassesMap )
+    {
+        try
+        {
+            Map<String, JavaAnnotatedElement> rawParams = new TreeMap<>();
+
+            // we have to add the parent fields first, so that they will be 
overwritten by the local fields 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 @@ public class JavaAnnotationsMojoDescriptorExtractor
         }
         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..9f68bd67 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 methodSource;
+
     private final List<String> typeParameters;
 
-    public ParameterAnnotationContent( String fieldName, String className, 
List<String> typeParameters )
+    public ParameterAnnotationContent( String fieldName, String className, 
List<String> typeParameters,
+                                       boolean methodSource )
     {
         super( fieldName );
         this.className = className;
         this.typeParameters = typeParameters;
+        this.methodSource = methodSource;
     }
 
     public ParameterAnnotationContent( String fieldName, String alias, String 
property, String defaultValue,
                                        boolean required, boolean readonly, 
String className,
-                                       List<String> typeParameters )
+                                       List<String> typeParameters, boolean 
methodSource )
     {
-        this( fieldName, className, typeParameters );
+        this( fieldName, className, typeParameters, methodSource );
         this.alias = alias;
         this.property = property;
         this.defaultValue = defaultValue;
@@ -156,6 +160,11 @@ public class ParameterAnnotationContent
         return typeParameters;
     }
 
+    public boolean isMethodSource()
+    {
+        return methodSource;
+    }
+
     @Override
     public String toString()
     {
@@ -172,6 +181,7 @@ public class ParameterAnnotationContent
         sb.append( ", defaultValue='" ).append( defaultValue ).append( '\'' );
         sb.append( ", required=" ).append( required );
         sb.append( ", readonly=" ).append( readonly );
+        sb.append( ", methodSource=" ).append( methodSource );
         sb.append( '}' );
         return sb.toString();
     }
@@ -199,6 +209,11 @@ public class ParameterAnnotationContent
             return false;
         }
 
+        if ( methodSource != that.methodSource )
+        {
+            return false;
+        }
+
         if ( getFieldName() != null ? !getFieldName().equals( 
that.getFieldName() ) : that.getFieldName() != null )
         {
             return false;
@@ -233,6 +248,6 @@ public class ParameterAnnotationContent
     public int hashCode()
     {
         return Objects.hash( alias, getFieldName(), getClassName(), 
typeParameters, property, defaultValue, required,
-                             readonly );
+                             readonly, methodSource );
     }
 }
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..6384b5dc 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 @@ public class DefaultMojoAnnotationsScanner
             {
                 ParameterAnnotationContent parameterAnnotationContent =
                     new ParameterAnnotationContent( 
parameterVisitor.getFieldName(), parameterVisitor.getClassName(),
-                                                    
parameterVisitor.getTypeParameters() );
+                                                    
parameterVisitor.getTypeParameters(),
+                                                    
parameterVisitor.isMethodSource() );
 
                 Map<String, MojoAnnotationVisitor> 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..e37ce1c4 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 class MojoFieldVisitor
     {
         return className;
     }
+
+    @Override
+    public boolean isMethodSource()
+    {
+        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..3d7bf31f 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 class MojoMethodVisitor extends MethodVisitor 
implements MojoParameterVis
     {
         return annotationVisitorMap;
     }
+
+    @Override
+    public boolean isMethodSource()
+    {
+        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..9068ef62 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<String> getTypeParameters();
 
     Map<String, MojoAnnotationVisitor> getAnnotationVisitorMap();
+
+    boolean isMethodSource();
 }
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 @@ class TestAnnotationsReader
             .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 )
             );
     }
 }

Reply via email to