Search in sources :

Example 21 with StringAttributeValue

use of org.springframework.roo.classpath.details.annotations.StringAttributeValue in project spring-roo by spring-projects.

the class SecurityOperationsImpl method getRooSecurityFilterAnnotation.

/**
 * This method provides {@link RooSecurityFilter} annotation with all the necessary
 * attributes
 *
 * @param method Method to add the annotation
 * @param lstParamTypes Parameter types of the method to add the annotation
 * @param roles Roles to apply by the filter
 * @param usernames Usernames apply by the filter
 * @param when Indicate the type of filter 'PRE' (@PreFilter) or 'POST' (@PostFilter)
 * @return the annotation created
 */
private AnnotationMetadataBuilder getRooSecurityFilterAnnotation(final String method, final List<AnnotationAttributeValue<?>> lstParamTypes, final String roles, final String usernames, final String when) {
    final List<AnnotationAttributeValue<?>> attributes = new ArrayList<AnnotationAttributeValue<?>>();
    attributes.add(new StringAttributeValue(new JavaSymbolName("method"), method));
    ArrayAttributeValue<AnnotationAttributeValue<?>> newParameters = new ArrayAttributeValue<AnnotationAttributeValue<?>>(new JavaSymbolName("parameters"), lstParamTypes);
    attributes.add(newParameters);
    if (roles != null) {
        attributes.add(new StringAttributeValue(new JavaSymbolName("roles"), roles));
    }
    if (usernames != null) {
        attributes.add(new StringAttributeValue(new JavaSymbolName("usernames"), usernames));
    }
    attributes.add(new StringAttributeValue(new JavaSymbolName("when"), when));
    return new AnnotationMetadataBuilder(RooJavaType.ROO_SECURITY_FILTER, attributes);
}
Also used : ArrayAttributeValue(org.springframework.roo.classpath.details.annotations.ArrayAttributeValue) AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ArrayList(java.util.ArrayList) StringAttributeValue(org.springframework.roo.classpath.details.annotations.StringAttributeValue) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 22 with StringAttributeValue

use of org.springframework.roo.classpath.details.annotations.StringAttributeValue 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)

Example 23 with StringAttributeValue

use of org.springframework.roo.classpath.details.annotations.StringAttributeValue in project spring-roo by spring-projects.

the class WsOperationsImpl method getWsClientAnnotation.

/**
 * This method provides @RooWsClient annotation with all the necessary attributes
 *
 * @param endpoint
 * @param targetNamespace
 * @param bindingType
 * @return
 */
private AnnotationMetadataBuilder getWsClientAnnotation(final String endpoint, final String targetNamespace, final SoapBindingType bindingType) {
    final List<AnnotationAttributeValue<?>> wsClientAttributes = new ArrayList<AnnotationAttributeValue<?>>();
    wsClientAttributes.add(new StringAttributeValue(new JavaSymbolName("endpoint"), endpoint));
    wsClientAttributes.add(new StringAttributeValue(new JavaSymbolName("targetNamespace"), targetNamespace));
    wsClientAttributes.add(new EnumAttributeValue(new JavaSymbolName("binding"), new EnumDetails(RooJavaType.ROO_ENUM_SOAP_BINDING_TYPE, new JavaSymbolName(bindingType.name()))));
    return new AnnotationMetadataBuilder(RooJavaType.ROO_WS_CLIENT, wsClientAttributes);
}
Also used : AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ArrayList(java.util.ArrayList) EnumAttributeValue(org.springframework.roo.classpath.details.annotations.EnumAttributeValue) EnumDetails(org.springframework.roo.model.EnumDetails) StringAttributeValue(org.springframework.roo.classpath.details.annotations.StringAttributeValue) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 24 with StringAttributeValue

use of org.springframework.roo.classpath.details.annotations.StringAttributeValue in project spring-roo by spring-projects.

the class DbreMetadata method excludeFieldsInToStringAnnotation.

