Search in sources :

Example 11 with AnnotationAttributeValue

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

the class AutoPopulationUtils method populate.

/**
 * Introspects the target {@link Object} for its declared fields, locating
 * all {@link AutoPopulate} annotated fields. For each field, an attempt
 * will be made to locate the value from the passed
 * {@link AnnotationMetadata}. The annotation value will be converted into
 * the required field type, or silently skipped if this is not possible (eg
 * the user edited source code and made a formatting error). As such it is
 * important that the caller.
 *
 * @param target to put values into (mandatory, cannot be null)
 * @param annotation to obtain values from (can be null, for convenience of
 *            the caller)
 */
public static void populate(final Object target, final AnnotationMetadata annotation) {
    Validate.notNull(target, "Target required");
    if (annotation == null) {
        return;
    }
    List<Field> fields = cachedIntrospections.get(target.getClass());
    if (fields == null) {
        // Go and cache them
        fields = new ArrayList<Field>();
        for (final Field field : target.getClass().getDeclaredFields()) {
            // Determine if this field even contains the necessary
            // annotation
            final AutoPopulate ap = field.getAnnotation(AutoPopulate.class);
            if (ap == null) {
                continue;
            }
            // Determine attribute name we should be looking for in the
            // annotation
            String attribute = ap.value();
            if ("".equals(ap.value())) {
                attribute = field.getName();
            }
            // Ensure field is accessible
            if (!field.isAccessible()) {
                field.setAccessible(true);
            }
            final JavaSymbolName attributeName = new JavaSymbolName(attribute);
            // Store the info
            fields.add(field);
            attributeNameForEachField.put(field, attributeName);
        }
        cachedIntrospections.put(target.getClass(), fields);
    }
    for (final Field field : fields) {
        // Lookup whether this attribute name was provided
        final JavaSymbolName attributeName = attributeNameForEachField.get(field);
        if (attributeName == null) {
            throw new IllegalStateException("Expected cached attribute name lookup");
        }
        if (annotation.getAttributeNames().contains(attributeName)) {
            // Get the value
            final AnnotationAttributeValue<?> value = annotation.getAttribute(attributeName);
            // Assign the value to the target object
            try {
                final Class<?> fieldType = field.getType();
                if (value instanceof BooleanAttributeValue && (fieldType.equals(Boolean.class) || fieldType.equals(Boolean.TYPE))) {
                    field.set(target, value.getValue());
                } else if (value instanceof CharAttributeValue && (fieldType.equals(Character.class) || fieldType.equals(Character.TYPE))) {
                    field.set(target, value.getValue());
                } else if (value instanceof ClassAttributeValue && fieldType.equals(JavaType.class)) {
                    field.set(target, value.getValue());
                } else if (value instanceof DoubleAttributeValue && (fieldType.equals(Double.class) || fieldType.equals(Double.TYPE))) {
                    field.set(target, value.getValue());
                } else if (value instanceof EnumAttributeValue && Enum.class.isAssignableFrom(fieldType)) {
                    field.set(target, ((EnumAttributeValue) value).getAsEnum(target));
                } else if (value instanceof IntegerAttributeValue && (fieldType.equals(Integer.class) || fieldType.equals(Integer.TYPE))) {
                    field.set(target, value.getValue());
                } else if (value instanceof LongAttributeValue && (fieldType.equals(Long.class) || fieldType.equals(Long.TYPE))) {
                    field.set(target, value.getValue());
                } else if (value instanceof StringAttributeValue && fieldType.equals(String.class)) {
                    field.set(target, value.getValue());
                } else if (value instanceof StringAttributeValue && fieldType.getComponentType() != null && fieldType.getComponentType().equals(String.class)) {
                    // ROO-618
                    final Object newValue = Array.newInstance(String.class, 1);
                    Array.set(newValue, 0, value.getValue());
                    field.set(target, newValue);
                } else if (value instanceof ArrayAttributeValue<?> && fieldType.isArray()) {
                    // The field is a string array, the attribute is an
                    // array, so let's hope it's a string array
                    final ArrayAttributeValue<?> castValue = (ArrayAttributeValue<?>) value;
                    final List<String> result = new ArrayList<String>();
                    final List<JavaType> result1 = new ArrayList<JavaType>();
                    for (final AnnotationAttributeValue<?> val : castValue.getValue()) {
                        // For now we'll only support arrays of strings
                        if (fieldType.getComponentType().equals(String.class) && val instanceof StringAttributeValue) {
                            final StringAttributeValue stringValue = (StringAttributeValue) val;
                            result.add(stringValue.getValue());
                        } else if (fieldType.getComponentType().equals(JavaType.class) && val instanceof ClassAttributeValue) {
                            final ClassAttributeValue classValue = (ClassAttributeValue) val;
                            result1.add(classValue.getValue());
                        }
                    }
                    if (result.size() > 0) {
                        // We had at least one string array, so we change
                        // the field
                        field.set(target, result.toArray(new String[] {}));
                    }
                    if (result1.size() > 0) {
                        // We had at least one string array, so we change
                        // the field
                        field.set(target, result1.toArray(new JavaType[] {}));
                    }
                }
            // If not in the above list, it's unsupported so we silently
            // skip
            } catch (final Throwable ignoreFailures) {
            }
        }
    }
}
Also used : ClassAttributeValue(org.springframework.roo.classpath.details.annotations.ClassAttributeValue) DoubleAttributeValue(org.springframework.roo.classpath.details.annotations.DoubleAttributeValue) CharAttributeValue(org.springframework.roo.classpath.details.annotations.CharAttributeValue) EnumAttributeValue(org.springframework.roo.classpath.details.annotations.EnumAttributeValue) Field(java.lang.reflect.Field) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ArrayList(java.util.ArrayList) List(java.util.List) StringAttributeValue(org.springframework.roo.classpath.details.annotations.StringAttributeValue) ArrayAttributeValue(org.springframework.roo.classpath.details.annotations.ArrayAttributeValue) AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) IntegerAttributeValue(org.springframework.roo.classpath.details.annotations.IntegerAttributeValue) BooleanAttributeValue(org.springframework.roo.classpath.details.annotations.BooleanAttributeValue) JavaType(org.springframework.roo.model.JavaType) LongAttributeValue(org.springframework.roo.classpath.details.annotations.LongAttributeValue)

