Search in sources :

Example 1 with NestedAnnotationAttributeValue

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

the class ExceptionsOperationsImpl method addHandlersAnnotations.

/**
 * Generates {@link RooExceptionHandlers} and {@link RooExceptionHandler} annotations
 * and adds or updates it on specified class.
 *
 * @param exception
 * @param targetClass
 * @param errorView
 */
private void addHandlersAnnotations(JavaType exception, JavaType targetClass, String errorView) {
    Validate.notNull(targetClass, "Target class is required to add @RooExceptionHandlers annotation");
    // Create @RooExceptionHandler Annotation
    final AnnotationMetadataBuilder exceptionHandlerAnnotationBuilder = new AnnotationMetadataBuilder(RooJavaType.ROO_EXCEPTION_HANDLER);
    final List<AnnotationAttributeValue<?>> exceptionHandlerAnnotationAttributes = new ArrayList<AnnotationAttributeValue<?>>();
    exceptionHandlerAnnotationAttributes.add(new ClassAttributeValue(new JavaSymbolName(EXCEPTION), exception));
    if (errorView != null) {
        exceptionHandlerAnnotationAttributes.add(new StringAttributeValue(new JavaSymbolName(ERROR_VIEW), errorView));
    }
    exceptionHandlerAnnotationBuilder.setAttributes(exceptionHandlerAnnotationAttributes);
    // Check if container annotation already exists
    ClassOrInterfaceTypeDetails typeDetails = typeLocationService.getTypeDetails(targetClass);
    ClassOrInterfaceTypeDetailsBuilder typeDetailsBuilder = new ClassOrInterfaceTypeDetailsBuilder(typeDetails);
    AnnotationMetadata exceptionHandlersAnnotation = typeDetails.getAnnotation(RooJavaType.ROO_EXCEPTION_HANDLERS);
    AnnotationMetadataBuilder exceptionHandlersAnnotationBuilder = null;
    if (exceptionHandlersAnnotation != null) {
        exceptionHandlersAnnotationBuilder = new AnnotationMetadataBuilder(exceptionHandlersAnnotation);
    } else {
        exceptionHandlersAnnotationBuilder = new AnnotationMetadataBuilder(RooJavaType.ROO_EXCEPTION_HANDLERS);
    }
    Validate.notNull(exceptionHandlersAnnotationBuilder);
    // Add @RooExceptionHandler annotation into @RooExceptionHandlers
    final List<NestedAnnotationAttributeValue> exceptionHandlersArrayValues = new ArrayList<NestedAnnotationAttributeValue>();
    exceptionHandlersArrayValues.add(new NestedAnnotationAttributeValue(new JavaSymbolName(VALUE), exceptionHandlerAnnotationBuilder.build()));
    final List<AnnotationAttributeValue<?>> attributeValues = new ArrayList<AnnotationAttributeValue<?>>();
    attributeValues.add(new ArrayAttributeValue<NestedAnnotationAttributeValue>(new JavaSymbolName(VALUE), exceptionHandlersArrayValues));
    if (exceptionHandlersAnnotation == null) {
        // Add new @RooExceptionHandlers annotation with given values
        exceptionHandlersAnnotationBuilder.setAttributes(attributeValues);
        typeDetailsBuilder.addAnnotation(exceptionHandlersAnnotationBuilder.build());
    } else {
        // Get current annotation values from @RooExceptionHandlers annotation
        AnnotationAttributeValue<?> currentHandlers = exceptionHandlersAnnotation.getAttribute(VALUE);
        if (currentHandlers != null) {
            List<?> values = (List<?>) currentHandlers.getValue();
            Iterator<?> it = values.iterator();
            while (it.hasNext()) {
                NestedAnnotationAttributeValue handler = (NestedAnnotationAttributeValue) it.next();
                if (handler.getValue() != null) {
                    // Check if there is a @RooExceptionHandlers with same 'exception' value
                    if (exceptionHandlerAnnotationBuilder.build().getAttribute(EXCEPTION).getValue().equals(handler.getValue().getAttribute(EXCEPTION).getValue())) {
                        LOGGER.warning(String.format("There is already a handler for exception %s in class %s", exception.getSimpleTypeName(), targetClass.getSimpleTypeName()));
                        return;
                    }
                    exceptionHandlersArrayValues.add(handler);
                }
            }
        }
        // Add found values
        attributeValues.add(new ArrayAttributeValue<NestedAnnotationAttributeValue>(new JavaSymbolName(VALUE), exceptionHandlersArrayValues));
        exceptionHandlersAnnotationBuilder.setAttributes(attributeValues);
        // Update annotation
        typeDetailsBuilder.updateTypeAnnotation(exceptionHandlersAnnotationBuilder.build());
    }
    // Write to disk
    getTypeManagementService().createOrUpdateTypeOnDisk(typeDetailsBuilder.build());
}
Also used : AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) ClassAttributeValue(org.springframework.roo.classpath.details.annotations.ClassAttributeValue) ArrayList(java.util.ArrayList) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ArrayList(java.util.ArrayList) List(java.util.List) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) StringAttributeValue(org.springframework.roo.classpath.details.annotations.StringAttributeValue) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)

