Search in sources :

Example 6 with AnnotationMetadataBuilder

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

the class ExceptionsOperationsImpl method addHandlersAnnotations.

/**
 * Generates {@link RooExceptionHandlers} and {@link RooExceptionHandler} annotations
 * and adds or updates it on specified class.
 *
 * @param exception
 * @param targetClass
 * @param errorView
 */
private void addHandlersAnnotations(JavaType exception, JavaType targetClass, String errorView) {
    Validate.notNull(targetClass, "Target class is required to add @RooExceptionHandlers annotation");
    // Create @RooExceptionHandler Annotation
    final AnnotationMetadataBuilder exceptionHandlerAnnotationBuilder = new AnnotationMetadataBuilder(RooJavaType.ROO_EXCEPTION_HANDLER);
    final List<AnnotationAttributeValue<?>> exceptionHandlerAnnotationAttributes = new ArrayList<AnnotationAttributeValue<?>>();
    exceptionHandlerAnnotationAttributes.add(new ClassAttributeValue(new JavaSymbolName(EXCEPTION), exception));
    if (errorView != null) {
        exceptionHandlerAnnotationAttributes.add(new StringAttributeValue(new JavaSymbolName(ERROR_VIEW), errorView));
    }
    exceptionHandlerAnnotationBuilder.setAttributes(exceptionHandlerAnnotationAttributes);
    // Check if container annotation already exists
    ClassOrInterfaceTypeDetails typeDetails = typeLocationService.getTypeDetails(targetClass);
    ClassOrInterfaceTypeDetailsBuilder typeDetailsBuilder = new ClassOrInterfaceTypeDetailsBuilder(typeDetails);
    AnnotationMetadata exceptionHandlersAnnotation = typeDetails.getAnnotation(RooJavaType.ROO_EXCEPTION_HANDLERS);
    AnnotationMetadataBuilder exceptionHandlersAnnotationBuilder = null;
    if (exceptionHandlersAnnotation != null) {
        exceptionHandlersAnnotationBuilder = new AnnotationMetadataBuilder(exceptionHandlersAnnotation);
    } else {
        exceptionHandlersAnnotationBuilder = new AnnotationMetadataBuilder(RooJavaType.ROO_EXCEPTION_HANDLERS);
    }
    Validate.notNull(exceptionHandlersAnnotationBuilder);
    // Add @RooExceptionHandler annotation into @RooExceptionHandlers
    final List<NestedAnnotationAttributeValue> exceptionHandlersArrayValues = new ArrayList<NestedAnnotationAttributeValue>();
    exceptionHandlersArrayValues.add(new NestedAnnotationAttributeValue(new JavaSymbolName(VALUE), exceptionHandlerAnnotationBuilder.build()));
    final List<AnnotationAttributeValue<?>> attributeValues = new ArrayList<AnnotationAttributeValue<?>>();
    attributeValues.add(new ArrayAttributeValue<NestedAnnotationAttributeValue>(new JavaSymbolName(VALUE), exceptionHandlersArrayValues));
    if (exceptionHandlersAnnotation == null) {
        // Add new @RooExceptionHandlers annotation with given values
        exceptionHandlersAnnotationBuilder.setAttributes(attributeValues);
        typeDetailsBuilder.addAnnotation(exceptionHandlersAnnotationBuilder.build());
    } else {
        // Get current annotation values from @RooExceptionHandlers annotation
        AnnotationAttributeValue<?> currentHandlers = exceptionHandlersAnnotation.getAttribute(VALUE);
        if (currentHandlers != null) {
            List<?> values = (List<?>) currentHandlers.getValue();
            Iterator<?> it = values.iterator();
            while (it.hasNext()) {
                NestedAnnotationAttributeValue handler = (NestedAnnotationAttributeValue) it.next();
                if (handler.getValue() != null) {
                    // Check if there is a @RooExceptionHandlers with same 'exception' value
                    if (exceptionHandlerAnnotationBuilder.build().getAttribute(EXCEPTION).getValue().equals(handler.getValue().getAttribute(EXCEPTION).getValue())) {
                        LOGGER.warning(String.format("There is already a handler for exception %s in class %s", exception.getSimpleTypeName(), targetClass.getSimpleTypeName()));
                        return;
                    }
                    exceptionHandlersArrayValues.add(handler);
                }
            }
        }
        // Add found values
        attributeValues.add(new ArrayAttributeValue<NestedAnnotationAttributeValue>(new JavaSymbolName(VALUE), exceptionHandlersArrayValues));
        exceptionHandlersAnnotationBuilder.setAttributes(attributeValues);
        // Update annotation
        typeDetailsBuilder.updateTypeAnnotation(exceptionHandlersAnnotationBuilder.build());
    }
    // Write to disk
    getTypeManagementService().createOrUpdateTypeOnDisk(typeDetailsBuilder.build());
}
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) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ArrayList(java.util.ArrayList) List(java.util.List) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) 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 7 with AnnotationMetadataBuilder

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

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

the class JsonControllerTestCreator method createIntegrationTest.