Example 12 with AnnotationAttributeValue

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

the class FieldDetails method decorateAnnotationsList.

public void decorateAnnotationsList(final List<AnnotationMetadataBuilder> annotations) {
    Validate.notNull(annotations);
    if (notNull) {
        annotations.add(new AnnotationMetadataBuilder(NOT_NULL));
    }
    if (nullRequired) {
        annotations.add(new AnnotationMetadataBuilder(NULL));
    }
    AnnotationMetadataBuilder columnBuilder = null;
    if (column != null) {
        final List<AnnotationAttributeValue<?>> attrs = new ArrayList<AnnotationAttributeValue<?>>();
        attrs.add(new StringAttributeValue(new JavaSymbolName("name"), column));
        columnBuilder = new AnnotationMetadataBuilder(COLUMN, attrs);
    }
    if (unique) {
        if (columnBuilder != null) {
            columnBuilder.addBooleanAttribute("unique", true);
        } else {
            final List<AnnotationAttributeValue<?>> attrs = new ArrayList<AnnotationAttributeValue<?>>();
            attrs.add(new BooleanAttributeValue(new JavaSymbolName("unique"), true));
            columnBuilder = new AnnotationMetadataBuilder(COLUMN, attrs);
        }
    }
    if (value != null) {
        final List<AnnotationAttributeValue<?>> attrs = new ArrayList<AnnotationAttributeValue<?>>();
        attrs.add(new StringAttributeValue(new JavaSymbolName("value"), value));
        annotations.add(new AnnotationMetadataBuilder(VALUE, attrs));
    }
    if (fieldName.getSymbolName().equals("created")) {
        if (columnBuilder == null) {
            columnBuilder = new AnnotationMetadataBuilder(COLUMN);
        }
        columnBuilder.addBooleanAttribute("updatable", false);
    }
    if (columnBuilder != null) {
        annotations.add(columnBuilder);
    }
}
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)

Example 13 with AnnotationAttributeValue

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

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

the class NumericField method decorateAnnotationsList.

@Override
public void decorateAnnotationsList(final List<AnnotationMetadataBuilder> annotations) {
    super.decorateAnnotationsList(annotations);
    if (min != null) {
        final List<AnnotationAttributeValue<?>> attrs = new ArrayList<AnnotationAttributeValue<?>>();
        attrs.add(new LongAttributeValue(new JavaSymbolName("value"), min));
        annotations.add(new AnnotationMetadataBuilder(MIN, attrs));
    }
    if (max != null) {
        final List<AnnotationAttributeValue<?>> attrs = new ArrayList<AnnotationAttributeValue<?>>();
        attrs.add(new LongAttributeValue(new JavaSymbolName("value"), max));
        annotations.add(new AnnotationMetadataBuilder(MAX, attrs));
    }
    Validate.isTrue(isDigitsSetCorrectly(), "Validation constraints for @Digit are not correctly set");
    if (digitsInteger != null) {
        final List<AnnotationAttributeValue<?>> attrs = new ArrayList<AnnotationAttributeValue<?>>();
        attrs.add(new IntegerAttributeValue(new JavaSymbolName("integer"), digitsInteger));
        attrs.add(new IntegerAttributeValue(new JavaSymbolName("fraction"), digitsFraction));
        annotations.add(new AnnotationMetadataBuilder(DIGITS, attrs));
    }
    // Always add a default @NumberFormat
    annotations.add(new AnnotationMetadataBuilder(SpringJavaType.NUMBER_FORMAT));
}
Also used : AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) LongAttributeValue(org.springframework.roo.classpath.details.annotations.LongAttributeValue) ArrayList(java.util.ArrayList) IntegerAttributeValue(org.springframework.roo.classpath.details.annotations.IntegerAttributeValue) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 15 with AnnotationAttributeValue

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

Aggregations

AnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue)41 ArrayList (java.util.ArrayList)36 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)36 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)31 StringAttributeValue (org.springframework.roo.classpath.details.annotations.StringAttributeValue)23 NestedAnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)22 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)17 ArrayAttributeValue (org.springframework.roo.classpath.details.annotations.ArrayAttributeValue)16 ClassAttributeValue (org.springframework.roo.classpath.details.annotations.ClassAttributeValue)13 JavaType (org.springframework.roo.model.JavaType)13 List (java.util.List)12 EnumAttributeValue (org.springframework.roo.classpath.details.annotations.EnumAttributeValue)11 EnumDetails (org.springframework.roo.model.EnumDetails)11 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)10 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)8 IntegerAttributeValue (org.springframework.roo.classpath.details.annotations.IntegerAttributeValue)7 BooleanAttributeValue (org.springframework.roo.classpath.details.annotations.BooleanAttributeValue)6 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)4 LongAttributeValue (org.springframework.roo.classpath.details.annotations.LongAttributeValue)4 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)4