Taras Leskiv
Taras Leskiv

Reputation: 1875

JavaParser AnnotationExpr get parameters

I have an AnnotationExpr, how do I get parameters and their values of the annotation (e.g. @UnityBridge(fullClassName = "test") - how do I obtain a value of fullClassName parameter). Does JavaParser support this?

Do I have to accept another visitor? Which one in this case?

Upvotes: 3

Views: 3453

Answers (3)

Jackkobec
Jackkobec

Reputation: 6705

The simplest solution is:

import com.github.javaparser.StaticJavaParser
import com.github.javaparser.ast.CompilationUnit
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration
import com.github.javaparser.ast.expr.AnnotationExpr
import com.github.javaparser.ast.NodeList
import com.github.javaparser.ast.expr.MemberValuePair

// Annotation
public @interface AnnotationName {
  String argumentName();
}

// Class with this annotation
@AnnotationName(argumentName = "yourValue")
public class ClassWithAnnotationName {}

// Parse class with annotation
CompilationUnit compilationUnit = StaticJavaParser.parse(sourceFile);
Optional<ClassOrInterfaceDeclaration> classInterfaceForParse = compilationUnit.getInterfaceByName("ClassWithAnnotationName");

// Get annotation by name
final AnnotationExpr messageQueueKeyAnnotation =
        classInterfaceForParse.get().getAnnotationByName("AnnotationName").get();

// Get all parameters. It doesn't matter how many.
final NodeList<MemberValuePair> annotationParameters = messageQueueKeyAnnotation.toNormalAnnotationExpr().get().pairs;

// Read annotation parameter from the list of all parameters
final String argumentName = annotationParameters.get(0).value;

Upvotes: -1

Spille
Spille

Reputation: 587

I prefer this approach without instanceof and searching children by type instead, although you still need a distinction for a single parameter without key to find the Parameter "value":

private static final String VALUE = "value";

public static Expression getValueParameter(AnnotationExpr annotationExpr){
    Expression expression = getParamater(annotationExpr, VALUE);
    if(expression == null){
        List<Expression> children = annotationExpr.getChildNodesByType(Expression.class);
        if(!children.isEmpty()){
            expression = children.get(0);
        }
    }
    return expression;
}

public static Expression getParamater(AnnotationExpr annotationExpr, String parameterName){
    List<MemberValuePair>children = annotationExpr.getChildNodesByType(MemberValuePair.class);
    for(MemberValuePair memberValuePair : children){
        if(parameterName.equals(memberValuePair.getNameAsString())){
            return memberValuePair.getValue();
        }
    }
    return null;
}

Upvotes: 0

notsyncing
notsyncing

Reputation: 106

Late answer, I came into the same problem, just cast the AnnotationExpr to one of following:

MarkerAnnotationExpr (for no parameter),

SingleMemberAnnotationExpr (for single parameter),

NormalAnnotationExpr (for multiple parameters).

You may need instanceof to determine current annotation type.

Upvotes: 7

Related Questions