Search in sources :

Example 1 with SingleMemberAnnotationExpr

use of com.github.antlrjavaparser.api.expr.SingleMemberAnnotationExpr in project spring-roo by spring-projects.

the class JavaParserUtils method getNameExpr.

/**
 * Obtains the name expression ({@link NameExpr}) for the passed
 * {@link AnnotationExpr}, which is the annotation's type.
 *
 * @param annotationExpr to retrieve the type name from (required)
 * @return the name (never null)
 */
public static NameExpr getNameExpr(final AnnotationExpr annotationExpr) {
    Validate.notNull(annotationExpr, "Annotation expression required");
    if (annotationExpr instanceof MarkerAnnotationExpr) {
        final MarkerAnnotationExpr a = (MarkerAnnotationExpr) annotationExpr;
        final NameExpr nameToFind = a.getName();
        Validate.notNull(nameToFind, "Unable to determine annotation name from '%s'", annotationExpr);
        return nameToFind;
    } else if (annotationExpr instanceof SingleMemberAnnotationExpr) {
        final SingleMemberAnnotationExpr a = (SingleMemberAnnotationExpr) annotationExpr;
        final NameExpr nameToFind = a.getName();
        Validate.notNull(nameToFind, "Unable to determine annotation name from '%s'", annotationExpr);
        return nameToFind;
    } else if (annotationExpr instanceof NormalAnnotationExpr) {
        final NormalAnnotationExpr a = (NormalAnnotationExpr) annotationExpr;
        final NameExpr nameToFind = a.getName();
        Validate.notNull(nameToFind, "Unable to determine annotation name from '%s'", annotationExpr);
        return nameToFind;
    }
    throw new UnsupportedOperationException("Unknown annotation expression type '" + annotationExpr.getClass().getName() + "'");
}
Also used : SingleMemberAnnotationExpr(com.github.antlrjavaparser.api.expr.SingleMemberAnnotationExpr) NameExpr(com.github.antlrjavaparser.api.expr.NameExpr) QualifiedNameExpr(com.github.antlrjavaparser.api.expr.QualifiedNameExpr) MarkerAnnotationExpr(com.github.antlrjavaparser.api.expr.MarkerAnnotationExpr) NormalAnnotationExpr(com.github.antlrjavaparser.api.expr.NormalAnnotationExpr)

Example 2 with SingleMemberAnnotationExpr

use of com.github.antlrjavaparser.api.expr.SingleMemberAnnotationExpr in project spring-roo by spring-projects.

the class JavaParserAnnotationMetadataBuilder method addAnnotationToList.

/**
 * Facilitates the addition of the annotation to the presented type.
 *
 * @param compilationUnitServices to use (required)
 * @param annotations to add to the end of (required)
 * @param annotation to add (required)
 */
