Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MPLUGIN-427] Expose generics information of parameter types in report #159

Merged
merged 3 commits into from Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions maven-plugin-tools-annotations/pom.xml
Expand Up @@ -78,6 +78,10 @@
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-util</artifactId>
</dependency>
<!-- for HTML to plain text conversion -->
<dependency>
<groupId>org.jsoup</groupId>
Expand Down
Expand Up @@ -39,6 +39,7 @@
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.stream.Collectors;

import com.thoughtworks.qdox.JavaProjectBuilder;
import com.thoughtworks.qdox.library.SortedClassLibraryBuilder;
Expand Down Expand Up @@ -698,7 +699,13 @@ private List<MojoDescriptor> toMojoDescriptors( Map<String, MojoAnnotatedClass>
+ property, null );
}
parameter.setExpression( StringUtils.isEmpty( property ) ? "" : "${" + property + "}" );
parameter.setType( parameterAnnotationContent.getClassName() );
StringBuilder type = new StringBuilder( parameterAnnotationContent.getClassName() );
if ( !parameterAnnotationContent.getTypeParameters().isEmpty() )
{
type.append( parameterAnnotationContent.getTypeParameters().stream()
.collect( Collectors.joining( ", ", "<", ">" ) ) );
}
parameter.setType( type.toString() );
parameter.setSince( parameterAnnotationContent.getSince() );
parameter.setRequired( parameterAnnotationContent.required() );

Expand Down
Expand Up @@ -23,6 +23,7 @@
import org.objectweb.asm.Type;

import java.lang.annotation.Annotation;
import java.util.List;
import java.util.Objects;

/**
Expand Down Expand Up @@ -50,16 +51,20 @@ public class ParameterAnnotationContent

private String className;

public ParameterAnnotationContent( String fieldName, String className )
private final List<String> typeParameters;

public ParameterAnnotationContent( String fieldName, String className, List<String> typeParameters )
{
super( fieldName );
this.className = className;
this.typeParameters = typeParameters;
}

public ParameterAnnotationContent( String fieldName, String alias, String property, String defaultValue,
Class<?> implementation, boolean required, boolean readonly, String className )
Class<?> implementation, boolean required, boolean readonly, String className,
List<String> typeParameters )
{
this( fieldName, className );
this( fieldName, className, typeParameters );
this.alias = alias;
this.property = property;
this.defaultValue = defaultValue;
Expand Down Expand Up @@ -177,6 +182,11 @@ public void setClassName( String className )
this.className = className;
}

public List<String> getTypeParameters()
{
return typeParameters;
}

@Override
public String toString()
{
Expand All @@ -185,6 +195,7 @@ public String toString()
sb.append( "ParameterAnnotationContent" );
sb.append( "{fieldName='" ).append( getFieldName() ).append( '\'' );
sb.append( ", className='" ).append( getClassName() ).append( '\'' );
sb.append( ", typeParameters='" ).append( getTypeParameters() ).append( '\'' );
sb.append( ", name='" ).append( name ).append( '\'' );
sb.append( ", alias='" ).append( alias ).append( '\'' );
sb.append( ", alias='" ).append( alias ).append( '\'' );
Expand Down Expand Up @@ -230,6 +241,10 @@ public boolean equals( Object o )
return false;
}

if ( !Objects.equals( typeParameters, that.typeParameters ) )
{
return false;
}
if ( !Objects.equals( alias, that.alias ) )
{
return false;
Expand All @@ -253,7 +268,7 @@ public boolean equals( Object o )
@Override
public int hashCode()
{
return Objects.hash( alias, getFieldName(), property, defaultValue, required, readonly,
implementationClassName );
return Objects.hash( alias, getFieldName(), getClassName(), typeParameters, property, defaultValue, required,
readonly, implementationClassName );
}
}
Expand Up @@ -302,7 +302,8 @@ protected void analyzeVisitors( MojoClassVisitor mojoClassVisitor )
for ( MojoParameterVisitor parameterVisitor : mojoParameterVisitors )
{
ParameterAnnotationContent parameterAnnotationContent =
new ParameterAnnotationContent( parameterVisitor.getFieldName(), parameterVisitor.getClassName() );
new ParameterAnnotationContent( parameterVisitor.getFieldName(), parameterVisitor.getClassName(),
parameterVisitor.getTypeParameters() );

Map<String, MojoAnnotationVisitor> annotationVisitorMap = parameterVisitor.getAnnotationVisitorMap();
MojoAnnotationVisitor fieldAnnotationVisitor = annotationVisitorMap.get( Parameter.class.getName() );
Expand Down
Expand Up @@ -20,6 +20,8 @@
*/

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -36,6 +38,8 @@
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.signature.SignatureReader;
import org.objectweb.asm.util.TraceSignatureVisitor;