Example 2 with NestedAnnotationAttributeValue

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

the class JavaParserAnnotationMetadataBuilder method convert.

private AnnotationAttributeValue<?> convert(JavaSymbolName annotationName, final Expression expression, final CompilationUnitServices compilationUnitServices) {
    if (annotationName == null) {
        annotationName = new JavaSymbolName("__ARRAY_ELEMENT__");
    }
    if (expression instanceof AnnotationExpr) {
        final AnnotationExpr annotationExpr = (AnnotationExpr) expression;
        final AnnotationMetadata value = getInstance(annotationExpr, compilationUnitServices).build();
        return new NestedAnnotationAttributeValue(annotationName, value);
    }
    if (expression instanceof BooleanLiteralExpr) {
        final boolean value = ((BooleanLiteralExpr) expression).getValue();
        return new BooleanAttributeValue(annotationName, value);
    }
    if (expression instanceof CharLiteralExpr) {
        final String value = ((CharLiteralExpr) expression).getValue();
        Validate.isTrue(value.length() == 1, "Expected a char expression, but instead received '%s' for attribute '%s'", value, annotationName);
        final char c = value.charAt(0);
        return new CharAttributeValue(annotationName, c);
    }
    if (expression instanceof LongLiteralExpr) {
        String value = ((LongLiteralExpr) expression).getValue();
        Validate.isTrue(value.toUpperCase().endsWith("L"), "Expected long literal expression '%s' to end in 'l' or 'L'", value);
        value = value.substring(0, value.length() - 1);
        final long l = new Long(value);
        return new LongAttributeValue(annotationName, l);
    }
    if (expression instanceof IntegerLiteralExpr) {
        final String value = ((IntegerLiteralExpr) expression).getValue();
        final int i = new Integer(value);
        return new IntegerAttributeValue(annotationName, i);
    }
    if (expression instanceof DoubleLiteralExpr) {
        String value = ((DoubleLiteralExpr) expression).getValue();
        boolean floatingPrecisionOnly = false;
        if (value.toUpperCase().endsWith("F")) {
            value = value.substring(0, value.length() - 1);
            floatingPrecisionOnly = true;
        }
        if (value.toUpperCase().endsWith("D")) {
            value = value.substring(0, value.length() - 1);
        }
        final double d = new Double(value);
        return new DoubleAttributeValue(annotationName, d, floatingPrecisionOnly);
    }
    if (expression instanceof BinaryExpr) {
        String result = "";
        BinaryExpr current = (BinaryExpr) expression;
        while (current != null) {
            String right = "";
            if (current.getRight() instanceof StringLiteralExpr) {
                right = ((StringLiteralExpr) current.getRight()).getValue();
            } else if (current.getRight() instanceof NameExpr) {
                right = ((NameExpr) current.getRight()).getName();
            }
            result = right + result;
            if (current.getLeft() instanceof StringLiteralExpr) {
                final String left = ((StringLiteralExpr) current.getLeft()).getValue();
                result = left + result;
            }
            if (current.getLeft() instanceof BinaryExpr) {
                current = (BinaryExpr) current.getLeft();
            } else {
                current = null;
            }
        }
        return new StringAttributeValue(annotationName, result);
    }
    if (expression instanceof StringLiteralExpr) {
        final String value = ((StringLiteralExpr) expression).getValue();
        return new StringAttributeValue(annotationName, value);
    }
    if (expression instanceof FieldAccessExpr) {
        final FieldAccessExpr field = (FieldAccessExpr) expression;
        final String fieldName = field.getField();
        // Determine the type
        final Expression scope = field.getScope();
        NameExpr nameToFind = null;
        if (scope instanceof FieldAccessExpr) {
            final FieldAccessExpr fScope = (FieldAccessExpr) scope;
            nameToFind = JavaParserUtils.getNameExpr(fScope.toString());
        } else if (scope instanceof NameExpr) {
            nameToFind = (NameExpr) scope;
        } else {
            throw new UnsupportedOperationException("A FieldAccessExpr for '" + field.getScope() + "' should return a NameExpr or FieldAccessExpr (was " + field.getScope().getClass().getName() + ")");
        }
        final JavaType fieldType = JavaParserUtils.getJavaType(compilationUnitServices, nameToFind, null);
        final EnumDetails enumDetails = new EnumDetails(fieldType, new JavaSymbolName(fieldName));
        return new EnumAttributeValue(annotationName, enumDetails);
    }
    if (expression instanceof NameExpr) {
        final NameExpr field = (NameExpr) expression;
        final String name = field.getName();
        // As we have no way of finding out the real type
        final JavaType fieldType = new JavaType("unknown.Object");
        final EnumDetails enumDetails = new EnumDetails(fieldType, new JavaSymbolName(name));
        return new EnumAttributeValue(annotationName, enumDetails);
    }
    if (expression instanceof ClassExpr) {
        final ClassExpr clazz = (ClassExpr) expression;
        final Type nameToFind = clazz.getType();
        final JavaType javaType = JavaParserUtils.getJavaType(compilationUnitServices, nameToFind, null);
        return new ClassAttributeValue(annotationName, javaType);
    }
    if (expression instanceof ArrayInitializerExpr) {
        final ArrayInitializerExpr castExp = (ArrayInitializerExpr) expression;
        final List<AnnotationAttributeValue<?>> arrayElements = new ArrayList<AnnotationAttributeValue<?>>();
        for (final Expression e : castExp.getValues()) {
            arrayElements.add(convert(null, e, compilationUnitServices));
        }
        return new ArrayAttributeValue<AnnotationAttributeValue<?>>(annotationName, arrayElements);
    }
    if (expression instanceof UnaryExpr) {
        final UnaryExpr castExp = (UnaryExpr) expression;
        if (castExp.getOperator() == Operator.negative) {
            String value = castExp.toString();
            value = value.toUpperCase().endsWith("L") ? value.substring(0, value.length() - 1) : value;
            final long l = new Long(value);
            return new LongAttributeValue(annotationName, l);
        } else {
            throw new UnsupportedOperationException("Only negative operator in UnaryExpr is supported");
        }
    }
    throw new UnsupportedOperationException("Unable to parse annotation attribute '" + annotationName + "' due to unsupported annotation expression '" + expression.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) EnumDetails(org.springframework.roo.model.EnumDetails) EnumAttributeValue(org.springframework.roo.classpath.details.annotations.EnumAttributeValue) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ArrayInitializerExpr(com.github.antlrjavaparser.api.expr.ArrayInitializerExpr) BooleanLiteralExpr(com.github.antlrjavaparser.api.expr.BooleanLiteralExpr) LongLiteralExpr(com.github.antlrjavaparser.api.expr.LongLiteralExpr) FieldAccessExpr(com.github.antlrjavaparser.api.expr.FieldAccessExpr) 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) BinaryExpr(com.github.antlrjavaparser.api.expr.BinaryExpr) IntegerAttributeValue(org.springframework.roo.classpath.details.annotations.IntegerAttributeValue) CharLiteralExpr(com.github.antlrjavaparser.api.expr.CharLiteralExpr) UnaryExpr(com.github.antlrjavaparser.api.expr.UnaryExpr) BooleanAttributeValue(org.springframework.roo.classpath.details.annotations.BooleanAttributeValue) JavaType(org.springframework.roo.model.JavaType) JavaType(org.springframework.roo.model.JavaType) Type(com.github.antlrjavaparser.api.type.Type) 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 3 with NestedAnnotationAttributeValue

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

