Search in sources :

Example 16 with StringAttributeValue

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

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

the class ReferenceField method setJoinAnnotations.

/**
 * Fill {@link #joinTableAttributes} for building @JoinTable annotation. The annotation
 * would have some nested @JoinColumn annotations in each of its "joinColumns" and
 * "inverseJoinColumns" attributes.
 *
 * @param joinTableName
 * @param joinColumns
 * @param referencedColumns
 * @param inverseJoinColumns
 * @param inverseReferencedColumns
 */
public void setJoinAnnotations(String joinTableName, String[] joinColumns, String[] referencedColumns, String[] inverseJoinColumns, String[] inverseReferencedColumns) {
    final List<AnnotationMetadataBuilder> joinColumnsBuilders = new ArrayList<AnnotationMetadataBuilder>();
    if (joinColumns != null) {
        // Build joinColumns attribute
        for (int i = 0; i < joinColumns.length; i++) {
            // Build @JoinColumn annotation for owner side of the relation
            final AnnotationMetadataBuilder joinColumnAnnotation = new AnnotationMetadataBuilder(JOIN_COLUMN);
            joinColumnAnnotation.addStringAttribute("name", joinColumns[i]);
            if (referencedColumns != null) {
                joinColumnAnnotation.addStringAttribute("referencedColumnName", referencedColumns[i]);
            }
            joinColumnsBuilders.add(joinColumnAnnotation);
        }
    }
    final List<AnnotationMetadataBuilder> inverseJoinColumnsBuilders = new ArrayList<AnnotationMetadataBuilder>();
    if (inverseJoinColumns != null) {
        // Build inverseJoinColumns attribute
        for (int i = 0; i < inverseJoinColumns.length; i++) {
            // Build @JoinColumn annotation for the not owner side of the relation
            final AnnotationMetadataBuilder inverseJoinColumnsAnnotation = new AnnotationMetadataBuilder(JOIN_COLUMN);
            inverseJoinColumnsAnnotation.addStringAttribute("name", inverseJoinColumns[i]);
            inverseJoinColumnsAnnotation.addStringAttribute("referencedColumnName", inverseReferencedColumns[i]);
            inverseJoinColumnsBuilders.add(inverseJoinColumnsAnnotation);
        }
    }
    if (StringUtils.isNotBlank(joinTableName) || !inverseJoinColumnsBuilders.isEmpty()) {
        // add @JoinTable annotation
        // Add attributes for @JoinTable annotation
        final List<AnnotationAttributeValue<?>> joinTableAttributes = new ArrayList<AnnotationAttributeValue<?>>();
        // If name not specified, use default name value
        joinTableAttributes.add(new StringAttributeValue(new JavaSymbolName("name"), joinTableName));
        // If joinColumns options were not specified, use default @JoinColumn values
        if (joinColumns != null) {
            final List<AnnotationAttributeValue<?>> joinColumnsAnnotations = new ArrayList<AnnotationAttributeValue<?>>();
            for (AnnotationMetadataBuilder joinColumnAnnotation : joinColumnsBuilders) {
                joinColumnsAnnotations.add(new NestedAnnotationAttributeValue(new JavaSymbolName("joinColumns"), joinColumnAnnotation.build()));
            }
            joinTableAttributes.add(new ArrayAttributeValue<AnnotationAttributeValue<?>>(new JavaSymbolName("joinColumns"), joinColumnsAnnotations));
        }
        // If inverseJoinColumns options were not specified, use default @JoinColumn values
        if (inverseJoinColumns != null) {
            final List<AnnotationAttributeValue<?>> inverseJoinColumnsAnnotations = new ArrayList<AnnotationAttributeValue<?>>();
            for (AnnotationMetadataBuilder inverseJoinColumnsAnnotation : inverseJoinColumnsBuilders) {
                inverseJoinColumnsAnnotations.add(new NestedAnnotationAttributeValue(new JavaSymbolName("inverseJoinColumns"), inverseJoinColumnsAnnotation.build()));
            }
            joinTableAttributes.add(new ArrayAttributeValue<AnnotationAttributeValue<?>>(new JavaSymbolName("inverseJoinColumns"), inverseJoinColumnsAnnotations));
        }
        // Add @JoinTable to additonalAnnotations
        additionaAnnotations.add(new AnnotationMetadataBuilder(JOIN_TABLE, joinTableAttributes));
    } else if (!joinColumnsBuilders.isEmpty()) {
        if (joinColumnsBuilders.size() == 1) {
            // Just one @JoinColumn
            additionaAnnotations.add(joinColumnsBuilders.iterator().next());
        } else {
            // Multiple @JoinColumn, wrap with @JoinColumns
            final AnnotationMetadataBuilder joinColumnsAnnotation = new AnnotationMetadataBuilder(JOIN_COLUMNS);
            final List<AnnotationAttributeValue<?>> joinColumnsAnnotations = new ArrayList<AnnotationAttributeValue<?>>();
            for (AnnotationMetadataBuilder joinColumnAnnotation : joinColumnsBuilders) {
                joinColumnsAnnotations.add(new NestedAnnotationAttributeValue(new JavaSymbolName("value"), joinColumnAnnotation.build()));
            }
            joinColumnsAnnotation.addAttribute(new ArrayAttributeValue<AnnotationAttributeValue<?>>(new JavaSymbolName("value"), joinColumnsAnnotations));
            // Add @JoinColumns
            additionaAnnotations.add(joinColumnsAnnotation);
        }
    }
}
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) ArrayList(java.util.ArrayList) List(java.util.List) 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 18 with StringAttributeValue

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