/**
* Visitor for Mojo classes.
Expand Down Expand Up @@ -117,11 +121,49 @@ public AnnotationVisitor visitAnnotation( String desc, boolean visible )
@Override
public FieldVisitor visitField( int access, String name, String desc, String signature, Object value )
{
MojoFieldVisitor mojoFieldVisitor = new MojoFieldVisitor( name, Type.getType( desc ).getClassName() );
List<String> typeParameters = extractTypeParameters( access, signature, true );
MojoFieldVisitor mojoFieldVisitor = new MojoFieldVisitor( name, Type.getType( desc ).getClassName(),
typeParameters );
fieldVisitors.add( mojoFieldVisitor );
return mojoFieldVisitor;
}

/**
* Parses the signature according to
* <a href="https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.3.4">JVMS 4.3.4</a>
* and returns the type parameters.
* @param access
* @param signature
* @param isField
* @return the list of type parameters (may be empty)
*/
private List<String> extractTypeParameters( int access, String signature, boolean isField )
{
if ( StringUtils.isEmpty( signature ) )
{
return Collections.emptyList();
}
TraceSignatureVisitor traceSignatureVisitor = new TraceSignatureVisitor( access );
SignatureReader signatureReader = new SignatureReader( signature );
if ( isField )
{
signatureReader.acceptType( traceSignatureVisitor );
}
else
{
signatureReader.accept( traceSignatureVisitor );
}
String declaration = traceSignatureVisitor.getDeclaration();
int startTypeParameters = declaration.indexOf( '<' );
if ( startTypeParameters == -1 )
{
return Collections.emptyList();
}
String typeParameters = declaration.substring( startTypeParameters + 1,
declaration.lastIndexOf( '>' ) );
return Arrays.asList( typeParameters.split( ", " ) );
}