the class SeiMetadata method getSEIMethodFromServiceMethod.

/**
 * This method obtains a SEI method from a provided service method.
 *
 * This method caches the generated methods
 *
 * @param serviceMethod defined in a service interface
 *
 * @return MethodMetadataBuilder that contains all the information about the new SEI method.
 */
private MethodMetadata getSEIMethodFromServiceMethod(MethodMetadata serviceMethod) {
    // Check if already exists the method
    if (seiMethodsFromServiceMethods.get(serviceMethod) != null) {
        return seiMethodsFromServiceMethods.get(serviceMethod);
    }
    // If not exists, generate it and cache it.
    // Obtain the necessary elements from service method
    JavaSymbolName methodName = serviceMethod.getMethodName();
    JavaType returnType = serviceMethod.getReturnType();
    List<AnnotatedJavaType> parameterTypes = serviceMethod.getParameterTypes();
    List<JavaSymbolName> parameterNames = serviceMethod.getParameterNames();
    // Obtain parameterList
    // Is necessary to change the method name to prevent errors
    String paramList = "";
    for (AnnotatedJavaType param : parameterTypes) {
        paramList = paramList.concat(StringUtils.capitalize(param.getJavaType().getSimpleTypeName())).concat("And");
    }
    if (StringUtils.isNotBlank(paramList)) {
        // Before to update, check if is a finder
        if (methodName.toString().startsWith("findBy")) {
            methodName = new JavaSymbolName("find");
        } else if (methodName.toString().startsWith("countBy")) {
            methodName = new JavaSymbolName("count");
        }
        paramList = paramList.substring(0, paramList.length() - "And".length());
        methodName = new JavaSymbolName(methodName.toString().concat("By").concat(paramList));
    }
    // Annotate parameter types with @WebParam and @XmlJavaTypeAdapter if needed
    List<AnnotatedJavaType> annotatedParameterTypes = new ArrayList<AnnotatedJavaType>();
    for (int i = 0; i < parameterTypes.size(); i++) {
        List<AnnotationMetadata> annotations = new ArrayList<AnnotationMetadata>();
        // Getting parameter type and parameter name
        AnnotatedJavaType paramType = parameterTypes.get(i);
        JavaSymbolName paramName = parameterNames.get(i);
        // Creating @WebParam annotation
        AnnotationMetadataBuilder webParamAnnotation = new AnnotationMetadataBuilder(JavaType.WEB_PARAM);
        webParamAnnotation.addStringAttribute("name", paramName.toString());
        webParamAnnotation.addStringAttribute("targetNamespace", "");
        annotations.add(webParamAnnotation.build());
        // Creating @XmlJavaTypeAdapter annotation
        AnnotationMetadataBuilder javaTypeAdapter = new AnnotationMetadataBuilder(JavaType.XML_JAVATYPE_ADAPTER);
        if (paramType.getJavaType().getFullyQualifiedTypeName().equals(JavaType.ITERABLE.getFullyQualifiedTypeName())) {
            javaTypeAdapter.addClassAttribute("value", SpringletsJavaType.SPRINGLETS_ITERABLE_ADAPTER);
            annotations.add(javaTypeAdapter.build());
        } else if (paramType.getJavaType().getFullyQualifiedTypeName().equals(SpringJavaType.PAGE.getFullyQualifiedTypeName())) {
            javaTypeAdapter.addClassAttribute("value", SpringletsJavaType.SPRINGLETS_PAGE_ADAPTER);
            annotations.add(javaTypeAdapter.build());
        } else if (paramType.getJavaType().equals(SpringletsJavaType.SPRINGLETS_GLOBAL_SEARCH)) {
            javaTypeAdapter.addClassAttribute("value", SpringletsJavaType.SPRINGLETS_GLOBAL_SEARCH_ADAPTER);
            annotations.add(javaTypeAdapter.build());
        } else if (paramType.getJavaType().equals(SpringJavaType.PAGEABLE)) {
            javaTypeAdapter.addClassAttribute("value", SpringletsJavaType.SPRINGLETS_PAGEABLE_ADAPTER);
            annotations.add(javaTypeAdapter.build());
        }
        // Creating new parameter type annotated with @WebParam
        AnnotatedJavaType annotatedParam = new AnnotatedJavaType(paramType.getJavaType(), annotations);
        annotatedParameterTypes.add(annotatedParam);
    }
    MethodMetadataBuilder seiMethod = new MethodMetadataBuilder(getId(), Modifier.PUBLIC + Modifier.ABSTRACT, methodName, returnType, annotatedParameterTypes, parameterNames, null);
    // Include @XmlJavaTypeAdapter annotation if needed
    AnnotationMetadataBuilder javaTypeAdapter = new AnnotationMetadataBuilder(JavaType.XML_JAVATYPE_ADAPTER);
    if (returnType.getFullyQualifiedTypeName().equals(JavaType.ITERABLE.getFullyQualifiedTypeName())) {
        javaTypeAdapter.addClassAttribute("value", SpringletsJavaType.SPRINGLETS_ITERABLE_ADAPTER);
        seiMethod.addAnnotation(javaTypeAdapter);
    } else if (returnType.getFullyQualifiedTypeName().equals(SpringJavaType.PAGE.getFullyQualifiedTypeName())) {
        javaTypeAdapter.addClassAttribute("value", SpringletsJavaType.SPRINGLETS_PAGE_ADAPTER);
        seiMethod.addAnnotation(javaTypeAdapter);
    } else if (returnType.equals(SpringletsJavaType.SPRINGLETS_GLOBAL_SEARCH)) {
        javaTypeAdapter.addClassAttribute("value", SpringletsJavaType.SPRINGLETS_GLOBAL_SEARCH_ADAPTER);
        seiMethod.addAnnotation(javaTypeAdapter);
    } else if (returnType.equals(SpringJavaType.PAGEABLE)) {
        javaTypeAdapter.addClassAttribute("value", SpringletsJavaType.SPRINGLETS_PAGEABLE_ADAPTER);
        seiMethod.addAnnotation(javaTypeAdapter);
    }
    // Include @RequestWrapper annotation
    AnnotationMetadataBuilder requestWrapperAnnotation = new AnnotationMetadataBuilder(JavaType.REQUEST_WRAPPER);
    requestWrapperAnnotation.addStringAttribute("className", String.format("%s.%sRequest", sei.getType().getPackage(), seiMethod.getMethodName().getSymbolNameCapitalisedFirstLetter()));
    requestWrapperAnnotation.addStringAttribute("localName", String.format("%sRequest", seiMethod.getMethodName().getSymbolNameCapitalisedFirstLetter()));
    requestWrapperAnnotation.addStringAttribute("targetNamespace", String.format("http://ws.%s/", StringUtils.reverseDelimited(projectTopLevelPackage.getFullyQualifiedPackageName(), '.')));
    seiMethod.addAnnotation(requestWrapperAnnotation);
    // Include @ResponseWrapper annotation
    AnnotationMetadataBuilder responseWrapperAnnotation = new AnnotationMetadataBuilder(JavaType.RESPONSE_WRAPPER);
    responseWrapperAnnotation.addStringAttribute("className", String.format("%s.%sResponse", sei.getType().getPackage(), seiMethod.getMethodName().getSymbolNameCapitalisedFirstLetter()));
    responseWrapperAnnotation.addStringAttribute("localName", String.format("%sResponse", seiMethod.getMethodName().getSymbolNameCapitalisedFirstLetter()));
    responseWrapperAnnotation.addStringAttribute("targetNamespace", String.format("http://ws.%s/", StringUtils.reverseDelimited(projectTopLevelPackage.getFullyQualifiedPackageName(), '.')));
    seiMethod.addAnnotation(responseWrapperAnnotation);
    // Include @WebMethod annotation
    AnnotationMetadataBuilder webMethodAnnotation = new AnnotationMetadataBuilder(JavaType.WEB_METHOD);
    webMethodAnnotation.addStringAttribute("action", String.format("urn:%s", seiMethod.getMethodName().getSymbolNameCapitalisedFirstLetter()));
    seiMethod.addAnnotation(webMethodAnnotation);
    // Include @WebResult annotation
    AnnotationMetadataBuilder webResultAnnotation = new AnnotationMetadataBuilder(JavaType.WEB_RESULT);
    webResultAnnotation.addStringAttribute("name", returnType.getBaseType().getSimpleTypeName().toLowerCase());
    webResultAnnotation.addStringAttribute("targetNamespace", "");
    seiMethod.addAnnotation(webResultAnnotation);
    // Include @WSDLDocumentationCollection annotation
    AnnotationMetadataBuilder wsdlDocumentationCollectionAnnotation = new AnnotationMetadataBuilder(new JavaType("org.apache.cxf.annotations.WSDLDocumentationCollection"));
    // Create @WSDLDocumentation annotation
    List<AnnotationAttributeValue<?>> documentations = new ArrayList<AnnotationAttributeValue<?>>();
    AnnotationMetadataBuilder documentationAnnotation1 = new AnnotationMetadataBuilder(wsdlDocumentationType);
    documentationAnnotation1.addStringAttribute("value", String.format("TODO Auto-generated documentation for %s", sei.getType().getSimpleTypeName()));
    documentationAnnotation1.addEnumAttribute("placement", wsdlDocumentationType, new JavaSymbolName("Placement.DEFAULT"));
    NestedAnnotationAttributeValue newDocumentation1 = new NestedAnnotationAttributeValue(new JavaSymbolName("value"), documentationAnnotation1.build());
    documentations.add(newDocumentation1);
    AnnotationMetadataBuilder documentationAnnotation2 = new AnnotationMetadataBuilder(wsdlDocumentationType);
    documentationAnnotation2.addStringAttribute("value", String.format("TODO Auto-generated documentation for %s", sei.getType().getSimpleTypeName()));
    documentationAnnotation2.addEnumAttribute("placement", wsdlDocumentationType, new JavaSymbolName("Placement.PORT_TYPE_OPERATION_OUTPUT"));
    NestedAnnotationAttributeValue newDocumentation2 = new NestedAnnotationAttributeValue(new JavaSymbolName("value"), documentationAnnotation2.build());
    documentations.add(newDocumentation2);
    ArrayAttributeValue<AnnotationAttributeValue<?>> newDocumentations = new ArrayAttributeValue<AnnotationAttributeValue<?>>(new JavaSymbolName("value"), documentations);
    wsdlDocumentationCollectionAnnotation.addAttribute(newDocumentations);
    seiMethod.addAnnotation(wsdlDocumentationCollectionAnnotation);
    seiMethodsFromServiceMethods.put(serviceMethod, seiMethod.build());
    seiMethods.put(seiMethod.build(), serviceMethod);
    return seiMethod.build();
}
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) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) SpringJavaType(org.springframework.roo.model.SpringJavaType) SpringletsJavaType(org.springframework.roo.model.SpringletsJavaType) JavaType(org.springframework.roo.model.JavaType) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)

