Search in sources :

Example 61 with MethodMetadataBuilder

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

the class RepositoryJpaIntegrationTestMetadata method getCountTestMethod.

/**
 * Builds a method to test count method.
 *
 * @return {@link MethodMetadata}
 */
private MethodMetadata getCountTestMethod() {
    MethodMetadata method = getGovernorMethod(COUNT_TEST_METHOD_NAME);
    if (method != null) {
        return method;
    }
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // Verify
    bodyBuilder.appendFormalLine("// Verify");
    // assertThat(repository.count()).as("Check there are available 'Pet' entries").isGreaterThan(0);
    bodyBuilder.appendFormalLine("%s(%s().count()).as(\"Check there are available '%s' entries\").isGreaterThan(0);", getNameOfJavaType(ASSERT_THAT), getAccessorMethod(this.repositoryField).getMethodName(), getNameOfJavaType(this.entity));
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, COUNT_TEST_METHOD_NAME, JavaType.VOID_PRIMITIVE, bodyBuilder);
    // Add @Test
    methodBuilder.addAnnotation(new AnnotationMetadataBuilder(TEST));
    return methodBuilder.build();
}
Also used : 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 62 with MethodMetadataBuilder

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

the class RepositoryJpaIntegrationTestMetadata method getFindOneTestMethod.

/**
 * Builds a method to test find one method.
 *
 * @return {@link MethodMetadata}
 */
private MethodMetadata getFindOneTestMethod() {
    final JavaSymbolName methodName = new JavaSymbolName(String.format("findOneShouldReturnExisting%s", this.entity.getSimpleTypeName()));
    MethodMetadata method = getGovernorMethod(methodName);
    if (method != null) {
        return method;
    }
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // Setup
    bodyBuilder.appendFormalLine("// Setup");
    // Long id = getRandomPetId();
    bodyBuilder.appendFormalLine("%s id = %s();", getNameOfJavaType(this.identifierType), this.getRandomIdMethodName);
    // Exercise
    bodyBuilder.newLine();
    bodyBuilder.appendFormalLine("// Exercise");
    // Pet pet = repository.findOne(id);
    bodyBuilder.appendFormalLine("%s %s = %s().findOne(id);", getNameOfJavaType(this.entity), entityVar, getAccessorMethod(this.repositoryField).getMethodName());
    // Verify
    bodyBuilder.newLine();
    bodyBuilder.appendFormalLine("// Verify");
    // assertThat(pet).as("Check that findOne illegally returned null for id %s", id).isNotNull();
    bodyBuilder.appendFormalLine("%s(%s).as(\"Check that findOne illegally returned null for id %s\", id).isNotNull();", getNameOfJavaType(ASSERT_THAT), this.entityVar, "%s");
    // assertThat(id).as("Check the identifier of the found 'Pet' is the same used to look for it")
    bodyBuilder.appendFormalLine("%s(id).as(\"Check the identifier of the found '%s' is the same used to look for it\")", getNameOfJavaType(ASSERT_THAT), getNameOfJavaType(this.entity));
    // .isEqualTo(pet.getId());
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine(".isEqualTo(%s.%s());", this.entityVar, this.identifierAccessorMethodName);
    bodyBuilder.reset();
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, bodyBuilder);
    // Add @Test
    methodBuilder.addAnnotation(new AnnotationMetadataBuilder(TEST));
    return methodBuilder.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 63 with MethodMetadataBuilder

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

the class ServiceImplMetadata method getIdentifierTypeGetterMethod.

/**
 * Builds a method which returns the class of the entity identifier JavaType.
 *
 * @return MethodMetadataBuilder
 */
private MethodMetadata getIdentifierTypeGetterMethod() {
    List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    MethodMetadata existingMethod = getGovernorMethod(GET_ID_TYPE_METHOD_NAME, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
    if (existingMethod != null) {
        return existingMethod;
    }
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // return IDENTIFIER_TYPE.class;
    bodyBuilder.appendFormalLine("return %s.class;", getNameOfJavaType(this.entityIdentifierType));
    // Use the MethodMetadataBuilder for easy creation of MethodMetadata
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, GET_ID_TYPE_METHOD_NAME, JavaType.wrapperOf(JavaType.CLASS, this.serviceMetadata.getIdType()), parameterTypes, parameterNames, bodyBuilder);
    // Build and return a MethodMetadata instance
    return methodBuilder.build();
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)

Example 64 with MethodMetadataBuilder

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

the class ServiceImplMetadata method getMethod.

/**
 * Method that generates implementation of provided method with specified
 * body
 *
 * @param methodToBeImplemented
 * @param isTransactional
 * @param bodyBuilder
 * @return
 */
private MethodMetadata getMethod(final MethodMetadata methodToBeImplemented, boolean isTransactional, InvocableMemberBodyBuilder bodyBuilder) {
    List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>(methodToBeImplemented.getParameterNames());
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>(methodToBeImplemented.getParameterTypes());
    MethodMetadata existingMethod = getGovernorMethod(methodToBeImplemented.getMethodName(), AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
    if (existingMethod != null) {
        return existingMethod;
    }
    // Use the MethodMetadataBuilder for easy creation of MethodMetadata
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodToBeImplemented.getMethodName(), methodToBeImplemented.getReturnType(), parameterTypes, parameterNames, bodyBuilder);
    // Adding @Transactional
    if (isTransactional) {
        AnnotationMetadataBuilder transactionalAnnotation = new AnnotationMetadataBuilder(SpringJavaType.TRANSACTIONAL);
        methodBuilder.addAnnotation(transactionalAnnotation);
    }
    // Build and return a MethodMetadata instance
    return methodBuilder.build();
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 65 with MethodMetadataBuilder

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

the class ServiceMetadata method getFindAllMethod.

/**
 * Method that generates method "findAll" method.
 *
 * @return MethodMetadataBuilder with public List <Entity> findAll();
 *         structure
 */
private MethodMetadata getFindAllMethod() {
    // Define method name
    JavaSymbolName methodName = new JavaSymbolName("findAll");
    // Define method parameter types
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    // Define method parameter names
    List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
    if (existingMethod != null) {
        return existingMethod;
    }
    JavaType listEntityJavaType = JavaType.listOf(entity);
    // Use the MethodMetadataBuilder for easy creation of MethodMetadata
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC + Modifier.ABSTRACT, methodName, listEntityJavaType, parameterTypes, parameterNames, null);
    // Build and return a MethodMetadata
    return methodBuilder.build();
// instance
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) SpringletsJavaType(org.springframework.roo.model.SpringletsJavaType) JavaType(org.springframework.roo.model.JavaType) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata)

Aggregations

MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)155 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)143 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)120 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)117 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)115 ArrayList (java.util.ArrayList)110 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)87 JavaType (org.springframework.roo.model.JavaType)65 SpringJavaType (org.springframework.roo.model.SpringJavaType)48 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)43 JdkJavaType (org.springframework.roo.model.JdkJavaType)34 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)30 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)27 ServiceMetadata (org.springframework.roo.addon.layers.service.addon.ServiceMetadata)15 CommentStructure (org.springframework.roo.classpath.details.comments.CommentStructure)14 JavadocComment (org.springframework.roo.classpath.details.comments.JavadocComment)14 RelationInfo (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo)12 RelationInfoExtended (org.springframework.roo.addon.web.mvc.controller.addon.RelationInfoExtended)11 JpaJavaType (org.springframework.roo.model.JpaJavaType)9 JpaEntityMetadata (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata)7