Search in sources :

Example 6 with ArrayAttributeValue

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

the class RepositoryJpaIntegrationTestMetadata method getImportAnnotation.

/**
 * Builds and returns `@Import` annotation
 *
 * @return {@link AnnotationMetadataBuilder}
 */
private AnnotationMetadataBuilder getImportAnnotation() {
    AnnotationMetadataBuilder annotationBuilder = new AnnotationMetadataBuilder(SpringJavaType.ANNOTATION_IMPORT);
    // Create List of ClassAttributeValue
    List<ClassAttributeValue> typesToImport = new ArrayList<ClassAttributeValue>();
    typesToImport.add(new ClassAttributeValue(new JavaSymbolName("value"), this.annotationValues.getDodConfigurationClass()));
    typesToImport.add(new ClassAttributeValue(new JavaSymbolName("value"), this.jpaDetachableRepositoryClass));
    // Convert List to ArrayAttributeValue
    ArrayAttributeValue<ClassAttributeValue> importAttr = new ArrayAttributeValue<ClassAttributeValue>(new JavaSymbolName("value"), typesToImport);
    // Add annotation attribute
    annotationBuilder.addAttribute(importAttr);
    return annotationBuilder;
}
Also used : ArrayAttributeValue(org.springframework.roo.classpath.details.annotations.ArrayAttributeValue) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ClassAttributeValue(org.springframework.roo.classpath.details.annotations.ClassAttributeValue) ArrayList(java.util.ArrayList) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 7 with ArrayAttributeValue

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

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

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

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

the class SecurityOperationsImpl method getRooSecurityFilterAnnotation.

/**
 * This method provides {@link RooSecurityFilter} annotation with all the necessary
 * attributes
 *
 * @param method Method to add the annotation
 * @param lstParamTypes Parameter types of the method to add the annotation
 * @param roles Roles to apply by the filter
 * @param usernames Usernames apply by the filter
 * @param when Indicate the type of filter 'PRE' (@PreFilter) or 'POST' (@PostFilter)
 * @return the annotation created
 */
private AnnotationMetadataBuilder getRooSecurityFilterAnnotation(final String method, final List<AnnotationAttributeValue<?>> lstParamTypes, final String roles, final String usernames, final String when) {
    final List<AnnotationAttributeValue<?>> attributes = new ArrayList<AnnotationAttributeValue<?>>();
    attributes.add(new StringAttributeValue(new JavaSymbolName("method"), method));
    ArrayAttributeValue<AnnotationAttributeValue<?>> newParameters = new ArrayAttributeValue<AnnotationAttributeValue<?>>(new JavaSymbolName("parameters"), lstParamTypes);
    attributes.add(newParameters);
    if (roles != null) {
        attributes.add(new StringAttributeValue(new JavaSymbolName("roles"), roles));
    }
    if (usernames != null) {
        attributes.add(new StringAttributeValue(new JavaSymbolName("usernames"), usernames));
    }
    attributes.add(new StringAttributeValue(new JavaSymbolName("when"), when));
    return new AnnotationMetadataBuilder(RooJavaType.ROO_SECURITY_FILTER, attributes);
}
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) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ArrayList(java.util.ArrayList) StringAttributeValue(org.springframework.roo.classpath.details.annotations.StringAttributeValue) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Aggregations

ArrayAttributeValue (org.springframework.roo.classpath.details.annotations.ArrayAttributeValue)19 ArrayList (java.util.ArrayList)18 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)18 AnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue)16 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)13 NestedAnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)13 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)11 List (java.util.List)10 StringAttributeValue (org.springframework.roo.classpath.details.annotations.StringAttributeValue)10 ClassAttributeValue (org.springframework.roo.classpath.details.annotations.ClassAttributeValue)9 JavaType (org.springframework.roo.model.JavaType)8 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)5 EnumAttributeValue (org.springframework.roo.classpath.details.annotations.EnumAttributeValue)5 EnumDetails (org.springframework.roo.model.EnumDetails)5 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)4 BooleanAttributeValue (org.springframework.roo.classpath.details.annotations.BooleanAttributeValue)4 CharAttributeValue (org.springframework.roo.classpath.details.annotations.CharAttributeValue)4 DoubleAttributeValue (org.springframework.roo.classpath.details.annotations.DoubleAttributeValue)4 IntegerAttributeValue (org.springframework.roo.classpath.details.annotations.IntegerAttributeValue)3 LongAttributeValue (org.springframework.roo.classpath.details.annotations.LongAttributeValue)3