// XXX DiSiD: Invoke this method when add non other fields to builder
// http://projects.disid.com/issues/7455
private void excludeFieldsInToStringAnnotation(final String fieldName) {
    if (toStringAnnotation == null) {
        return;
    }
    final List<AnnotationAttributeValue<?>> attributes = new ArrayList<AnnotationAttributeValue<?>>();
    final List<StringAttributeValue> ignoreFields = new ArrayList<StringAttributeValue>();
    // Copy the existing attributes, excluding the "ignoreFields" attribute
    boolean alreadyAdded = false;
    AnnotationAttributeValue<?> value = toStringAnnotation.getAttribute(new JavaSymbolName("excludeFields"));
    if (value == null) {
        value = new ArrayAttributeValue<StringAttributeValue>(new JavaSymbolName("excludeFields"), new ArrayList<StringAttributeValue>());
    }
    // Ensure we have an array of strings
    final String errMsg = "@RooToString attribute 'excludeFields' must be an array of strings";
    Validate.isInstanceOf(ArrayAttributeValue.class, value, errMsg);
    final ArrayAttributeValue<?> arrayVal = (ArrayAttributeValue<?>) value;
    for (final Object obj : arrayVal.getValue()) {
        Validate.isInstanceOf(StringAttributeValue.class, obj, errMsg);
        final StringAttributeValue sv = (StringAttributeValue) obj;
        if (sv.getValue().equals(fieldName)) {
            alreadyAdded = true;
        }
        ignoreFields.add(sv);
    }
    // Add the desired field to ignore to the end
    if (!alreadyAdded) {
        ignoreFields.add(new StringAttributeValue(new JavaSymbolName("ignored"), fieldName));
    }
    attributes.add(new ArrayAttributeValue<StringAttributeValue>(new JavaSymbolName("excludeFields"), ignoreFields));
    final AnnotationMetadataBuilder toStringAnnotationBuilder = new AnnotationMetadataBuilder(ROO_TO_STRING, attributes);
    updatedGovernorBuilder = new ClassOrInterfaceTypeDetailsBuilder(governorTypeDetails);
    toStringAnnotation = toStringAnnotationBuilder.build();
    updatedGovernorBuilder.updateTypeAnnotation(toStringAnnotation, new HashSet<JavaSymbolName>());
}
Also used : ArrayAttributeValue(org.springframework.roo.classpath.details.annotations.ArrayAttributeValue) AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) ArrayList(java.util.ArrayList) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) StringAttributeValue(org.springframework.roo.classpath.details.annotations.StringAttributeValue) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 25 with StringAttributeValue

use of org.springframework.roo.classpath.details.annotations.StringAttributeValue in project spring-roo by spring-projects.

the class JspOperationsImpl method getHttpPostMethod.

private MethodMetadataBuilder getHttpPostMethod(final String declaredByMetadataId) {
    final List<AnnotationMetadataBuilder> postMethodAnnotations = new ArrayList<AnnotationMetadataBuilder>();
    final List<AnnotationAttributeValue<?>> postMethodAttributes = new ArrayList<AnnotationAttributeValue<?>>();
    postMethodAttributes.add(new EnumAttributeValue(new JavaSymbolName("method"), new EnumDetails(REQUEST_METHOD, new JavaSymbolName("POST"))));
    postMethodAttributes.add(new StringAttributeValue(new JavaSymbolName("value"), "{id}"));
    postMethodAnnotations.add(new AnnotationMetadataBuilder(REQUEST_MAPPING, postMethodAttributes));
    final List<AnnotatedJavaType> postParamTypes = new ArrayList<AnnotatedJavaType>();
    final AnnotationMetadataBuilder idParamAnnotation = new AnnotationMetadataBuilder(PATH_VARIABLE);
    postParamTypes.add(new AnnotatedJavaType(new JavaType("java.lang.Long"), idParamAnnotation.build()));
    postParamTypes.add(new AnnotatedJavaType(MODEL_MAP));
    postParamTypes.add(new AnnotatedJavaType(HTTP_SERVLET_REQUEST));
    postParamTypes.add(new AnnotatedJavaType(HTTP_SERVLET_RESPONSE));
    final List<JavaSymbolName> postParamNames = new ArrayList<JavaSymbolName>();
    postParamNames.add(new JavaSymbolName("id"));
    postParamNames.add(new JavaSymbolName("modelMap"));
    postParamNames.add(new JavaSymbolName("request"));
    postParamNames.add(new JavaSymbolName("response"));
    final MethodMetadataBuilder postMethodBuilder = new MethodMetadataBuilder(declaredByMetadataId, Modifier.PUBLIC, new JavaSymbolName("post"), JavaType.VOID_PRIMITIVE, postParamTypes, postParamNames, new InvocableMemberBodyBuilder());
    postMethodBuilder.setAnnotations(postMethodAnnotations);
    return postMethodBuilder;
}
Also used : AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) EnumAttributeValue(org.springframework.roo.classpath.details.annotations.EnumAttributeValue) EnumDetails(org.springframework.roo.model.EnumDetails) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) JavaType(org.springframework.roo.model.JavaType) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) StringAttributeValue(org.springframework.roo.classpath.details.annotations.StringAttributeValue) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Aggregations

StringAttributeValue (org.springframework.roo.classpath.details.annotations.StringAttributeValue)29 ArrayList (java.util.ArrayList)28 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)27 AnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue)23 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)23 NestedAnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)12 ArrayAttributeValue (org.springframework.roo.classpath.details.annotations.ArrayAttributeValue)10 EnumAttributeValue (org.springframework.roo.classpath.details.annotations.EnumAttributeValue)10 EnumDetails (org.springframework.roo.model.EnumDetails)9 List (java.util.List)8 JavaType (org.springframework.roo.model.JavaType)8 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)7 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)7 BooleanAttributeValue (org.springframework.roo.classpath.details.annotations.BooleanAttributeValue)7 ClassAttributeValue (org.springframework.roo.classpath.details.annotations.ClassAttributeValue)6 IntegerAttributeValue (org.springframework.roo.classpath.details.annotations.IntegerAttributeValue)5 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)4 CharAttributeValue (org.springframework.roo.classpath.details.annotations.CharAttributeValue)4 DoubleAttributeValue (org.springframework.roo.classpath.details.annotations.DoubleAttributeValue)4 LongAttributeValue (org.springframework.roo.classpath.details.annotations.LongAttributeValue)4