Search in sources :

Example 6 with JavaSymbolName

use of org.springframework.roo.model.JavaSymbolName in project spring-roo by spring-projects.

the class PushInOperationsImpl method pushInClass.

/**
 * Makes push-in of all items defined on a provided class
 *
 * @param klass
 *            class to make the push-in operation
 * @param weiteOnDisk
 *            indicates if pushed elements should be writed on .java file
 * @param force
 *            if some operation will produce several changes, this parameter
 *            should be true.
 *
 * @return list of objects with all the pushed elements.
 */
public List<Object> pushInClass(JavaType klass, boolean writeOnDisk, boolean force) {
    List<Object> pushedElements = new ArrayList<Object>();
    // Check if current klass exists
    Validate.notNull(klass, "ERROR: You must specify a valid class to continue with push-in action");
    // Getting class details
    ClassOrInterfaceTypeDetails classDetails = getTypeLocationService().getTypeDetails(klass);
    Validate.notNull(klass, "ERROR: You must specify a valid class to continue with push-in action");
    // String builder where changes will be registered
    StringBuilder changesToApply = new StringBuilder();
    // Getting member details
    MemberDetails memberDetails = getMemberDetailsScanner().getMemberDetails(getClass().getName(), classDetails);
    List<MemberHoldingTypeDetails> memberHoldingTypes = memberDetails.getDetails();
    // Return if the class has not associated ITD's
    if (memberHoldingTypes.size() == 1 && memberHoldingTypes.get(0).getPhysicalTypeCategory() != PhysicalTypeCategory.ITD) {
        return pushedElements;
    }
    // Check if the provided class is a test to be able to select valid
    // class path
    Path path = classDetails.getAnnotation(RooJavaType.ROO_JPA_UNIT_TEST) == null ? Path.SRC_MAIN_JAVA : Path.SRC_TEST_JAVA;
    // Getting current class .java file metadata ID
    final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(klass, getPathResolver().getPath(klass.getModule(), path));
    // Getting detailsBuilder
    ClassOrInterfaceTypeDetailsBuilder detailsBuilder = new ClassOrInterfaceTypeDetailsBuilder(classDetails);
    // Getting all details
    for (final MemberHoldingTypeDetails memberHoldingTypeDetails : memberDetails.getDetails()) {
        // this .java file
        if (!memberHoldingTypeDetails.getType().equals(classDetails.getType())) {
            continue;
        }
        // Getting all declared methods (including declared on ITDs
        // and .java files)
        List<MethodMetadata> allDeclaredMethods = memberHoldingTypeDetails.getMethods();
        // Checking if is necessary to make push-in for all declared methods
        for (MethodMetadata method : allDeclaredMethods) {
            // If method exists on .aj file, add it!
            if (method.getDeclaredByMetadataId().split("\\?").length > 1 && method.getDeclaredByMetadataId().split("\\?")[1].equals(klass.getFullyQualifiedTypeName()) && !method.getDeclaredByMetadataId().equals(declaredByMetadataId)) {
                // Add method to .java file
                MethodMetadata newMethod = getNewMethod(declaredByMetadataId, method);
                detailsBuilder.addMethod(newMethod);
                // Save changes on pushed elements list
                pushedElements.add(newMethod);
                changesToApply.append(String.format("Method '%s' will be pushed on '%s.java' class. \n", method.getMethodName(), klass.getSimpleTypeName()));
            }
        }
        // Getting all declared fields (including declared on ITDs
        // and .java files)
        List<? extends FieldMetadata> allDeclaredFields = memberHoldingTypeDetails.getDeclaredFields();
        // Checking if is necessary to make push-in for all declared fields
        for (FieldMetadata field : allDeclaredFields) {
            // If field exists on .aj file, add it!
            if (field.getDeclaredByMetadataId().split("\\?").length > 1 && field.getDeclaredByMetadataId().split("\\?")[1].equals(klass.getFullyQualifiedTypeName()) && !field.getDeclaredByMetadataId().equals(declaredByMetadataId)) {
                // Add field to .java file
                FieldMetadata newField = getNewField(declaredByMetadataId, field);
                detailsBuilder.addField(newField);
                // Save changes on pushed elements list
                pushedElements.add(newField);
                changesToApply.append(String.format("Field '%s' will be pushed on '%s.java' class. \n", field.getFieldName(), klass.getSimpleTypeName()));
            }
        }
        // Getting all declared constructors (including declared on ITDs and
        // .java files)
        List<? extends ConstructorMetadata> allDeclaredConstructors = memberHoldingTypeDetails.getDeclaredConstructors();
        // constructors
        for (ConstructorMetadata constructor : allDeclaredConstructors) {
            // Check if current constructor exists on .java file
            classDetails = getTypeLocationService().getTypeDetails(detailsBuilder.build().getType());
            List<JavaType> parameterTypes = new ArrayList<JavaType>();
            for (AnnotatedJavaType type : constructor.getParameterTypes()) {
                parameterTypes.add(type.getJavaType());
            }
            ConstructorMetadata javaDeclaredConstructor = classDetails.getDeclaredConstructor(parameterTypes);
            // If not exists, add it!
            if (javaDeclaredConstructor == null) {
                // Add constructor to .java file
                detailsBuilder.addConstructor(constructor);
                // Save changes on pushed elements list
                pushedElements.add(constructor);
                String constructorParametersNames = "";
                for (JavaSymbolName paramName : constructor.getParameterNames()) {
                    constructorParametersNames = constructorParametersNames.concat(paramName.getSymbolName()).concat(", ");
                    changesToApply.append(String.format("Constructor with parameters '%s' will be pushed on '%s.java' class. \n", constructorParametersNames.substring(0, constructorParametersNames.length() - 2), klass.getSimpleTypeName()));
                }
            }
        }
        // Getting all declared annotations (including declared on ITDs
        // and .java files)
        List<AnnotationMetadata> allDeclaredAnnotations = memberHoldingTypeDetails.getAnnotations();
        for (AnnotationMetadata annotation : allDeclaredAnnotations) {
            // Check if current annotation exists on .java file
            classDetails = getTypeLocationService().getTypeDetails(detailsBuilder.build().getType());
            List<AnnotationMetadata> javaDeclaredAnnotations = classDetails.getAnnotations();
            boolean annotationExists = false;
            for (AnnotationMetadata javaAnnotation : javaDeclaredAnnotations) {
                if (javaAnnotation.getAnnotationType().getFullyQualifiedTypeName().equals(annotation.getAnnotationType().getFullyQualifiedTypeName())) {
                    annotationExists = true;
                }
            }
            // If not exists, add it!
            if (!annotationExists) {
                // Add annotation to .java file
                detailsBuilder.addAnnotation(annotation);
                // Save changes on pushed elements list
                pushedElements.add(annotation);
                changesToApply.append(String.format("Annotation '%s' will be pushed on '%s.java' class. \n", annotation.getAnnotationType().getSimpleTypeName(), klass.getSimpleTypeName()));
            }
        }
        // Getting all extends registered on .aj file to move to .java file
        List<JavaType> allExtendsTypes = memberHoldingTypeDetails.getExtendsTypes();
        for (JavaType extendsType : allExtendsTypes) {
            // If extends exists on .aj file, add it!
            if (!detailsBuilder.getExtendsTypes().contains(extendsType)) {
                detailsBuilder.addExtendsTypes(extendsType);
                // Save changes on pushed elements list
                pushedElements.add(extendsType);
                changesToApply.append(String.format("Extends type '%s' will be pushed on '%s.java' class. \n", extendsType.getSimpleTypeName(), klass.getSimpleTypeName()));
            }
        }
        // Getting all implements registered on .aj file to move to .java
        // file
        List<JavaType> allImplementsTypes = memberHoldingTypeDetails.getImplementsTypes();
        for (JavaType implementsType : allImplementsTypes) {
            if (!detailsBuilder.getImplementsTypes().contains(implementsType)) {
                detailsBuilder.addImplementsType(implementsType);
                // Save changes on pushed elements list
                pushedElements.add(implementsType);
                changesToApply.append(String.format("Implements type '%s' will be pushed on '%s.java' class. \n", implementsType.getSimpleTypeName(), klass.getSimpleTypeName()));
            }
        }
        // Getting all imports registered on .aj file to move to .java file
        Set<ImportMetadata> allRegisteredImports = memberHoldingTypeDetails.getImports();
        detailsBuilder.addImports(allRegisteredImports);
        // Save changes on pushed elements list
        pushedElements.add(allRegisteredImports);
    }
    // Updating .java file
    if (!force) {
        // Show message to be able to know which changes will be applied
        if (changesToApply.length() > 0) {
            LOGGER.log(Level.INFO, changesToApply.toString());
        }
    } else if (writeOnDisk) {
        getTypeManagementService().createOrUpdateTypeOnDisk(detailsBuilder.build());
    }
    return pushedElements;
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ArrayList(java.util.ArrayList) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) Path(org.springframework.roo.project.Path) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ConstructorMetadata(org.springframework.roo.classpath.details.ConstructorMetadata) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) MemberHoldingTypeDetails(org.springframework.roo.classpath.details.MemberHoldingTypeDetails) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) ImportMetadata(org.springframework.roo.classpath.details.ImportMetadata)