@Override
public void createIntegrationTest(JavaType type, Pom module) {
    Validate.notNull(type, "Class to produce an integration test class for is required");
    // Check if provided JavaType is a JSON Controller
    ClassOrInterfaceTypeDetails cid = typeLocationService.getTypeDetails(type);
    Validate.notNull(cid.getAnnotation(RooJavaType.ROO_CONTROLLER), "Type must be a Roo controller.");
    Validate.notNull(cid.getAnnotation(RooJavaType.ROO_JSON), "Type must be a Roo JSON controller.");
    // Add springlets-boot-starter-test dependency
    projectOperations.addProperty("", SPRINGLETS_VERSION_PROPERTY);
    projectOperations.addDependency(module.getModuleName(), SPRINGLETS_BOOT_STARTER_TEST_DEPENDENCY);
    // Get the controller managed entity
    ControllerAnnotationValues controllerAnnotationValues = new ControllerAnnotationValues(cid);
    JavaType managedEntity = controllerAnnotationValues.getEntity();
    // Workaround to get a JavaType with not null module when recovering it
    // from a ClassAttributeValue
    managedEntity = new JavaType(managedEntity.getFullyQualifiedTypeName(), managedEntity.getArray(), managedEntity.getDataType(), managedEntity.getArgName(), managedEntity.getParameters(), typeLocationService.getTypeDetails(managedEntity).getType().getModule());
    // Create Data On Demand artifacts for managed entity
    List<DataOnDemandCreatorProvider> dodCreators = getValidDataOnDemandCreatorsForType(managedEntity);
    Validate.isTrue(!dodCreators.isEmpty(), "Couldn't find any 'DataOnDemandCreatorProvider' for JSON controllers.");
    Validate.isTrue(dodCreators.size() == 1, "More than 1 valid 'DataOnDemandCreatorProvider' found for JSON controllers. %s can't decide which one to use.", this.getClass().getName());
    DataOnDemandCreatorProvider creator = dodCreators.get(0);
    creator.createDataOnDemand(managedEntity);
    // Add module dependency with test-jar dependency
    if (projectOperations.isMultimoduleProject()) {
        String managedEntityModuleName = managedEntity.getModule();
        Pom managedEntityModule = projectOperations.getPomFromModuleName(managedEntityModuleName);
        projectOperations.addDependency(module.getModuleName(), new Dependency(managedEntityModule.getGroupId(), managedEntityModule.getArtifactId(), "${project.version}", DependencyType.valueOfTypeCode("test-jar"), DependencyScope.TEST), true, true);
    }
    // Create integration test class
    final JavaType name = new JavaType(type + "IT", module.getModuleName());
    final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(name, Path.SRC_TEST_JAVA.getModulePathId(module.getModuleName()));
    if (metadataService.get(declaredByMetadataId) != null) {
        // The file already exists
        return;
    }
    // Add @RooJsonControllerIntegrationTest to source file
    AnnotationMetadataBuilder rooIntegrationTestAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_JSON_CONTROLLER_INTEGRATION_TEST);
    rooIntegrationTestAnnotation.addClassAttribute("targetClass", type);
    // Create integration test class
    final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataId, Modifier.PUBLIC, name, PhysicalTypeCategory.CLASS);
    cidBuilder.addAnnotation(rooIntegrationTestAnnotation);
    // Write changes to disk
    typeManagementService.createOrUpdateTypeOnDisk(cidBuilder.build());
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) ControllerAnnotationValues(org.springframework.roo.addon.web.mvc.controller.addon.ControllerAnnotationValues) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) Dependency(org.springframework.roo.project.Dependency) DataOnDemandCreatorProvider(org.springframework.roo.addon.test.providers.DataOnDemandCreatorProvider) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) Pom(org.springframework.roo.project.maven.Pom)

Example 9 with AnnotationMetadataBuilder

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

the class JpaDataOnDemandConfigurationMetadata method getConstructor.

/**
 * Builds and returns a class constructor which injects
 * {@link EntityManager} field.
 *
 * @return a ConstructorMetadataBuilder to add to ITD.
 */
private ConstructorMetadataBuilder getConstructor() {
    // Create instance and add parameters
    ConstructorMetadataBuilder constructorBuilder = new ConstructorMetadataBuilder(this.getId());
    constructorBuilder.addParameter(ENTITY_MANAGER_FIELD_NAME, JpaJavaType.ENTITY_MANAGER);
    // Add body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    bodyBuilder.appendFormalLine("%1$s(%2$s);", getMutatorMethod(getEntityManagerField().build()).getMethodName(), ENTITY_MANAGER_FIELD_NAME);
    constructorBuilder.setBodyBuilder(bodyBuilder);
    constructorBuilder.setModifier(Modifier.PUBLIC);
    // Add @Autowired
    constructorBuilder.addAnnotation(new AnnotationMetadataBuilder(SpringJavaType.AUTOWIRED));
    return constructorBuilder;
}
Also used : ConstructorMetadataBuilder(org.springframework.roo.classpath.details.ConstructorMetadataBuilder) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 10 with AnnotationMetadataBuilder

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

Aggregations

AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)232 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)155 ArrayList (java.util.ArrayList)129 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)90 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)87 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)81 JavaType (org.springframework.roo.model.JavaType)78 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)73 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)54 SpringJavaType (org.springframework.roo.model.SpringJavaType)47 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)42 FieldMetadataBuilder (org.springframework.roo.classpath.details.FieldMetadataBuilder)40 JdkJavaType (org.springframework.roo.model.JdkJavaType)39 AnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue)31 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)30 RooJavaType (org.springframework.roo.model.RooJavaType)28 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)28 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)26 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)25 StringAttributeValue (org.springframework.roo.classpath.details.annotations.StringAttributeValue)23