Skip to content

Commit 76d99af

Browse files
[MPLUGIN-438] Parameter description should be taken from annotated item
1 parent 5b60490 commit 76d99af

File tree

8 files changed

+119
-22
lines changed

8 files changed

+119
-22
lines changed

maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/JavaAnnotationsMojoDescriptorExtractor.java

+65-11
Original file line numberDiff line numberDiff line change
@@ -288,15 +288,25 @@ protected void populateDataFromJavadoc( JavaProjectBuilder javaProjectBuilder,
288288
}
289289
}
290290

291-
Map<String, JavaAnnotatedElement> elementMap = extractParameterAnnotations( javaClass, javaClassesMap );
291+
Map<String, JavaAnnotatedElement> fieldsMap = extractFieldsAnnotations( javaClass, javaClassesMap );
292+
Map<String, JavaAnnotatedElement> methodsMap = extractMethodsAnnotations( javaClass, javaClassesMap );
292293

293294
// populate parameters
294295
Map<String, ParameterAnnotationContent> parameters =
295296
getParametersParentHierarchy( entry.getValue(), mojoAnnotatedClasses );
296297
parameters = new TreeMap<>( parameters );
297298
for ( Map.Entry<String, ParameterAnnotationContent> parameter : parameters.entrySet() )
298299
{
299-
JavaAnnotatedElement element = elementMap.get( parameter.getKey() );
300+
JavaAnnotatedElement element;
301+
if ( parameter.getValue().isAnnotationOnMethod() )
302+
{
303+
element = methodsMap.get( parameter.getKey() );
304+
}
305+
else
306+
{
307+
element = fieldsMap.get( parameter.getKey() );
308+
}
309+
300310
if ( element == null )
301311
{
302312
continue;
@@ -327,7 +337,7 @@ protected void populateDataFromJavadoc( JavaProjectBuilder javaProjectBuilder,
327337
Map<String, ComponentAnnotationContent> components = entry.getValue().getComponents();
328338
for ( Map.Entry<String, ComponentAnnotationContent> component : components.entrySet() )
329339
{
330-
JavaAnnotatedElement element = elementMap.get( component.getKey() );
340+
JavaAnnotatedElement element = fieldsMap.get( component.getKey() );
331341
if ( element == null )
332342
{
333343
continue;
@@ -423,13 +433,12 @@ private DocletTag findInClassHierarchy( JavaClass javaClass, String tagName )
423433

424434
/**
425435
* extract fields that are either parameters or components.
426-
* Also extract methods that are parameters
427436
*
428437
* @param javaClass not null
429438
* @return map with Mojo parameters names as keys
430439
*/
431-
private Map<String, JavaAnnotatedElement> extractParameterAnnotations( JavaClass javaClass,
432-
Map<String, JavaClass> javaClassesMap )
440+
private Map<String, JavaAnnotatedElement> extractFieldsAnnotations( JavaClass javaClass,
441+
Map<String, JavaClass> javaClassesMap )
433442
{
434443
try
435444
{
@@ -441,15 +450,15 @@ private Map<String, JavaAnnotatedElement> extractParameterAnnotations( JavaClass
441450

442451
if ( superClass != null )
443452
{
444-
if ( !superClass.getFields().isEmpty() || !superClass.getMethods().isEmpty() )
453+
if ( !superClass.getFields().isEmpty() )
445454
{
446-
rawParams = extractParameterAnnotations( superClass, javaClassesMap );
455+
rawParams = extractFieldsAnnotations( superClass, javaClassesMap );
447456
}
448457
// maybe sources comes from scan of sources artifact
449458
superClass = javaClassesMap.get( superClass.getFullyQualifiedName() );
450-
if ( superClass != null && ( !superClass.getFields().isEmpty() || !superClass.getMethods().isEmpty() ) )
459+
if ( superClass != null && !superClass.getFields().isEmpty() )
451460
{
452-
rawParams = extractParameterAnnotations( superClass, javaClassesMap );
461+
rawParams = extractFieldsAnnotations( superClass, javaClassesMap );
453462
}
454463
}
455464
else
@@ -463,6 +472,51 @@ private Map<String, JavaAnnotatedElement> extractParameterAnnotations( JavaClass
463472
rawParams.put( field.getName(), field );
464473
}
465474

475+
return rawParams;
476+
}
477+
catch ( NoClassDefFoundError e )
478+
{
479+
getLogger().warn( "Failed extracting parameters from " + javaClass );
480+
throw e;
481+
}
482+
}
483+
484+
/**
485+
* extract methods that are parameters.
486+
*
487+
* @param javaClass not null
488+
* @return map with Mojo parameters names as keys
489+
*/
490+
private Map<String, JavaAnnotatedElement> extractMethodsAnnotations( JavaClass javaClass,
491+
Map<String, JavaClass> javaClassesMap )
492+
{
493+
try
494+
{
495+
Map<String, JavaAnnotatedElement> rawParams = new TreeMap<>();
496+
497+
// we have to add the parent methods first, so that they will be overwritten by the local methods if
498+
// that actually happens...
499+
JavaClass superClass = javaClass.getSuperJavaClass();
500+
501+
if ( superClass != null )
502+
{
503+
if ( !superClass.getMethods().isEmpty() )
504+
{
505+
rawParams = extractMethodsAnnotations( superClass, javaClassesMap );
506+
}
507+
// maybe sources comes from scan of sources artifact
508+
superClass = javaClassesMap.get( superClass.getFullyQualifiedName() );
509+
if ( superClass != null && !superClass.getMethods().isEmpty() )
510+
{
511+
rawParams = extractMethodsAnnotations( superClass, javaClassesMap );
512+
}
513+
}
514+
else
515+
{
516+
517+
rawParams = new TreeMap<>();
518+
}
519+
466520
for ( JavaMethod method : javaClass.getMethods() )
467521
{
468522
if ( isPublicSetterMethod( method ) )
@@ -476,7 +530,7 @@ private Map<String, JavaAnnotatedElement> extractParameterAnnotations( JavaClass
476530
}
477531
catch ( NoClassDefFoundError e )
478532
{
479-
getLogger().warn( "Failed extracting parameters from " + javaClass );
533+
getLogger().warn( "Failed extracting methods from " + javaClass );
480534
throw e;
481535
}
482536
}

maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/datamodel/ParameterAnnotationContent.java

+19-4
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,24 @@ public class ParameterAnnotationContent
4848

4949
private String className;
5050

51+
private boolean annotationOnMethod;
52+
5153
private final List<String> typeParameters;
5254

53-
public ParameterAnnotationContent( String fieldName, String className, List<String> typeParameters )
55+
public ParameterAnnotationContent( String fieldName, String className, List<String> typeParameters,
56+
boolean annotationOnMethod )
5457
{
5558
super( fieldName );
5659
this.className = className;
5760
this.typeParameters = typeParameters;
61+
this.annotationOnMethod = annotationOnMethod;
5862
}
5963

6064
public ParameterAnnotationContent( String fieldName, String alias, String property, String defaultValue,
6165
boolean required, boolean readonly, String className,
62-
List<String> typeParameters )
66+
List<String> typeParameters, boolean annotationOnMethod )
6367
{
64-
this( fieldName, className, typeParameters );
68+
this( fieldName, className, typeParameters, annotationOnMethod );
6569
this.alias = alias;
6670
this.property = property;
6771
this.defaultValue = defaultValue;
@@ -156,6 +160,11 @@ public List<String> getTypeParameters()
156160
return typeParameters;
157161
}
158162

163+
public boolean isAnnotationOnMethod()
164+
{
165+
return annotationOnMethod;
166+
}
167+
159168
@Override
160169
public String toString()
161170
{
@@ -172,6 +181,7 @@ public String toString()
172181
sb.append( ", defaultValue='" ).append( defaultValue ).append( '\'' );
173182
sb.append( ", required=" ).append( required );
174183
sb.append( ", readonly=" ).append( readonly );
184+
sb.append( ", methodSource=" ).append( annotationOnMethod );
175185
sb.append( '}' );
176186
return sb.toString();
177187
}
@@ -199,6 +209,11 @@ public boolean equals( Object o )
199209
return false;
200210
}
201211

212+
if ( annotationOnMethod != that.annotationOnMethod )
213+
{
214+
return false;
215+
}
216+
202217
if ( getFieldName() != null ? !getFieldName().equals( that.getFieldName() ) : that.getFieldName() != null )
203218
{
204219
return false;
@@ -233,6 +248,6 @@ public boolean equals( Object o )
233248
public int hashCode()
234249
{
235250
return Objects.hash( alias, getFieldName(), getClassName(), typeParameters, property, defaultValue, required,
236-
readonly );
251+
readonly, annotationOnMethod );
237252
}
238253
}

maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/DefaultMojoAnnotationsScanner.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,8 @@ protected void analyzeVisitors( MojoClassVisitor mojoClassVisitor )
303303
{
304304
ParameterAnnotationContent parameterAnnotationContent =
305305
new ParameterAnnotationContent( parameterVisitor.getFieldName(), parameterVisitor.getClassName(),
306-
parameterVisitor.getTypeParameters() );
306+
parameterVisitor.getTypeParameters(),
307+
parameterVisitor.isAnnotationOnMethod() );
307308

308309
Map<String, MojoAnnotationVisitor> annotationVisitorMap = parameterVisitor.getAnnotationVisitorMap();
309310
MojoAnnotationVisitor fieldAnnotationVisitor = annotationVisitorMap.get( Parameter.class.getName() );

maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/visitors/MojoFieldVisitor.java

+6
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,10 @@ public String getClassName()
9090
{
9191
return className;
9292
}
93+
94+
@Override
95+
public boolean isAnnotationOnMethod()
96+
{
97+
return false;
98+
}
9399
}

maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/visitors/MojoMethodVisitor.java

+6
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,10 @@ public Map<String, MojoAnnotationVisitor> getAnnotationVisitorMap()
8686
{
8787
return annotationVisitorMap;
8888
}
89+
90+
@Override
91+
public boolean isAnnotationOnMethod()
92+
{
93+
return true;
94+
}
8995
}

maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/scanner/visitors/MojoParameterVisitor.java

+2
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,6 @@ public interface MojoParameterVisitor
3636
List<String> getTypeParameters();
3737

3838
Map<String, MojoAnnotationVisitor> getAnnotationVisitorMap();
39+
40+
boolean isAnnotationOnMethod();
3941
}

maven-plugin-tools-annotations/src/test/java/org/apache/maven/tools/plugin/extractor/annotations/FooMojo.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ public class FooMojo
4545
@Parameter( property = "thebar", required = true, defaultValue = "coolbar" )
4646
protected String bar;
4747

48+
/**
49+
* Setter method for Parameter field
50+
*/
51+
public void setBar( String bar )
52+
{
53+
this.bar = bar;
54+
}
55+
4856
/**
4957
* beer for non french folks
5058
* @deprecated wine is better
@@ -53,13 +61,18 @@ public class FooMojo
5361
@Parameter( property = "thebeer", defaultValue = "coolbeer" )
5462
protected String beer;
5563

64+
/**
65+
* Field for setter method
66+
*/
67+
private String paramFromSetter;
68+
5669
/**
5770
* setter as parameter.
5871
*/
5972
@Parameter( property = "props.paramFromSetter" )
6073
public void setParamFromSetter(String value)
6174
{
62-
// empty
75+
this.paramFromSetter = paramFromSetter;
6376
}
6477

6578
/**

maven-plugin-tools-annotations/src/test/java/org/apache/maven/tools/plugin/extractor/annotations/TestAnnotationsReader.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,18 @@ void testReadMojoClass()
9898
.hasSize( 5 )
9999
.containsExactlyInAnyOrder(
100100
new ParameterAnnotationContent( "bar", null, "thebar", "coolbar", true, false,
101-
String.class.getName(), Collections.emptyList() ),
101+
String.class.getName(), Collections.emptyList(), false ),
102102
new ParameterAnnotationContent( "beer", null, "thebeer", "coolbeer", false, false,
103-
String.class.getName(), Collections.emptyList() ),
103+
String.class.getName(), Collections.emptyList(), false ),
104104
new ParameterAnnotationContent( "paramFromSetter", null, "props.paramFromSetter", null,
105105
false,
106-
false, String.class.getName(), Collections.emptyList() ),
106+
false, String.class.getName(), Collections.emptyList(), true ),
107107
new ParameterAnnotationContent( "paramFromAdd", null, "props.paramFromAdd", null,
108108
false,
109-
false, String.class.getName(), Collections.emptyList() ),
109+
false, String.class.getName(), Collections.emptyList(), true ),
110110
new ParameterAnnotationContent( "paramFromSetterDeprecated", null, "props.paramFromSetterDeprecated", null,
111111
false,
112-
false, List.class.getName(), Collections.singletonList("java.lang.String") )
112+
false, List.class.getName(), Collections.singletonList("java.lang.String"), true )
113113
);
114114
}
115115
}

0 commit comments

Comments
 (0)