Example 4 with NestedAnnotationAttributeValue

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

the class DbreMetadata method getManyToManyOwningSideField.

private FieldMetadataBuilder getManyToManyOwningSideField(final JavaSymbolName fieldName, final Table joinTable, final Table inverseSideTable, final CascadeAction onUpdate, final CascadeAction onDelete) {
    final JavaType element = DbreTypeUtils.findTypeForTable(managedEntities, inverseSideTable);
    Validate.notNull(element, "Attempted to create many-to-many owning-side field '%s' in '%s' %s", fieldName, destination.getFullyQualifiedTypeName(), getErrorMsg(inverseSideTable.getFullyQualifiedTableName()));
    final List<JavaType> params = Arrays.asList(element);
    final JavaType fieldType = new JavaType(SET.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, params);
    // Add annotations to field
    final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
    // Add @ManyToMany annotation
    final AnnotationMetadataBuilder manyToManyBuilder = new AnnotationMetadataBuilder(MANY_TO_MANY);
    annotations.add(manyToManyBuilder);
    // Add @JoinTable annotation
    final AnnotationMetadataBuilder joinTableBuilder = new AnnotationMetadataBuilder(JOIN_TABLE);
    final List<AnnotationAttributeValue<?>> joinTableAnnotationAttributes = new ArrayList<AnnotationAttributeValue<?>>();
    joinTableAnnotationAttributes.add(new StringAttributeValue(new JavaSymbolName(NAME), joinTable.getName()));
    final Iterator<ForeignKey> iter = joinTable.getImportedKeys().iterator();
    // Add "joinColumns" attribute containing nested @JoinColumn annotations
    final List<NestedAnnotationAttributeValue> joinColumnArrayValues = new ArrayList<NestedAnnotationAttributeValue>();
    final Set<Reference> firstKeyReferences = iter.next().getReferences();
    for (final Reference reference : firstKeyReferences) {
        final AnnotationMetadataBuilder joinColumnBuilder = getJoinColumnAnnotation(reference, firstKeyReferences.size() > 1);
        joinColumnArrayValues.add(new NestedAnnotationAttributeValue(new JavaSymbolName(VALUE), joinColumnBuilder.build()));
    }
    joinTableAnnotationAttributes.add(new ArrayAttributeValue<NestedAnnotationAttributeValue>(new JavaSymbolName("joinColumns"), joinColumnArrayValues));
    // Add "inverseJoinColumns" attribute containing nested @JoinColumn
    // annotations
    final List<NestedAnnotationAttributeValue> inverseJoinColumnArrayValues = new ArrayList<NestedAnnotationAttributeValue>();
    final Set<Reference> lastKeyReferences = iter.next().getReferences();
    for (final Reference reference : lastKeyReferences) {
        final AnnotationMetadataBuilder joinColumnBuilder = getJoinColumnAnnotation(reference, lastKeyReferences.size() > 1);
        inverseJoinColumnArrayValues.add(new NestedAnnotationAttributeValue(new JavaSymbolName(VALUE), joinColumnBuilder.build()));
    }
    joinTableAnnotationAttributes.add(new ArrayAttributeValue<NestedAnnotationAttributeValue>(new JavaSymbolName("inverseJoinColumns"), inverseJoinColumnArrayValues));
    // Add attributes to a @JoinTable annotation builder
    joinTableBuilder.setAttributes(joinTableAnnotationAttributes);
    annotations.add(joinTableBuilder);
    return new FieldMetadataBuilder(getId(), Modifier.PRIVATE, annotations, fieldName, fieldType);
}
Also used : AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) Reference(org.springframework.roo.addon.dbre.addon.model.Reference) ArrayList(java.util.ArrayList) ForeignKey(org.springframework.roo.addon.dbre.addon.model.ForeignKey) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder) JdkJavaType(org.springframework.roo.model.JdkJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) StringAttributeValue(org.springframework.roo.classpath.details.annotations.StringAttributeValue) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)