Example 7 with JavaSymbolName

use of org.springframework.roo.model.JavaSymbolName in project spring-roo by spring-projects.

the class SecurityOperationsImpl method getRooSecurityAuthorizationsAnnotation.

/**
 * This method provides {@link RooSecurityAuthorization} 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
 * @return the annotation created
 */
private AnnotationMetadataBuilder getRooSecurityAuthorizationsAnnotation(final String method, final List<AnnotationAttributeValue<?>> lstParamTypes, final String roles, final String usernames) {
    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));
    }
    return new AnnotationMetadataBuilder(RooJavaType.ROO_SECURITY_AUTHORIZATION, 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)

Example 8 with JavaSymbolName

use of org.springframework.roo.model.JavaSymbolName in project spring-roo by spring-projects.

the class JpaDataOnDemandConfigurationMetadata method getDodTypeBeanCreationMethod.

/**
 * Builds and returns a method used to instance a DataOnDemand class using
 * {@link EntityManager} and `@Bean` annotation.
 *
 * @param dodType
 *            the class to inject in the Spring context.
 * @return the MethodMetadata to add to ITD.
 */
private MethodMetadata getDodTypeBeanCreationMethod(JavaType dodType) {
    // Define methodName
    final JavaSymbolName methodName = new JavaSymbolName(StringUtils.uncapitalize(dodType.getSimpleTypeName()));
    // Check if method exists
    MethodMetadata existingMethod = getGovernorMethod(methodName);
    if (existingMethod != null) {
        return existingMethod;
    }
    // Add body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    bodyBuilder.appendFormalLine("return new %s(%s());", getNameOfJavaType(dodType), getAccessorMethod(getEntityManagerField().build()).getMethodName());
    // Create method
    MethodMetadataBuilder method = new MethodMetadataBuilder(this.getId(), Modifier.PUBLIC, methodName, dodType, bodyBuilder);
    // Add annotation
    method.addAnnotation(new AnnotationMetadataBuilder(SpringJavaType.BEAN));
    return method.build();
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 9 with JavaSymbolName

use of org.springframework.roo.model.JavaSymbolName in project spring-roo by spring-projects.

the class IdentifierMetadata method getParameterizedConstructor.

/**
 * Locates the parameterised constructor consisting of the id fields for
 * this class.
 *
 * @param fields
 * @return the constructor, never null.
 */
private ConstructorMetadataBuilder getParameterizedConstructor(final List<FieldMetadataBuilder> fields) {
    // Search for an existing constructor
    final List<JavaType> parameterTypes = new ArrayList<JavaType>();
    for (final FieldMetadataBuilder field : fields) {
        parameterTypes.add(field.getFieldType());
    }
    final ConstructorMetadata result = governorTypeDetails.getDeclaredConstructor(parameterTypes);
    if (result != null) {
        // Found an existing parameterised constructor on this class
        publicNoArgConstructor = true;
        return null;
    }
    final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    bodyBuilder.appendFormalLine("super();");
    for (final FieldMetadataBuilder field : fields) {
        final String fieldName = field.getFieldName().getSymbolName();
        bodyBuilder.appendFormalLine("this." + fieldName + " = " + fieldName + ";");
        parameterNames.add(field.getFieldName());
    }
    // Create the constructor
    final ConstructorMetadataBuilder constructorBuilder = new ConstructorMetadataBuilder(getId());
    constructorBuilder.setModifier(Modifier.PUBLIC);
    constructorBuilder.setParameterTypes(AnnotatedJavaType.convertFromJavaTypes(parameterTypes));
    constructorBuilder.setParameterNames(parameterNames);
    constructorBuilder.setBodyBuilder(bodyBuilder);
    return constructorBuilder;
}
Also used : AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) JavaType(org.springframework.roo.model.JavaType) ConstructorMetadata(org.springframework.roo.classpath.details.ConstructorMetadata) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ConstructorMetadataBuilder(org.springframework.roo.classpath.details.ConstructorMetadataBuilder) ArrayList(java.util.ArrayList) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 10 with JavaSymbolName