public static void addAnnotationToList(final CompilationUnitServices compilationUnitServices, final List<AnnotationExpr> annotations, final AnnotationMetadata annotation) {
    Validate.notNull(compilationUnitServices, "Compilation unit services required");
    Validate.notNull(annotations, "Annotations required");
    Validate.notNull(annotation, "Annotation metadata required");
    // ROO-3678: Add-on which include new annotation should be the responsible to check if annotation
    // exists, not JavaParser.
    /*// Create a holder for the annotation we're going to create
    boolean foundExisting = false;

    // Search for an existing annotation of this type
    for (final AnnotationExpr candidate : annotations) {
        NameExpr existingName = null;
        if (candidate instanceof NormalAnnotationExpr) {
            existingName = ((NormalAnnotationExpr) candidate).getName();
        }
        else if (candidate instanceof MarkerAnnotationExpr) {
            existingName = ((MarkerAnnotationExpr) candidate).getName();
        }
        else if (candidate instanceof SingleMemberAnnotationExpr) {
            existingName = ((SingleMemberAnnotationExpr) candidate)
                    .getName();
        }

        // Convert the candidate annotation type's into a JavaType
        final JavaType javaType = JavaParserUtils.getJavaType(
                compilationUnitServices, existingName, null);
        if (annotation.getAnnotationType().equals(javaType)) {
            foundExisting = true;
            break;
        }
    }
    Validate.isTrue(!foundExisting,
            "Found an existing annotation for type '%s'",
            annotation.getAnnotationType());*/
    // Import the annotation type, if needed
    final NameExpr nameToUse = JavaParserUtils.importTypeIfRequired(compilationUnitServices.getEnclosingTypeName(), compilationUnitServices.getImports(), annotation.getAnnotationType());
    // Create member-value pairs in accordance with Java Parser requirements
    final List<MemberValuePair> memberValuePairs = new ArrayList<MemberValuePair>();
    for (final JavaSymbolName attributeName : annotation.getAttributeNames()) {
        final AnnotationAttributeValue<?> value = annotation.getAttribute(attributeName);
        Validate.notNull(value, "Unable to acquire value '%s' from annotation", attributeName);
        final MemberValuePair memberValuePair = convert(value, compilationUnitServices);
        // "Member value pair should have been set");
        if (memberValuePair != null) {
            memberValuePairs.add(memberValuePair);
        }
    }
    // Create the AnnotationExpr; it varies depending on how many
    // member-value pairs we need to present
    AnnotationExpr annotationExpression = null;
    if (memberValuePairs.isEmpty()) {
        annotationExpression = new MarkerAnnotationExpr(nameToUse);
    } else if (memberValuePairs.size() == 1 && (memberValuePairs.get(0).getName() == null || "value".equals(memberValuePairs.get(0).getName()))) {
        final Expression toUse = JavaParserUtils.importExpressionIfRequired(compilationUnitServices.getEnclosingTypeName(), compilationUnitServices.getImports(), memberValuePairs.get(0).getValue());
        annotationExpression = new SingleMemberAnnotationExpr(nameToUse, toUse);
    } else {
        // We have a number of pairs being presented
        annotationExpression = new NormalAnnotationExpr(nameToUse, new ArrayList<MemberValuePair>());
    }
    // Add our AnnotationExpr to the actual annotations that will eventually
    // be flushed through to the compilation unit
    JavaParserCommentMetadataBuilder.updateCommentsToJavaParser(annotationExpression, annotation.getCommentStructure());
    annotations.add(annotationExpression);
    // Add member-value pairs to our AnnotationExpr
    if (!memberValuePairs.isEmpty()) {
        // SingleMemberAnnotationExpr
        if (annotationExpression instanceof MarkerAnnotationExpr) {
            final MarkerAnnotationExpr mae = (MarkerAnnotationExpr) annotationExpression;
            annotations.remove(mae);
            if (memberValuePairs.size() == 1 && (memberValuePairs.get(0).getName() == null || "value".equals(memberValuePairs.get(0).getName()))) {
                final Expression toUse = JavaParserUtils.importExpressionIfRequired(compilationUnitServices.getEnclosingTypeName(), compilationUnitServices.getImports(), memberValuePairs.get(0).getValue());
                annotationExpression = new SingleMemberAnnotationExpr(nameToUse, toUse);
                JavaParserCommentMetadataBuilder.updateCommentsToJavaParser(annotationExpression, annotation.getCommentStructure());
                annotations.add(annotationExpression);
            } else {
                // We have a number of pairs being presented
                annotationExpression = new NormalAnnotationExpr(nameToUse, new ArrayList<MemberValuePair>());
                JavaParserCommentMetadataBuilder.updateCommentsToJavaParser(annotationExpression, annotation.getCommentStructure());
                annotations.add(annotationExpression);
            }
        }
        if (annotationExpression instanceof SingleMemberAnnotationExpr) {
            // Potentially upgrade this expression to a NormalAnnotationExpr
            final SingleMemberAnnotationExpr smae = (SingleMemberAnnotationExpr) annotationExpression;
            if (memberValuePairs.size() == 1 && memberValuePairs.get(0).getName() == null || memberValuePairs.get(0).getName().equals("value") || memberValuePairs.get(0).getName().equals("")) {
                // They specified only a single member-value pair, and it is
                // the default anyway, so we need not do anything except
                // update the value
                final Expression toUse = JavaParserUtils.importExpressionIfRequired(compilationUnitServices.getEnclosingTypeName(), compilationUnitServices.getImports(), memberValuePairs.get(0).getValue());
                smae.setMemberValue(toUse);
                return;
            }
            // There is > 1 expression, or they have provided some sort of
            // non-default value, so it's time to upgrade the expression
            // (whilst retaining any potentially existing expression values)
            final Expression existingValue = smae.getMemberValue();
            annotationExpression = new NormalAnnotationExpr(smae.getName(), new ArrayList<MemberValuePair>());
            ((NormalAnnotationExpr) annotationExpression).getPairs().add(new MemberValuePair("value", existingValue));
        }
        Validate.isInstanceOf(NormalAnnotationExpr.class, annotationExpression, "Attempting to add >1 annotation member-value pair requires an existing normal annotation expression");
        final List<MemberValuePair> annotationPairs = ((NormalAnnotationExpr) annotationExpression).getPairs();
        annotationPairs.clear();
        for (final MemberValuePair pair : memberValuePairs) {
            final Expression toUse = JavaParserUtils.importExpressionIfRequired(compilationUnitServices.getEnclosingTypeName(), compilationUnitServices.getImports(), pair.getValue());
            pair.setValue(toUse);
            annotationPairs.add(pair);
        }
    }
}
Also used : SingleMemberAnnotationExpr(com.github.antlrjavaparser.api.expr.SingleMemberAnnotationExpr) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) MemberValuePair(com.github.antlrjavaparser.api.expr.MemberValuePair) AnnotationExpr(com.github.antlrjavaparser.api.expr.AnnotationExpr) MarkerAnnotationExpr(com.github.antlrjavaparser.api.expr.MarkerAnnotationExpr) SingleMemberAnnotationExpr(com.github.antlrjavaparser.api.expr.SingleMemberAnnotationExpr) NormalAnnotationExpr(com.github.antlrjavaparser.api.expr.NormalAnnotationExpr) Expression(com.github.antlrjavaparser.api.expr.Expression) NameExpr(com.github.antlrjavaparser.api.expr.NameExpr) ArrayList(java.util.ArrayList) MarkerAnnotationExpr(com.github.antlrjavaparser.api.expr.MarkerAnnotationExpr) NormalAnnotationExpr(com.github.antlrjavaparser.api.expr.NormalAnnotationExpr)