Example 5 with NestedAnnotationAttributeValue

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

the class AnnotationMetadataUtils method computeAttributeValue.

private static String computeAttributeValue(final AnnotationAttributeValue<?> value, final ImportRegistrationResolver resolver) {
    String attributeValue = null;
    if (value instanceof BooleanAttributeValue) {
        attributeValue = ((BooleanAttributeValue) value).getValue().toString();
    } else if (value instanceof CharAttributeValue) {
        attributeValue = "'" + ((CharAttributeValue) value).getValue().toString() + "'";
    } else if (value instanceof ClassAttributeValue) {
        final JavaType clazz = ((ClassAttributeValue) value).getValue();
        if (resolver == null || resolver.isFullyQualifiedFormRequiredAfterAutoImport(clazz)) {
            attributeValue = clazz.getFullyQualifiedTypeName() + ".class";
        } else {
            attributeValue = clazz.getSimpleTypeName() + ".class";
        }
    } else if (value instanceof DoubleAttributeValue) {
        final DoubleAttributeValue dbl = (DoubleAttributeValue) value;
        if (dbl.isFloatingPrecisionOnly()) {
            attributeValue = dbl.getValue().toString() + "F";
        } else {
            attributeValue = dbl.getValue().toString() + "D";
        }
    } else if (value instanceof EnumAttributeValue) {
        final EnumDetails enumDetails = ((EnumAttributeValue) value).getValue();
        final JavaType clazz = enumDetails.getType();
        if (resolver == null || resolver.isFullyQualifiedFormRequiredAfterAutoImport(clazz)) {
            attributeValue = clazz.getFullyQualifiedTypeName() + "." + enumDetails.getField().getSymbolName();
        } else {
            attributeValue = clazz.getSimpleTypeName() + "." + enumDetails.getField().getSymbolName();
        }
    } else if (value instanceof IntegerAttributeValue) {
        attributeValue = ((IntegerAttributeValue) value).getValue().toString();
    } else if (value instanceof LongAttributeValue) {
        attributeValue = ((LongAttributeValue) value).getValue().toString() + "L";
    } else if (value instanceof StringAttributeValue) {
        attributeValue = "\"" + ((StringAttributeValue) value).getValue() + "\"";
    } else if (value instanceof NestedAnnotationAttributeValue) {
        final AnnotationMetadata annotationMetadata = ((NestedAnnotationAttributeValue) value).getValue();
        final StringBuilder data = new StringBuilder("@");
        final JavaType annotationType = annotationMetadata.getAnnotationType();
        if (resolver == null || resolver.isFullyQualifiedFormRequiredAfterAutoImport(annotationType)) {
            data.append(annotationType.getFullyQualifiedTypeName());
        } else {
            data.append(annotationType.getSimpleTypeName());
        }
        if (!annotationMetadata.getAttributeNames().isEmpty()) {
            data.append("(");
            int i = 0;
            for (final JavaSymbolName attributeName : annotationMetadata.getAttributeNames()) {
                i++;
                if (i > 1) {
                    data.append(", ");
                }
                data.append(attributeName.getSymbolName()).append(" = ");
                data.append(computeAttributeValue(annotationMetadata.getAttribute(attributeName), resolver));
            }
            data.append(")");
        }
        attributeValue = data.toString();
    } else if (value instanceof ArrayAttributeValue<?>) {
        final ArrayAttributeValue<?> array = (ArrayAttributeValue<?>) value;
        final StringBuilder data = new StringBuilder("{ ");
        int i = 0;
        for (final AnnotationAttributeValue<?> val : array.getValue()) {
            i++;
            if (i > 1) {
                data.append(", ");
            }
            data.append(computeAttributeValue(val, resolver));
        }
        data.append(" }");
        attributeValue = data.toString();
    }
    return attributeValue;
}
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) ClassAttributeValue(org.springframework.roo.classpath.details.annotations.ClassAttributeValue) DoubleAttributeValue(org.springframework.roo.classpath.details.annotations.DoubleAttributeValue) IntegerAttributeValue(org.springframework.roo.classpath.details.annotations.IntegerAttributeValue) 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) BooleanAttributeValue(org.springframework.roo.classpath.details.annotations.BooleanAttributeValue) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) LongAttributeValue(org.springframework.roo.classpath.details.annotations.LongAttributeValue) StringAttributeValue(org.springframework.roo.classpath.details.annotations.StringAttributeValue) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)

Aggregations

NestedAnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)19 ArrayList (java.util.ArrayList)16 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)15 List (java.util.List)14 AnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue)14 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)14 JavaType (org.springframework.roo.model.JavaType)10 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)9 ArrayAttributeValue (org.springframework.roo.classpath.details.annotations.ArrayAttributeValue)9 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)8 ClassAttributeValue (org.springframework.roo.classpath.details.annotations.ClassAttributeValue)8 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)5 StringAttributeValue (org.springframework.roo.classpath.details.annotations.StringAttributeValue)5 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)5 EnumDetails (org.springframework.roo.model.EnumDetails)5 RooJavaType (org.springframework.roo.model.RooJavaType)5 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)4 LinkedHashMap (java.util.LinkedHashMap)3 AnnotationExpr (com.github.antlrjavaparser.api.expr.AnnotationExpr)2 ArrayInitializerExpr (com.github.antlrjavaparser.api.expr.ArrayInitializerExpr)2