@Override
public MethodVisitor visitMethod( int access, String name, String desc, String signature, String[] exceptions )
{
Expand All @@ -142,8 +184,9 @@ public MethodVisitor visitMethod( int access, String name, String desc, String s
{
String fieldName = StringUtils.lowercaseFirstLetter( name.substring( 3 ) );
String className = type.getArgumentTypes()[0].getClassName();
List<String> typeParameters = extractTypeParameters( access, signature, false );

MojoMethodVisitor mojoMethodVisitor = new MojoMethodVisitor( fieldName, className );
MojoMethodVisitor mojoMethodVisitor = new MojoMethodVisitor( fieldName, className, typeParameters );
methodVisitors.add( mojoMethodVisitor );
return mojoMethodVisitor;
}
Expand Down
Expand Up @@ -20,6 +20,7 @@
*/

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.maven.tools.plugin.extractor.annotations.scanner.MojoAnnotationsScanner;
Expand All @@ -43,11 +44,14 @@ public class MojoFieldVisitor

private String className;

MojoFieldVisitor( String fieldName, String className )
private final List<String> typeParameters;

MojoFieldVisitor( String fieldName, String className, List<String> typeParameters )
{
super( Opcodes.ASM9 );
this.fieldName = fieldName;
this.className = className;
this.typeParameters = typeParameters;
}

@Override
Expand All @@ -62,6 +66,12 @@ public String getFieldName()
return fieldName;
}

@Override
public List<String> getTypeParameters()
{
return typeParameters;
}

@Override
public AnnotationVisitor visitAnnotation( String desc, boolean visible )
{
Expand Down
Expand Up @@ -20,6 +20,7 @@
*/

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.maven.tools.plugin.extractor.annotations.scanner.MojoAnnotationsScanner;
Expand All @@ -37,14 +38,15 @@ public class MojoMethodVisitor extends MethodVisitor implements MojoParameterVis
{
private final String className;
private final String fieldName;

private final List<String> typeParameters;
private Map<String, MojoAnnotationVisitor> annotationVisitorMap = new HashMap<>();

public MojoMethodVisitor( String fieldName, String className )
public MojoMethodVisitor( String fieldName, String className, List<String> typeParameters )
{
super( Opcodes.ASM9 );
this.fieldName = fieldName;
this.className = className;
this.typeParameters = typeParameters;
}

@Override
Expand Down Expand Up @@ -73,6 +75,12 @@ public String getClassName()
return className;
}

@Override
public List<String> getTypeParameters()
{
return typeParameters;
}

@Override
public Map<String, MojoAnnotationVisitor> getAnnotationVisitorMap()
{
Expand Down
Expand Up @@ -19,6 +19,7 @@
* under the License.
*/

import java.util.List;
import java.util.Map;

/**
Expand All @@ -32,5 +33,7 @@ public interface MojoParameterVisitor

String getClassName();

List<String> getTypeParameters();

Map<String, MojoAnnotationVisitor> getAnnotationVisitorMap();
}
@@ -0,0 +1,66 @@
package org.apache.maven.tools.plugin.extractor.annotations;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

@Mojo( name = "parameter-with-generics" )
public class ParametersWithGenericsMojo
extends AbstractMojo
{

@Parameter
private String string;

@Parameter
private Map<String, Boolean> stringBooleanMap;

@Parameter
private Collection<Integer> integerCollection;

@Parameter
private Collection<Collection<String>> nestedStringCollection;

@Parameter
private Collection<Integer[]> integerArrayCollection;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
}

@Parameter( name="numberList" )
public void setNumberList(List<Number> numberList) {
}

public static class NestedClass<E extends Number> {
/**
* Some field without type parameter but non-empty signature
*/
protected E filter;
}
}
Expand Up @@ -98,25 +98,25 @@ void testReadMojoClass()
.hasSize( 6 )
.containsExactlyInAnyOrder(
new ParameterAnnotationContent( "bar", null, "thebar", "coolbar", null, true, false,
String.class.getName() ),
String.class.getName(), Collections.emptyList() ),
new ParameterAnnotationContent( "beer", null, "thebeer", "coolbeer", null, false, false,
String.class.getName() ),
String.class.getName(), Collections.emptyList() ),
new ParameterAnnotationContent( "fooInterface", null, "fooInterface", null,
FooInterfaceImpl.class,
false,
false, FooInterface.class.getName() ),
false, FooInterface.class.getName(), Collections.emptyList() ),
new ParameterAnnotationContent( "paramFromSetter", null, "props.paramFromSetter", null,
null,
false,
false, String.class.getName() ),
false, String.class.getName(), Collections.emptyList() ),
new ParameterAnnotationContent( "paramFromAdd", null, "props.paramFromAdd", null,
null,
false,
false, String.class.getName() ),
false, String.class.getName(), Collections.emptyList() ),
new ParameterAnnotationContent( "paramFromSetterDeprecated", null, "props.paramFromSetterDeprecated", null,
null,
false,
false, List.class.getName() )
false, List.class.getName(), Collections.singletonList("java.lang.String") )
);
}
}