Search in sources :

Example 6 with InvocableMemberBodyBuilder

use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.

the class ExceptionsMetadata method getLoggerField.

public FieldMetadata getLoggerField() {
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    bodyBuilder.append(getNameOfJavaType(LOGGER_FACTORY_JAVA_TYPE));
    bodyBuilder.append(".getLogger(");
    bodyBuilder.append(governorPhysicalTypeMetadata.getType().getSimpleTypeName());
    bodyBuilder.append(".class)");
    final String initializer = bodyBuilder.getOutput();
    FieldMetadataBuilder field = new FieldMetadataBuilder(getId(), Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL, new JavaSymbolName("LOG"), LOGGER_JAVA_TYPE, initializer);
    return field.build();
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 7 with InvocableMemberBodyBuilder

use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder 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 8 with InvocableMemberBodyBuilder

use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder 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 InvocableMemberBodyBuilder

use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder 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 InvocableMemberBodyBuilder

use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.

the class JpaUnitTestMetadata method getRemoveFromRelationMethod.

/**
 * Creates a method to test item removes from a relationship.
 *
 * @param removeMethod the {@link MethodMetadata} to test.
 * @param relationFieldName a {@link String} with the field name
 *            representing the relation on the parent entity.
 * @param childEntity the {@link JavaType} of child entity.
 * @param field the {@link FieldMetadata} of relation field.
 * @return the {@link MethodMetadata}
 */
private MethodMetadata getRemoveFromRelationMethod(RelationInfo relationInfo) {
    // Initialize local variables
    String relationFieldName = relationInfo.fieldName;
    JavaType childEntity = relationInfo.childType;
    FieldMetadata relationField = relationInfo.fieldMetadata;
    // Create test method name using target method name, child entity name and relation field
    JavaSymbolName methodName = new JavaSymbolName(String.format("%sShouldRemoveThe%sFromThe%sRelationship", relationInfo.addMethod.getMethodName().getSymbolName(), childEntity.getSimpleTypeName(), relationFieldName));
    MethodMetadata method = getGovernorMethod(methodName);
    if (method != null) {
        return method;
    }
    // Create body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // Setup
    bodyBuilder.appendFormalLine("// Setup");
    // Entity entity = entityFactory.create(0);
    FieldMetadata entityFactory = entityFactories.get(this.entityFactory);
    bodyBuilder.appendFormalLine("%s %s = %s().create(0);", getNameOfJavaType(this.entity), this.entityVar, getAccessorMethod(entityFactory).getMethodName());
    // ChildEntity childEntity1 = childEntityFactory.create(0);
    FieldMetadata childEntityFactory = entityFactories.get(this.entityAndItsFactoryMap.get(childEntity));
    Validate.notNull(childEntityFactory, "Unable to locate the entity factory of %s in JpaUnitTestMetadata", childEntity.getSimpleTypeName());
    String childEntityVar1 = String.format("%s1", StringUtils.uncapitalize(childEntity.getSimpleTypeName()));
    bodyBuilder.appendFormalLine("%s %s = %s().create(0);", getNameOfJavaType(childEntity), childEntityVar1, getAccessorMethod(childEntityFactory).getMethodName());
    // ChildEntity childEntity2 = childEntityFactory.create(1);
    String childEntityVar2 = String.format("%s2", StringUtils.uncapitalize(childEntity.getSimpleTypeName()));
    bodyBuilder.appendFormalLine("%s %s = %s().create(1);", getNameOfJavaType(childEntity), childEntityVar2, getAccessorMethod(childEntityFactory).getMethodName());
    // entity.ADD_METHOD(Arrays.asList(childEntity1, childEntity2));
    bodyBuilder.appendFormalLine("%s.%s(%s.asList(%s, %s));", entityVar, relationInfo.addMethod.getMethodName(), getNameOfJavaType(JavaType.ARRAYS), childEntityVar1, childEntityVar2);
    bodyBuilder.newLine();
    // Exercise
    bodyBuilder.appendFormalLine("// Exercise");
    // entity.REMOVE_METHOD(Collections.singleton(childEntity1));
    bodyBuilder.appendFormalLine("%s.%s(%s.singleton(%s));", this.entityVar, relationInfo.removeMethod.getMethodName(), getNameOfJavaType(JavaType.COLLECTIONS), childEntityVar1);
    bodyBuilder.newLine();
    // Verify
    bodyBuilder.appendFormalLine("// Verify");
    // assertThat(childEntity1.PARENT_ENTITY_ACCESSOR()).as("Check 'REMOVE_METHOD' updates the CHILD_ENTITY relationship side")
    JavaSymbolName parentEntityAccessor = BeanInfoUtils.getAccessorMethodName(new JavaSymbolName(relationInfo.mappedBy), this.entity);
    bodyBuilder.appendFormalLine("%s(%s.%s()).as(\"Check '%s' updates the %s relationship side\")", getNameOfJavaType(new JavaType("org.assertj.core.api.Assertions.assertThat"), true), childEntityVar1, parentEntityAccessor, relationInfo.removeMethod.getMethodName(), getNameOfJavaType(childEntity));
    // .isNull();
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine(".isNull();");
    bodyBuilder.indentRemove();
    // assertThat(entity.RELATION_FIELD_ACCESSOR()).as("Check 'REMOVE_METHOD' removes a CHILD_ENTITY from the relationship")
    JavaSymbolName relationFieldAccessorName = BeanInfoUtils.getAccessorMethodName(relationField);
    bodyBuilder.appendFormalLine("%s(%s.%s()).as(\"Check '%s' removes a %s from the relationship\")", getNameOfJavaType(new JavaType("org.assertj.core.api.Assertions.assertThat"), true), this.entityVar, relationFieldAccessorName, relationInfo.removeMethod.getMethodName(), getNameOfJavaType(childEntity));
    // .doesNotContain(childEntity1).contains(childEntity2);
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine(".doesNotContain(%s).contains(%s);", childEntityVar1, childEntityVar2);
    bodyBuilder.reset();
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(this.getId(), Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, bodyBuilder);
    // Add @Test
    methodBuilder.addAnnotation(new AnnotationMetadataBuilder(TEST));
    // Add throws types
    methodBuilder.addThrowsType(JdkJavaType.EXCEPTION);
    return methodBuilder.build();
}
Also used : JdkJavaType(org.springframework.roo.model.JdkJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) 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

InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)152 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)121 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)117 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)105 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)92 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)90 ArrayList (java.util.ArrayList)85 JavaType (org.springframework.roo.model.JavaType)70 SpringJavaType (org.springframework.roo.model.SpringJavaType)46 JdkJavaType (org.springframework.roo.model.JdkJavaType)44 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)40 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)34 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)27 CommentStructure (org.springframework.roo.classpath.details.comments.CommentStructure)16 JavadocComment (org.springframework.roo.classpath.details.comments.JavadocComment)16 ServiceMetadata (org.springframework.roo.addon.layers.service.addon.ServiceMetadata)15 ConstructorMetadataBuilder (org.springframework.roo.classpath.details.ConstructorMetadataBuilder)15 RelationInfo (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo)14 RelationInfoExtended (org.springframework.roo.addon.web.mvc.controller.addon.RelationInfoExtended)11 JpaJavaType (org.springframework.roo.model.JpaJavaType)9