use of org.springframework.roo.model.JavaSymbolName in project spring-roo by spring-projects.

the class IdentifierMetadata method setDateAnnotations.

private void setDateAnnotations(final String columnDefinition, final List<AnnotationMetadataBuilder> annotations) {
    // Add JSR 220 @Temporal annotation to date fields
    String temporalType = StringUtils.defaultIfEmpty(StringUtils.upperCase(columnDefinition), "DATE");
    if ("DATETIME".equals(temporalType)) {
        // ROO-2606
        temporalType = "TIMESTAMP";
    }
    final AnnotationMetadataBuilder temporalBuilder = new AnnotationMetadataBuilder(TEMPORAL);
    temporalBuilder.addEnumAttribute("value", new EnumDetails(TEMPORAL_TYPE, new JavaSymbolName(temporalType)));
    annotations.add(temporalBuilder);
    final AnnotationMetadataBuilder dateTimeFormatBuilder = new AnnotationMetadataBuilder(DATE_TIME_FORMAT);
    dateTimeFormatBuilder.addStringAttribute("style", "M-");
    annotations.add(dateTimeFormatBuilder);
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) EnumDetails(org.springframework.roo.model.EnumDetails) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Aggregations

JavaSymbolName (org.springframework.roo.model.JavaSymbolName)317 ArrayList (java.util.ArrayList)186 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)155 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)143 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)142 JavaType (org.springframework.roo.model.JavaType)133 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)121 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)119 SpringJavaType (org.springframework.roo.model.SpringJavaType)61 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)59 JdkJavaType (org.springframework.roo.model.JdkJavaType)59 FieldMetadataBuilder (org.springframework.roo.classpath.details.FieldMetadataBuilder)48 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)47 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)45 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)39 AnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue)36 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)29 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)29 StringAttributeValue (org.springframework.roo.classpath.details.annotations.StringAttributeValue)27 EnumDetails (org.springframework.roo.model.EnumDetails)25