Skip to content

Commit

Permalink
[MPLUGIN-438] Parameter description should be taken from annotated item
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed Oct 29, 2022
1 parent 5b60490 commit 76d99af
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -288,15 +288,25 @@ protected void populateDataFromJavadoc( JavaProjectBuilder javaProjectBuilder,
}
}

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 =
getParametersParentHierarchy( entry.getValue(), mojoAnnotatedClasses );
parameters = new TreeMap<>( parameters );
for ( Map.Entry<String, ParameterAnnotationContent> 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;
Expand Down Expand Up @@ -327,7 +337,7 @@ protected void populateDataFromJavadoc( JavaProjectBuilder javaProjectBuilder,
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;
Expand Down Expand Up @@ -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<String, JavaAnnotatedElement> extractParameterAnnotations( JavaClass javaClass,
Map<String, JavaClass> javaClassesMap )
private Map<String, JavaAnnotatedElement> extractFieldsAnnotations( JavaClass javaClass,
Map<String, JavaClass> javaClassesMap )
{
try
{
Expand All @@ -441,15 +450,15 @@ private Map<String, JavaAnnotatedElement> 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
Expand All @@ -463,6 +472,51 @@ private Map<String, JavaAnnotatedElement> 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<String, JavaAnnotatedElement> extractMethodsAnnotations( JavaClass javaClass,
Map<String, JavaClass> javaClassesMap )
{
try
{
Map<String, JavaAnnotatedElement> 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 ) )
Expand All @@ -476,7 +530,7 @@ private Map<String, JavaAnnotatedElement> extractParameterAnnotations( JavaClass
}
catch ( NoClassDefFoundError e )
{
getLogger().warn( "Failed extracting parameters from " + javaClass );
getLogger().warn( "Failed extracting methods from " + javaClass );
throw e;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,24 @@ public class ParameterAnnotationContent

private String className;

private boolean annotationOnMethod;

private final List<String> typeParameters;

public ParameterAnnotationContent( String fieldName, String className, List<String> typeParameters )
public ParameterAnnotationContent( String fieldName, String className, List<String> 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<String> typeParameters )
List<String> typeParameters, boolean annotationOnMethod )
{
this( fieldName, className, typeParameters );
this( fieldName, className, typeParameters, annotationOnMethod );
this.alias = alias;
this.property = property;
this.defaultValue = defaultValue;
Expand Down Expand Up @@ -156,6 +160,11 @@ public List<String> getTypeParameters()
return typeParameters;
}

public boolean isAnnotationOnMethod()
{
return annotationOnMethod;
}

@Override
public String toString()
{
Expand All @@ -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();
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,8 @@ protected void analyzeVisitors( MojoClassVisitor mojoClassVisitor )
{
ParameterAnnotationContent parameterAnnotationContent =
new ParameterAnnotationContent( parameterVisitor.getFieldName(), parameterVisitor.getClassName(),
parameterVisitor.getTypeParameters() );
parameterVisitor.getTypeParameters(),
parameterVisitor.isAnnotationOnMethod() );

Map<String, MojoAnnotationVisitor> annotationVisitorMap = parameterVisitor.getAnnotationVisitorMap();
MojoAnnotationVisitor fieldAnnotationVisitor = annotationVisitorMap.get( Parameter.class.getName() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,10 @@ public String getClassName()
{
return className;
}

@Override
public boolean isAnnotationOnMethod()
{
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,10 @@ public Map<String, MojoAnnotationVisitor> getAnnotationVisitorMap()
{
return annotationVisitorMap;
}

@Override
public boolean isAnnotationOnMethod()
{
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ public interface MojoParameterVisitor
List<String> getTypeParameters();

Map<String, MojoAnnotationVisitor> getAnnotationVisitorMap();

boolean isAnnotationOnMethod();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
);
}
}

0 comments on commit 76d99af

Please sign in to comment.