Search in sources :

Example 41 with AnnotationMetadata

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

Example 42 with AnnotationMetadata

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

the class ItdSourceFileComposer method writeInnerTypeConstructors.

private void writeInnerTypeConstructors(final JavaType innerType, final List<? extends ConstructorMetadata> constructors, final boolean defineTarget, final boolean isInterfaceMethod) {
    for (final ConstructorMetadata constructor : constructors) {
        Validate.isTrue(constructor.getParameterTypes().size() == constructor.getParameterNames().size(), "One constructor has mismatched parameter names against parameter types");
        // Append annotations
        for (final AnnotationMetadata annotation : constructor.getAnnotations()) {
            appendIndent();
            outputAnnotation(annotation);
            this.newLine(false);
        }
        // Append "<modifier> <methodName>" portion
        appendIndent();
        if (constructor.getModifier() != 0) {
            append(Modifier.toString(constructor.getModifier()));
            append(" ");
        }
        append(innerType.getSimpleTypeName());
        // Append parameter types and names
        append("(");
        final List<AnnotatedJavaType> parameterTypes = constructor.getParameterTypes();
        final List<JavaSymbolName> parameterNames = constructor.getParameterNames();
        for (int i = 0; i < parameterTypes.size(); i++) {
            final AnnotatedJavaType paramType = parameterTypes.get(i);
            final JavaSymbolName paramName = parameterNames.get(i);
            for (final AnnotationMetadata methodParameterAnnotation : paramType.getAnnotations()) {
                outputAnnotation(methodParameterAnnotation);
                append(" ");
            }
            append(paramType.getJavaType().getNameIncludingTypeParameters(false, resolver));
            append(" ");
            append(paramName.getSymbolName());
            if (i < parameterTypes.size() - 1) {
                append(", ");
            }
        }
        // Add exceptions to be thrown
        final List<JavaType> throwsTypes = constructor.getThrowsTypes();
        if (throwsTypes.size() > 0) {
            append(") throws ");
            for (int i = 0; i < throwsTypes.size(); i++) {
                append(throwsTypes.get(i).getNameIncludingTypeParameters(false, resolver));
                if (throwsTypes.size() > i + 1) {
                    append(", ");
                }
            }
        } else {
            append(")");
        }
        if (isInterfaceMethod) {
            append(";");
        } else {
            append(" {");
            this.newLine(false);
            // Add body
            indent();
            append(constructor.getBody());
            indentRemove();
            appendFormalLine("}");
        }
        this.newLine();
    }
}
Also used : ConstructorMetadata(org.springframework.roo.classpath.details.ConstructorMetadata) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) JavaType(org.springframework.roo.model.JavaType) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Example 43 with AnnotationMetadata

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

the class ItdSourceFileComposer method appendTypeAnnotations.

private void appendTypeAnnotations() {
    final List<? extends AnnotationMetadata> typeAnnotations = itdTypeDetails.getAnnotations();
    if (typeAnnotations == null || typeAnnotations.isEmpty()) {
        return;
    }
    content = true;
    for (final AnnotationMetadata typeAnnotation : typeAnnotations) {
        appendIndent();
        append("declare @type: ");
        append(introductionTo.getSimpleTypeName());
        append(": ");
        outputAnnotation(typeAnnotation);
        append(";");
        this.newLine(false);
        this.newLine();
    }
}
Also used : AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Example 44 with AnnotationMetadata

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

the class ItdSourceFileComposer method writeMethods.