Example 3 with SingleMemberAnnotationExpr

use of com.github.antlrjavaparser.api.expr.SingleMemberAnnotationExpr in project spring-roo by spring-projects.

the class JavaParserAnnotationMetadataBuilder method convert.

@SuppressWarnings("unchecked")
private static MemberValuePair convert(final AnnotationAttributeValue<?> value, CompilationUnitServices compilationUnitServices) {
    if (value instanceof NestedAnnotationAttributeValue) {
        final NestedAnnotationAttributeValue castValue = (NestedAnnotationAttributeValue) value;
        AnnotationExpr annotationExpr;
        final AnnotationMetadata nestedAnnotation = castValue.getValue();
        if (castValue.getValue().getAttributeNames().size() == 0) {
            annotationExpr = new MarkerAnnotationExpr(JavaParserUtils.getNameExpr(nestedAnnotation.getAnnotationType().getSimpleTypeName()));
        } else if (castValue.getValue().getAttributeNames().size() == 1 && (castValue.getValue().getAttributeNames().get(0) == null || "value".equals(castValue.getValue().getAttributeNames().get(0).getSymbolName()))) {
            annotationExpr = new SingleMemberAnnotationExpr(JavaParserUtils.getNameExpr(nestedAnnotation.getAnnotationType().getSimpleTypeName()), convert(nestedAnnotation.getAttribute(nestedAnnotation.getAttributeNames().get(0)), compilationUnitServices).getValue());
        } else {
            final List<MemberValuePair> memberValuePairs = new ArrayList<MemberValuePair>();
            for (final JavaSymbolName attributeName : nestedAnnotation.getAttributeNames()) {
                memberValuePairs.add(convert(nestedAnnotation.getAttribute(attributeName), compilationUnitServices));
            }
            annotationExpr = new NormalAnnotationExpr(JavaParserUtils.getNameExpr(nestedAnnotation.getAnnotationType().getSimpleTypeName()), memberValuePairs);
        }
        // Add imports for nested annotation types
        JavaParserUtils.importTypeIfRequired(compilationUnitServices.getEnclosingTypeName(), compilationUnitServices.getImports(), nestedAnnotation.getAnnotationType());
        // Rely on the nested instance to know its member value pairs
        return new MemberValuePair(value.getName().getSymbolName(), annotationExpr);
    }
    if (value instanceof BooleanAttributeValue) {
        final boolean castValue = ((BooleanAttributeValue) value).getValue();
        final BooleanLiteralExpr convertedValue = new BooleanLiteralExpr(castValue);
        return new MemberValuePair(value.getName().getSymbolName(), convertedValue);
    }
    if (value instanceof CharAttributeValue) {
        final char castValue = ((CharAttributeValue) value).getValue();
        final CharLiteralExpr convertedValue = new CharLiteralExpr(new String(new char[] { castValue }));
        return new MemberValuePair(value.getName().getSymbolName(), convertedValue);
    }
    if (value instanceof LongAttributeValue) {
        final Long castValue = ((LongAttributeValue) value).getValue();
        final LongLiteralExpr convertedValue = new LongLiteralExpr(castValue.toString() + "L");
        return new MemberValuePair(value.getName().getSymbolName(), convertedValue);
    }
    if (value instanceof IntegerAttributeValue) {
        final Integer castValue = ((IntegerAttributeValue) value).getValue();
        final IntegerLiteralExpr convertedValue = new IntegerLiteralExpr(castValue.toString());
        return new MemberValuePair(value.getName().getSymbolName(), convertedValue);
    }
    if (value instanceof DoubleAttributeValue) {
        final DoubleAttributeValue doubleAttributeValue = (DoubleAttributeValue) value;
        final Double castValue = doubleAttributeValue.getValue();
        DoubleLiteralExpr convertedValue;
        if (doubleAttributeValue.isFloatingPrecisionOnly()) {
            convertedValue = new DoubleLiteralExpr(castValue.toString() + "F");
        } else {
            convertedValue = new DoubleLiteralExpr(castValue.toString() + "D");
        }
        return new MemberValuePair(value.getName().getSymbolName(), convertedValue);
    }
    if (value instanceof StringAttributeValue) {
        final String castValue = ((StringAttributeValue) value).getValue();
        final StringLiteralExpr convertedValue = new StringLiteralExpr(castValue.toString());
        return new MemberValuePair(value.getName().getSymbolName(), convertedValue);
    }
    if (value instanceof EnumAttributeValue) {
        final EnumDetails castValue = ((EnumAttributeValue) value).getValue();
        // This isn't as elegant as it could be (ie loss of type
        // parameters), but it will do for now
        final FieldAccessExpr convertedValue = new FieldAccessExpr(JavaParserUtils.getNameExpr(castValue.getType().getFullyQualifiedTypeName()), castValue.getField().getSymbolName());
        return new MemberValuePair(value.getName().getSymbolName(), convertedValue);
    }
    if (value instanceof ClassAttributeValue) {
        final JavaType castValue = ((ClassAttributeValue) value).getValue();
        // This doesn't preserve type parameters
        final NameExpr nameExpr = JavaParserUtils.importTypeIfRequired(compilationUnitServices.getEnclosingTypeName(), compilationUnitServices.getImports(), castValue);
        final ClassExpr convertedValue = new ClassExpr(JavaParserUtils.getReferenceType(nameExpr));
        return new MemberValuePair(value.getName().getSymbolName(), convertedValue);
    }
    if (value instanceof ArrayAttributeValue) {
        final ArrayAttributeValue<AnnotationAttributeValue<?>> castValue = (ArrayAttributeValue<AnnotationAttributeValue<?>>) value;
        final List<Expression> arrayElements = new ArrayList<Expression>();
        for (final AnnotationAttributeValue<?> v : castValue.getValue()) {
            final MemberValuePair converted = convert(v, compilationUnitServices);
            if (converted != null) {
                arrayElements.add(converted.getValue());
            }
        }
        return new MemberValuePair(value.getName().getSymbolName(), new ArrayInitializerExpr(arrayElements));
    }
    throw new UnsupportedOperationException("Unsupported attribute value '" + value.getName() + "' of type '" + value.getClass().getName() + "'");
}
Also used : IntegerLiteralExpr(com.github.antlrjavaparser.api.expr.IntegerLiteralExpr) ClassAttributeValue(org.springframework.roo.classpath.details.annotations.ClassAttributeValue) AnnotationExpr(com.github.antlrjavaparser.api.expr.AnnotationExpr) MarkerAnnotationExpr(com.github.antlrjavaparser.api.expr.MarkerAnnotationExpr) SingleMemberAnnotationExpr(com.github.antlrjavaparser.api.expr.SingleMemberAnnotationExpr) NormalAnnotationExpr(com.github.antlrjavaparser.api.expr.NormalAnnotationExpr) DoubleAttributeValue(org.springframework.roo.classpath.details.annotations.DoubleAttributeValue) StringLiteralExpr(com.github.antlrjavaparser.api.expr.StringLiteralExpr) NameExpr(com.github.antlrjavaparser.api.expr.NameExpr) ArrayList(java.util.ArrayList) CharAttributeValue(org.springframework.roo.classpath.details.annotations.CharAttributeValue) EnumAttributeValue(org.springframework.roo.classpath.details.annotations.EnumAttributeValue) EnumDetails(org.springframework.roo.model.EnumDetails) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) SingleMemberAnnotationExpr(com.github.antlrjavaparser.api.expr.SingleMemberAnnotationExpr) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) MemberValuePair(com.github.antlrjavaparser.api.expr.MemberValuePair) ArrayInitializerExpr(com.github.antlrjavaparser.api.expr.ArrayInitializerExpr) BooleanLiteralExpr(com.github.antlrjavaparser.api.expr.BooleanLiteralExpr) LongLiteralExpr(com.github.antlrjavaparser.api.expr.LongLiteralExpr) ArrayList(java.util.ArrayList) List(java.util.List) FieldAccessExpr(com.github.antlrjavaparser.api.expr.FieldAccessExpr) NormalAnnotationExpr(com.github.antlrjavaparser.api.expr.NormalAnnotationExpr) StringAttributeValue(org.springframework.roo.classpath.details.annotations.StringAttributeValue) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) ArrayAttributeValue(org.springframework.roo.classpath.details.annotations.ArrayAttributeValue) AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) IntegerAttributeValue(org.springframework.roo.classpath.details.annotations.IntegerAttributeValue) CharLiteralExpr(com.github.antlrjavaparser.api.expr.CharLiteralExpr) MarkerAnnotationExpr(com.github.antlrjavaparser.api.expr.MarkerAnnotationExpr) BooleanAttributeValue(org.springframework.roo.classpath.details.annotations.BooleanAttributeValue) JavaType(org.springframework.roo.model.JavaType) LongAttributeValue(org.springframework.roo.classpath.details.annotations.LongAttributeValue) DoubleLiteralExpr(com.github.antlrjavaparser.api.expr.DoubleLiteralExpr) Expression(com.github.antlrjavaparser.api.expr.Expression) ClassExpr(com.github.antlrjavaparser.api.expr.ClassExpr)

