Search in sources :

Example 81 with AnnotationMetadata

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

the class ItdSourceFileComposer method appendFields.

private void appendFields() {
    final List<? extends FieldMetadata> fields = itdTypeDetails.getDeclaredFields();
    if (fields == null || fields.isEmpty()) {
        return;
    }
    content = true;
    for (final FieldMetadata field : fields) {
        // ROO-3447: Append comments if exists
        CommentStructure commentStructure = field.getCommentStructure();
        if (commentStructure != null && commentStructure.getBeginComments() != null) {
            List<AbstractComment> comments = commentStructure.getBeginComments();
            String commentString = "";
            for (AbstractComment comment : comments) {
                // Join all JavadocComment's
                if (comment instanceof JavadocComment) {
                    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
            JavadocComment javadocComment = new JavadocComment("TODO Auto-generated attribute documentation");
            // 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 : field.getAnnotations()) {
            appendIndent();
            outputAnnotation(annotation);
            this.newLine(false);
        }
        // Append "<modifier> <fieldType> <fieldName>" portion
        appendIndent();
        if (field.getModifier() != 0) {
            append(Modifier.toString(field.getModifier()));
            append(" ");
        }
        append(field.getFieldType().getNameIncludingTypeParameters(false, resolver));
        append(" ");
        append(introductionTo.getSimpleTypeName());
        append(".");
        append(field.getFieldName().getSymbolName());
        // Append initializer, if present
        if (field.getFieldInitializer() != null) {
            append(" = ");
            append(field.getFieldInitializer());
        }
        // Complete the field declaration
        append(";");
        this.newLine(false);
        this.newLine();
    }
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) JavadocComment(org.springframework.roo.classpath.details.comments.JavadocComment) AbstractComment(org.springframework.roo.classpath.details.comments.AbstractComment) CommentStructure(org.springframework.roo.classpath.details.comments.CommentStructure) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Example 82 with AnnotationMetadata

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

the class AbstractIdentifiableAnnotatedJavaStructureBuilder method updateTypeAnnotation.

public boolean updateTypeAnnotation(final AnnotationMetadata annotation, final Set<JavaSymbolName> attributesToDeleteIfPresent) {
    boolean hasChanged = false;
    // We are going to build a replacement AnnotationMetadata.
    // This variable tracks the new attribute values the replacement will
    // hold.
    final Map<JavaSymbolName, AnnotationAttributeValue<?>> replacementAttributeValues = new LinkedHashMap<JavaSymbolName, AnnotationAttributeValue<?>>();
    final AnnotationMetadataBuilder existingBuilder = getDeclaredTypeAnnotation(annotation.getAnnotationType());
    if (existingBuilder == null) {
        // Not already present, so just go and add it
        for (final JavaSymbolName incomingAttributeName : annotation.getAttributeNames()) {
            // attributesToDeleteIfPresent Set
            if (attributesToDeleteIfPresent == null || !attributesToDeleteIfPresent.contains(incomingAttributeName)) {
                final AnnotationAttributeValue<?> incomingValue = annotation.getAttribute(incomingAttributeName);
                replacementAttributeValues.put(incomingAttributeName, incomingValue);
            }
        }
        final AnnotationMetadataBuilder replacement = new AnnotationMetadataBuilder(annotation.getAnnotationType(), new ArrayList<AnnotationAttributeValue<?>>(replacementAttributeValues.values()));
        addAnnotation(replacement);
        return true;
    }
    final AnnotationMetadata existing = existingBuilder.build();
    // Copy the existing attributes into the new attributes
    for (final JavaSymbolName existingAttributeName : existing.getAttributeNames()) {
        if (attributesToDeleteIfPresent != null && attributesToDeleteIfPresent.contains(existingAttributeName)) {
            hasChanged = true;
        } else {
            final AnnotationAttributeValue<?> existingValue = existing.getAttribute(existingAttributeName);
            replacementAttributeValues.put(existingAttributeName, existingValue);
        }
    }
    // Now we ensure every incoming attribute replaces the existing
    for (final JavaSymbolName incomingAttributeName : annotation.getAttributeNames()) {
        final AnnotationAttributeValue<?> incomingValue = annotation.getAttribute(incomingAttributeName);
        // already present
        if (replacementAttributeValues.keySet().contains(incomingAttributeName)) {
            // There was already an attribute. Need to determine if this new
            // attribute value is materially different
            final AnnotationAttributeValue<?> existingValue = replacementAttributeValues.get(incomingAttributeName);
            Validate.notNull(existingValue, "Existing value should have been provided by earlier loop");
            if (!existingValue.equals(incomingValue)) {
                replacementAttributeValues.put(incomingAttributeName, incomingValue);
                hasChanged = true;
            }
        } else if (attributesToDeleteIfPresent == null || !attributesToDeleteIfPresent.contains(incomingAttributeName)) {
            // This is a new attribute that does not already exist, so add
            // it to the end of the replacement attributes
            replacementAttributeValues.put(incomingAttributeName, incomingValue);
            hasChanged = true;
        }
    }
    // Were there any material changes?
    if (!hasChanged) {
        return false;
    }
    // Make a new AnnotationMetadata representing the replacement
    final AnnotationMetadataBuilder replacement = new AnnotationMetadataBuilder(annotation.getAnnotationType(), new ArrayList<AnnotationAttributeValue<?>>(replacementAttributeValues.values()));
    annotations.remove(existingBuilder);
    addAnnotation(replacement);
    return true;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) LinkedHashMap(java.util.LinkedHashMap)

Example 83 with AnnotationMetadata

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

the class AbstractIdentifiableAnnotatedJavaStructureBuilder method setAnnotations.

public final void setAnnotations(final Collection<AnnotationMetadata> annotations) {
    final List<AnnotationMetadataBuilder> annotationBuilders = new ArrayList<AnnotationMetadataBuilder>();
    for (final AnnotationMetadata annotationMetadata : annotations) {
        annotationBuilders.add(new AnnotationMetadataBuilder(annotationMetadata));
    }
    setAnnotations(annotationBuilders);
}
Also used : ArrayList(java.util.ArrayList) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Example 84 with AnnotationMetadata

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

the class LayerTypeMatcher method getTagValue.

@Override
public Object getTagValue(final MemberHoldingTypeDetails type) {
    final AnnotationMetadata layerAnnotation = MemberFindingUtils.getAnnotationOfType(type.getAnnotations(), layerAnnotationType);
    if (layerAnnotation == null || layerAnnotation.getAttribute(domainTypesAttribute) == null) {
        return null;
    }
    final AnnotationAttributeValue<?> value = layerAnnotation.getAttribute(domainTypesAttribute);
    final List<JavaType> domainTypes = new ArrayList<JavaType>();
    if (value instanceof ClassAttributeValue) {
        domainTypes.add(((ClassAttributeValue) value).getValue());
    } else if (value instanceof ArrayAttributeValue<?>) {
        final ArrayAttributeValue<?> castValue = (ArrayAttributeValue<?>) value;
        for (final AnnotationAttributeValue<?> val : castValue.getValue()) {
            if (val instanceof ClassAttributeValue) {
                domainTypes.add(((ClassAttributeValue) val).getValue());
            }
        }
    }
    return domainTypes;
}
Also used : ArrayAttributeValue(org.springframework.roo.classpath.details.annotations.ArrayAttributeValue) JavaType(org.springframework.roo.model.JavaType) AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) ClassAttributeValue(org.springframework.roo.classpath.details.annotations.ClassAttributeValue) ArrayList(java.util.ArrayList) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Example 85 with AnnotationMetadata

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

the class DtoOperationsImpl method addFieldsToProjection.

/**
 * Removes persistence annotations of provided fields and adds them to a
 * ClassOrInterfaceTypeDetailsBuilder representing a Projection in construction.
 * Also adds final modifier to fields if required.
 *
 * @param projectionBuilder the ClassOrInterfaceTypeDetailsBuilder for building the
 *            Projection class.
 * @param fieldsToAdd the List<FieldMetadata> to add.
 */
private void addFieldsToProjection(ClassOrInterfaceTypeDetailsBuilder projectionBuilder, Map<String, FieldMetadata> fieldsToAdd) {
    Iterator<Entry<String, FieldMetadata>> iterator = fieldsToAdd.entrySet().iterator();
    while (iterator.hasNext()) {
        Entry<String, FieldMetadata> entry = iterator.next();
        FieldMetadata field = entry.getValue();
        // List and Set types require special management
        FieldMetadataBuilder fieldBuilder = null;
        FieldDetails fieldDetails = null;
        if (field.getFieldType().getFullyQualifiedTypeName().equals("java.util.Set")) {
            JavaType fieldType = field.getFieldType().getParameters().get(0);
            fieldDetails = new SetField(projectionBuilder.getDeclaredByMetadataId(), new JavaType(JdkJavaType.SET.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Arrays.asList(fieldType)), field.getFieldName(), fieldType, null, null, true);
            fieldBuilder = new FieldMetadataBuilder(fieldDetails);
            fieldBuilder.setModifier(field.getModifier());
            fieldBuilder.setAnnotations(field.getAnnotations());
        } else if (field.getFieldType().getFullyQualifiedTypeName().equals("java.util.List")) {
            JavaType fieldType = field.getFieldType().getParameters().get(0);
            fieldDetails = new ListField(projectionBuilder.getDeclaredByMetadataId(), new JavaType(JdkJavaType.LIST.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Arrays.asList(fieldType)), field.getFieldName(), fieldType, null, null, true);
            fieldBuilder = new FieldMetadataBuilder(fieldDetails);
            fieldBuilder.setModifier(field.getModifier());
            fieldBuilder.setAnnotations(field.getAnnotations());
        } else {
            // Can't just copy FieldMetadata because field.declaredByMetadataId will be the same
            fieldBuilder = new FieldMetadataBuilder(projectionBuilder.getDeclaredByMetadataId(), field);
        }
        // Add dependency between modules
        typeLocationService.addModuleDependency(projectionBuilder.getName().getModule(), field.getFieldType());
        // Set new fieldName
        fieldBuilder.setFieldName(new JavaSymbolName(entry.getKey()));
        // If it is a CollectionField it needs an initializer
        String initializer = null;
        if (fieldDetails instanceof CollectionField) {
            final CollectionField collectionField = (CollectionField) fieldDetails;
            initializer = "new " + collectionField.getInitializer() + "()";
        } else if (fieldDetails instanceof DateField && fieldDetails.getFieldName().getSymbolName().equals("created")) {
            initializer = "new Date()";
        }
        fieldBuilder.setFieldInitializer(initializer);
        // Remove persistence annotations
        List<AnnotationMetadata> annotations = field.getAnnotations();
        for (AnnotationMetadata annotation : annotations) {
            if (annotation.getAnnotationType().getFullyQualifiedTypeName().contains("javax.persistence")) {
                fieldBuilder.removeAnnotation(annotation.getAnnotationType());
            } else if (annotation.getAnnotationType().getFullyQualifiedTypeName().startsWith("javax.validation")) {
                // Add validation dependency
                projectOperations.addDependency(projectionBuilder.getName().getModule(), new Dependency("javax.validation", "validation-api", null));
            } else if (annotation.getAnnotationType().equals(RooJavaType.ROO_JPA_RELATION)) {
                fieldBuilder.removeAnnotation(annotation.getAnnotationType());
            }
        }
        fieldBuilder.setModifier(Modifier.PRIVATE);
        // Build field
        FieldMetadata projectionField = fieldBuilder.build();
        // Add field to DTO
        projectionBuilder.addField(projectionField);
    }
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) FieldDetails(org.springframework.roo.classpath.details.FieldDetails) ListField(org.springframework.roo.classpath.operations.jsr303.ListField) Dependency(org.springframework.roo.project.Dependency) SetField(org.springframework.roo.classpath.operations.jsr303.SetField) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder) Entry(java.util.Map.Entry) JdkJavaType(org.springframework.roo.model.JdkJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) DateField(org.springframework.roo.classpath.operations.jsr303.DateField) CollectionField(org.springframework.roo.classpath.operations.jsr303.CollectionField)

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