Search in sources :

Example 11 with ClassAttributeValue

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

the class ServiceOperationsImpl method createServiceImplementation.

/**
 * Method that creates the service implementation
 *
 * @param interfaceType
 * @param implType
 * @param domainType
 */
private void createServiceImplementation(final JavaType interfaceType, JavaType implType, ClassOrInterfaceTypeDetails repository, JavaType domainType) {
    Validate.notNull(interfaceType, "ERROR: Interface should be provided to be able to generate its implementation");
    Validate.notNull(interfaceType.getModule(), "ERROR: Interface module is required");
    Validate.notNull(domainType, "ERROR: Domain type required to generate service");
    // Generating implementation JavaType if needed
    if (implType == null) {
        implType = new JavaType(String.format("%sImpl", interfaceType.getFullyQualifiedTypeName()), interfaceType.getModule());
    }
    Validate.notNull(implType.getModule(), "ERROR: Implementation module is required");
    // Checks if new service interface already exists.
    final String implIdentifier = pathResolver.getCanonicalPath(implType.getModule(), Path.SRC_MAIN_JAVA, implType);
    if (fileManager.exists(implIdentifier)) {
        // Type already exists - nothing to do
        return;
    }
    // Generating @RooServiceImpl annotation
    final AnnotationMetadataBuilder implAnnotationMetadata = new AnnotationMetadataBuilder(ROO_SERVICE_IMPL);
    implAnnotationMetadata.addAttribute(new ClassAttributeValue(new JavaSymbolName("service"), interfaceType));
    // Creating class builder
    final String implMid = PhysicalTypeIdentifier.createIdentifier(implType, pathResolver.getPath(implIdentifier));
    final ClassOrInterfaceTypeDetailsBuilder implTypeBuilder = new ClassOrInterfaceTypeDetailsBuilder(implMid, PUBLIC, implType, PhysicalTypeCategory.CLASS);
    // Adding @RooService annotation to current interface
    implTypeBuilder.addAnnotation(implAnnotationMetadata.build());
    // Adding implements
    implTypeBuilder.addImplementsType(interfaceType);
    final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(implTypeBuilder.build().getType(), pathResolver.getPath(implType.getModule(), Path.SRC_MAIN_JAVA));
    // Write service implementation on disk
    typeManagementService.createOrUpdateTypeOnDisk(implTypeBuilder.build());
    // Add dependencies between modules
    projectOperations.addModuleDependency(implType.getModule(), interfaceType.getModule());
    projectOperations.addModuleDependency(implType.getModule(), repository.getName().getModule());
    projectOperations.addModuleDependency(implType.getModule(), domainType.getModule());
    // ROO-3799 Included dependency spring-tx if it's a multimodule project
    if (projectOperations.isMultimoduleProject()) {
        projectOperations.addDependency(implType.getModule(), new Dependency("org.springframework", "spring-tx", "", DependencyType.JAR, DependencyScope.COMPILE));
    }
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ClassAttributeValue(org.springframework.roo.classpath.details.annotations.ClassAttributeValue) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) Dependency(org.springframework.roo.project.Dependency) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 12 with ClassAttributeValue

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

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

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

the class SecurityOperationsImpl method checkRooSecurityFilterMaintainAnnotation.

/**
 * Check if {@link RooSecurityFilter} annotation should be kept
 * or should be replaced because is defined in the annotations list to add.
 *
 * @param rooSecurityFiltersToAdd Annotations list to add
 * @param filterAnnotation Annotation to check
 * @return
 */