private void writeMethods(final List<? extends MethodMetadata> methods, final boolean defineTarget, final boolean isInterfaceMethod) {
    for (final MethodMetadata method : methods) {
        Validate.isTrue(method.getParameterTypes().size() == method.getParameterNames().size(), "Method %s has mismatched parameter names against parameter types", method.getMethodName().getSymbolName());
        // ROO-3447: Append comments if exists
        CommentStructure commentStructure = method.getCommentStructure();
        if (commentStructure != null && commentStructure.getBeginComments() != null) {
            List<AbstractComment> comments = commentStructure.getBeginComments();
            String commentString = "";
            boolean missingComponentsAdded = false;
            for (AbstractComment comment : comments) {
                // Join all JavadocComment's
                if (comment instanceof JavadocComment) {
                    // Add JavaDoc missing components
                    if (!missingComponentsAdded) {
                        // Check params info
                        if (!method.getParameterNames().isEmpty() && ((JavadocComment) comment).getParamsInfo() == null) {
                            List<String> paramsInfo = new ArrayList<String>();
                            for (JavaSymbolName name : method.getParameterNames()) {
                                paramsInfo.add(name.getSymbolName());
                            }
                            ((JavadocComment) comment).setParamsInfo(paramsInfo);
                        }
                        // Check return info
                        if (!method.getReturnType().equals(JavaType.VOID_OBJECT) && !method.getReturnType().equals(JavaType.VOID_PRIMITIVE) && ((JavadocComment) comment).getReturnInfo() == null) {
                            ((JavadocComment) comment).setReturnInfo(method.getReturnType().getSimpleTypeName());
                        }
                        // Check throws info
                        if (!method.getThrowsTypes().isEmpty() && ((JavadocComment) comment).getThrowsInfo() == null) {
                            List<String> throwsInfo = new ArrayList<String>();
                            for (JavaType throwsType : method.getThrowsTypes()) {
                                throwsInfo.add(throwsType.getSimpleTypeName());
                            }
                            ((JavadocComment) comment).setThrowsInfo(throwsInfo);
                        }
                        missingComponentsAdded = true;
                    }
                    commentString = commentString.concat(comment.getComment()).concat(IOUtils.LINE_SEPARATOR);
                } else {
                    // Not JavadocComment, write comment as it is, unchanged
                    appendFormalLine(comment.getComment());
                }
            }
            // Write JavadocComment's to ITD, in a single JavadocComment instance
            String[] commentLines = commentString.split(IOUtils.LINE_SEPARATOR);
            for (String commentLine : commentLines) {
                appendFormalLine(commentLine);
            }
        } else {
            // ROO-3834: Append default Javadoc if not exists a comment structure,
            // including params, return and throws
            CommentStructure defaultCommentStructure = new CommentStructure();
            List<String> parameterNames = new ArrayList<String>();
            for (JavaSymbolName name : method.getParameterNames()) {
                parameterNames.add(name.getSymbolName());
            }
            List<String> throwsTypesNames = new ArrayList<String>();
            for (JavaType type : method.getThrowsTypes()) {
                throwsTypesNames.add(type.getSimpleTypeName());
            }
            String returnInfo = null;
            JavaType returnType = method.getReturnType();
            if (!returnType.equals(JavaType.VOID_OBJECT) && !returnType.equals(JavaType.VOID_PRIMITIVE)) {
                returnInfo = returnType.getSimpleTypeName();
            }
            JavadocComment javadocComment = new JavadocComment("TODO Auto-generated method documentation", parameterNames, returnInfo, throwsTypesNames);
            defaultCommentStructure.addComment(javadocComment, CommentLocation.BEGINNING);
            method.setCommentStructure(defaultCommentStructure);
            // Now lines should be formatted, so write them
            String[] comment = javadocComment.getComment().split(IOUtils.LINE_SEPARATOR);
            for (String line : comment) {
                appendFormalLine(line);
            }
        }
        // Append annotations
        for (final AnnotationMetadata annotation : method.getAnnotations()) {
            appendIndent();
            outputAnnotation(annotation);
            this.newLine(false);
        }
        // Append "<modifier> <genericDefinition> <returnType> <methodName>" portion
        appendIndent();
        // modifier
        if (method.getModifier() != 0) {
            append(Modifier.toString(method.getModifier()));
            append(" ");
        }
        // ROO-3648: genericDefinition
        if (method.getGenericDefinition() != null && !method.getGenericDefinition().trim().equals("")) {
            append("<".concat(method.getGenericDefinition()).concat(">"));
            append(" ");
        }
        // return type
        append(method.getReturnType().getNameIncludingTypeParameters(false, resolver));
        append(" ");
        if (defineTarget) {
            append(introductionTo.getSimpleTypeName());
            append(".");
        }
        append(method.getMethodName().getSymbolName());
        // Append parameter types and names
        append("(");
        final List<AnnotatedJavaType> parameterTypes = method.getParameterTypes();
        final List<JavaSymbolName> parameterNames = method.getParameterNames();
        for (int i = 0; i < parameterTypes.size(); i++) {
            final AnnotatedJavaType paramType = parameterTypes.get(i);
            final JavaSymbolName paramName = parameterNames.get(i);
            for (final AnnotationMetadata methodParameterAnnotation : paramType.getAnnotations()) {
                outputAnnotation(methodParameterAnnotation);
                append(" ");
            }
            append(paramType.getJavaType().getNameIncludingTypeParameters(false, resolver));
            append(" ");
            append(paramName.getSymbolName());
            if (i < parameterTypes.size() - 1) {
                append(", ");
            }
        }
        // Add exceptions to be thrown
        final List<JavaType> throwsTypes = method.getThrowsTypes();
        if (throwsTypes.size() > 0) {
            append(") throws ");
            for (int i = 0; i < throwsTypes.size(); i++) {
                append(throwsTypes.get(i).getNameIncludingTypeParameters(false, resolver));
                if (throwsTypes.size() > i + 1) {
                    append(", ");
                }
            }
        } else {
            append(")");
        }
        if (isInterfaceMethod) {
            append(";");
            this.newLine(false);
        } else {
            append(" {");
            this.newLine(false);
            // Add body
            indent();
            append(method.getBody());
            indentRemove();
            appendFormalLine("}");
        }
        this.newLine();
    }
}
Also used : AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) CommentStructure(org.springframework.roo.classpath.details.comments.CommentStructure) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) JavaType(org.springframework.roo.model.JavaType) JavadocComment(org.springframework.roo.classpath.details.comments.JavadocComment) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) AbstractComment(org.springframework.roo.classpath.details.comments.AbstractComment)

Example 45 with AnnotationMetadata

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

the class FieldMatcher method getAttributeMap.

private Map<String, Object> getAttributeMap(final FieldMetadata field) {
    final Map<String, Object> map = new HashMap<String, Object>();
    final AnnotationMetadata annotationMetadata = getMatchingAnnotation(field);
    if (annotationMetadata != null) {
        for (final JavaSymbolName attributeName : annotationMetadata.getAttributeNames()) {
            map.put(attributeName.getSymbolName(), annotationMetadata.getAttribute(attributeName).getValue());
        }
    }
    return map;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) HashMap(java.util.HashMap) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Aggregations

AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)109 JavaType (org.springframework.roo.model.JavaType)59 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)52 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)47 ArrayList (java.util.ArrayList)40 RooJavaType (org.springframework.roo.model.RooJavaType)34 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)30 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)24 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)21 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)20 List (java.util.List)19 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)19 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)18 AnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue)17 NestedAnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)16 SpringJavaType (org.springframework.roo.model.SpringJavaType)15 JdkJavaType (org.springframework.roo.model.JdkJavaType)12 JpaJavaType (org.springframework.roo.model.JpaJavaType)12 ArrayAttributeValue (org.springframework.roo.classpath.details.annotations.ArrayAttributeValue)11 LinkedHashMap (java.util.LinkedHashMap)10