Aggregations

MarkerAnnotationExpr (com.github.antlrjavaparser.api.expr.MarkerAnnotationExpr)3 NameExpr (com.github.antlrjavaparser.api.expr.NameExpr)3 NormalAnnotationExpr (com.github.antlrjavaparser.api.expr.NormalAnnotationExpr)3 SingleMemberAnnotationExpr (com.github.antlrjavaparser.api.expr.SingleMemberAnnotationExpr)3 AnnotationExpr (com.github.antlrjavaparser.api.expr.AnnotationExpr)2 Expression (com.github.antlrjavaparser.api.expr.Expression)2 MemberValuePair (com.github.antlrjavaparser.api.expr.MemberValuePair)2 ArrayList (java.util.ArrayList)2 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)2 ArrayInitializerExpr (com.github.antlrjavaparser.api.expr.ArrayInitializerExpr)1 BooleanLiteralExpr (com.github.antlrjavaparser.api.expr.BooleanLiteralExpr)1 CharLiteralExpr (com.github.antlrjavaparser.api.expr.CharLiteralExpr)1 ClassExpr (com.github.antlrjavaparser.api.expr.ClassExpr)1 DoubleLiteralExpr (com.github.antlrjavaparser.api.expr.DoubleLiteralExpr)1 FieldAccessExpr (com.github.antlrjavaparser.api.expr.FieldAccessExpr)1 IntegerLiteralExpr (com.github.antlrjavaparser.api.expr.IntegerLiteralExpr)1 LongLiteralExpr (com.github.antlrjavaparser.api.expr.LongLiteralExpr)1 QualifiedNameExpr (com.github.antlrjavaparser.api.expr.QualifiedNameExpr)1 StringLiteralExpr (com.github.antlrjavaparser.api.expr.StringLiteralExpr)1 List (java.util.List)1