private boolean checkRooSecurityFilterMaintainAnnotation(List<AnnotationAttributeValue<?>> rooSecurityFiltersToAdd, NestedAnnotationAttributeValue filterAnnotation) {
    boolean maintainAnnotation = true;
    String annotationMethod = (String) filterAnnotation.getValue().getAttribute("method").getValue();
    List<?> annotationParameters = (List<?>) filterAnnotation.getValue().getAttribute("parameters").getValue();
    String annotationWhen = (String) filterAnnotation.getValue().getAttribute("when").getValue();
    Iterator<AnnotationAttributeValue<?>> iterParamTypes = rooSecurityFiltersToAdd.iterator();
    while (iterParamTypes.hasNext()) {
        NestedAnnotationAttributeValue rooSecurityFilterToAdd = (NestedAnnotationAttributeValue) iterParamTypes.next();
        String annotationMethodToAdd = (String) rooSecurityFilterToAdd.getValue().getAttribute("method").getValue();
        List<?> annotationParametersToAdd = (List<?>) rooSecurityFilterToAdd.getValue().getAttribute("parameters").getValue();
        String annotationWhenToAdd = (String) rooSecurityFilterToAdd.getValue().getAttribute("when").getValue();
        boolean parametersAreEquals = true;
        if (annotationParametersToAdd.size() != annotationParameters.size()) {
            parametersAreEquals = false;
        } else {
            for (int i = 0; i < annotationParametersToAdd.size(); i++) {
                ClassAttributeValue classAnnotationParametersToAdd = (ClassAttributeValue) annotationParametersToAdd.get(i);
                ClassAttributeValue classAnnotationParameters = (ClassAttributeValue) annotationParameters.get(i);
                if (!classAnnotationParametersToAdd.getValue().getSimpleTypeName().equals(classAnnotationParameters.getValue().getSimpleTypeName())) {
                    parametersAreEquals = false;
                    break;
                }
            }
        }
        if (annotationMethodToAdd.equals(annotationMethod) && annotationWhenToAdd.equals(annotationWhen) && parametersAreEquals) {
            maintainAnnotation = false;
            break;
        }
    }
    return maintainAnnotation;
}
Also used : AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) ClassAttributeValue(org.springframework.roo.classpath.details.annotations.ClassAttributeValue) ArrayList(java.util.ArrayList) List(java.util.List) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)

Example 15 with ClassAttributeValue

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

the class SecurityOperationsImpl method checkRooSecurityAuthorizationMaintainAnnotation.

/**
 * Check if {@link RooSecurityAuthorization} annotation should be kept
 * or should be replaced because is defined in the annotations list to add.
 *
 * @param rooSecurityAuthorizationsToAdd Annotations list to add
 * @param authorizationAnnotation Annotation to check
 * @return
 */
private boolean checkRooSecurityAuthorizationMaintainAnnotation(List<AnnotationAttributeValue<?>> rooSecurityAuthorizationsToAdd, NestedAnnotationAttributeValue authorizationAnnotation) {
    boolean maintainAnnotation = true;
    String annotationMethod = (String) authorizationAnnotation.getValue().getAttribute("method").getValue();
    List<?> annotationParameters = (List<?>) authorizationAnnotation.getValue().getAttribute("parameters").getValue();
    Iterator<AnnotationAttributeValue<?>> iterParamTypes = rooSecurityAuthorizationsToAdd.iterator();
    while (iterParamTypes.hasNext()) {
        NestedAnnotationAttributeValue rooSecurityAuthorizationToAdd = (NestedAnnotationAttributeValue) iterParamTypes.next();
        String annotationMethodToAdd = (String) rooSecurityAuthorizationToAdd.getValue().getAttribute("method").getValue();
        List<?> annotationParametersToAdd = (List<?>) rooSecurityAuthorizationToAdd.getValue().getAttribute("parameters").getValue();
        boolean parametersAreEquals = true;
        if (annotationParametersToAdd.size() != annotationParameters.size()) {
            parametersAreEquals = false;
        } else {
            for (int i = 0; i < annotationParametersToAdd.size(); i++) {
                ClassAttributeValue classAnnotationParametersToAdd = (ClassAttributeValue) annotationParametersToAdd.get(i);
                ClassAttributeValue classAnnotationParameters = (ClassAttributeValue) annotationParameters.get(i);
                if (!classAnnotationParametersToAdd.getValue().getSimpleTypeName().equals(classAnnotationParameters.getValue().getSimpleTypeName())) {
                    parametersAreEquals = false;
                    break;
                }
            }
        }
        if (annotationMethodToAdd.equals(annotationMethod) && parametersAreEquals) {
            maintainAnnotation = false;
            break;
        }
    }
    return maintainAnnotation;
}
Also used : AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) ClassAttributeValue(org.springframework.roo.classpath.details.annotations.ClassAttributeValue) ArrayList(java.util.ArrayList) List(java.util.List) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)

Aggregations

ClassAttributeValue (org.springframework.roo.classpath.details.annotations.ClassAttributeValue)22 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)16 ArrayList (java.util.ArrayList)13 AnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue)13 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)12 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)10 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)10 JavaType (org.springframework.roo.model.JavaType)10 List (java.util.List)9 ArrayAttributeValue (org.springframework.roo.classpath.details.annotations.ArrayAttributeValue)9 NestedAnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)9 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)7 StringAttributeValue (org.springframework.roo.classpath.details.annotations.StringAttributeValue)6 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)5 EnumAttributeValue (org.springframework.roo.classpath.details.annotations.EnumAttributeValue)5 RooJavaType (org.springframework.roo.model.RooJavaType)5 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)4