the class ReferenceField method decorateAnnotationsList.

@Override
public void decorateAnnotationsList(final List<AnnotationMetadataBuilder> annotations) {
    super.decorateAnnotationsList(annotations);
    final List<AnnotationAttributeValue<?>> attributes = new ArrayList<AnnotationAttributeValue<?>>();
    // Add cascade if option exists
    if (cascadeType != null) {
        List<EnumAttributeValue> cascadeValues = new ArrayList<EnumAttributeValue>();
        for (Cascade type : cascadeType) {
            cascadeValues.add(new EnumAttributeValue(new JavaSymbolName("cascade"), new EnumDetails(CASCADE_TYPE, new JavaSymbolName(type.name()))));
        }
        attributes.add(new ArrayAttributeValue<EnumAttributeValue>(new JavaSymbolName("cascade"), cascadeValues));
    }
    // Add orphanRemoval if option exists
    if (getOrphanRemoval() != null) {
        attributes.add(new BooleanAttributeValue(new JavaSymbolName("orphanRemoval"), getOrphanRemoval().booleanValue()));
    }
    if (fetch != null) {
        JavaSymbolName value = new JavaSymbolName("EAGER");
        if (fetch == Fetch.LAZY) {
            value = new JavaSymbolName("LAZY");
        }
        attributes.add(new EnumAttributeValue(new JavaSymbolName("fetch"), new EnumDetails(FETCH_TYPE, value)));
    }
    if (mappedBy != null) {
        attributes.add(new StringAttributeValue(new JavaSymbolName("mappedBy"), mappedBy.getSymbolName()));
    }
    switch(cardinality) {
        case ONE_TO_MANY:
            annotations.add(new AnnotationMetadataBuilder(ONE_TO_MANY, attributes));
            break;
        case MANY_TO_MANY:
            annotations.add(new AnnotationMetadataBuilder(MANY_TO_MANY, attributes));
            break;
        case ONE_TO_ONE:
            annotations.add(new AnnotationMetadataBuilder(ONE_TO_ONE, attributes));
            break;
        default:
            annotations.add(new AnnotationMetadataBuilder(MANY_TO_ONE, attributes));
            break;
    }
    // Add additional annotations (if any)
    if (additionaAnnotations != null) {
        annotations.addAll(additionaAnnotations);
    }
}
Also used : AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) BooleanAttributeValue(org.springframework.roo.classpath.details.annotations.BooleanAttributeValue) 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) Cascade(org.springframework.roo.classpath.operations.Cascade) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 19 with StringAttributeValue

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

the class StringField method decorateAnnotationsList.

@Override
public void decorateAnnotationsList(final List<AnnotationMetadataBuilder> annotations) {
    super.decorateAnnotationsList(annotations);
    if (sizeMin != null || sizeMax != null) {
        final List<AnnotationAttributeValue<?>> attrs = new ArrayList<AnnotationAttributeValue<?>>();
        if (sizeMin != null) {
            attrs.add(new IntegerAttributeValue(new JavaSymbolName("min"), sizeMin));
        }
        if (sizeMax != null) {
            attrs.add(new IntegerAttributeValue(new JavaSymbolName("max"), sizeMax));
        }
        annotations.add(new AnnotationMetadataBuilder(SIZE, attrs));
    }
    if (regexp != null) {
        final List<AnnotationAttributeValue<?>> attrs = new ArrayList<AnnotationAttributeValue<?>>();
        attrs.add(new StringAttributeValue(new JavaSymbolName("regexp"), regexp));
        annotations.add(new AnnotationMetadataBuilder(PATTERN, attrs));
    }
}
Also used : AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ArrayList(java.util.ArrayList) IntegerAttributeValue(org.springframework.roo.classpath.details.annotations.IntegerAttributeValue) StringAttributeValue(org.springframework.roo.classpath.details.annotations.StringAttributeValue) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 20 with StringAttributeValue

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

the class UploadedFileField method decorateAnnotationsList.

@Override
public void decorateAnnotationsList(final List<AnnotationMetadataBuilder> annotations) {
    super.decorateAnnotationsList(annotations);
    final List<AnnotationAttributeValue<?>> attrs = new ArrayList<AnnotationAttributeValue<?>>();
    attrs.add(new StringAttributeValue(new JavaSymbolName("contentType"), contentType.getContentType()));
    if (autoUpload) {
        attrs.add(new BooleanAttributeValue(new JavaSymbolName("autoUpload"), autoUpload));
    }
    annotations.add(new AnnotationMetadataBuilder(ROO_UPLOADED_FILE, attrs));
    annotations.add(new AnnotationMetadataBuilder(LOB));
}
Also used : AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) BooleanAttributeValue(org.springframework.roo.classpath.details.annotations.BooleanAttributeValue) ArrayList(java.util.ArrayList) 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