Search in sources :

Example 56 with MethodMetadata

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

the class RepositoryJpaMetadata method getFindOneMethod.

private MethodMetadata getFindOneMethod(JavaType entity, FieldMetadata identifierFieldMetadata) {
    // Define method parameter type and name
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    parameterTypes.add(AnnotatedJavaType.convertFromJavaType(identifierFieldMetadata.getFieldType()));
    parameterNames.add(identifierFieldMetadata.getFieldName());
    MethodMetadata existingMethod = getGovernorMethod(FIND_ONE_METHOD_NAME, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
    if (existingMethod != null) {
        return existingMethod;
    }
    // Use the MethodMetadataBuilder for easy creation of MethodMetadata
    return new MethodMetadataBuilder(getId(), Modifier.PUBLIC + Modifier.ABSTRACT, FIND_ONE_METHOD_NAME, entity, parameterTypes, parameterNames, null).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)

Example 57 with MethodMetadata

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

the class RepositoryJpaIntegrationTestMetadata method getFindAllCustomNotFilteredPagedTestMethod.

/**
 * Builds a method to test the find all custom method not filtered and paged.
 *
 * @return {@link MethodMetadata}
 */
private MethodMetadata getFindAllCustomNotFilteredPagedTestMethod() {
    JavaSymbolName methodName = new JavaSymbolName(String.format("findAllCustomNotFilteredPagedShouldReturnA%sPage", this.entityPlural));
    // Check if method exists on governor
    MethodMetadata method = getGovernorMethod(methodName);
    if (method != null) {
        return method;
    }
    // Build method body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // Exercise
    bodyBuilder.appendFormalLine("// Exercise");
    // Page<Pet> all = repository.findAll((GlobalSearch) null, new PageRequest(0, 3));
    bodyBuilder.appendFormalLine("%s<%s> all = %s().findAll((%s) null, new %s(0, 3));", getNameOfJavaType(SpringJavaType.PAGE), getNameOfJavaType(this.defaultReturnType), getAccessorMethod(this.repositoryField).getMethodName(), getNameOfJavaType(SpringletsJavaType.SPRINGLETS_GLOBAL_SEARCH), getNameOfJavaType(SpringJavaType.PAGE_REQUEST));
    // Verify
    bodyBuilder.newLine();
    bodyBuilder.appendFormalLine("// Verify");
    // assertThat(all.getNumberOfElements())
    bodyBuilder.appendFormalLine("%s(all.getNumberOfElements())", getNameOfJavaType(ASSERT_THAT));
    // .as("Check result number is not greater than the page size").isLessThanOrEqualTo(3);
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine(".as(\"Check result number is not greater than the page size\").isLessThanOrEqualTo(3);");
    bodyBuilder.indentRemove();
    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 58 with MethodMetadata

use of org.springframework.roo.classpath.details.MethodMetadata 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 59 with MethodMetadata

use of org.springframework.roo.classpath.details.MethodMetadata 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 60 with MethodMetadata

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

Aggregations

MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)149 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)119 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)115 ArrayList (java.util.ArrayList)111 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)110 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)92 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)73 JavaType (org.springframework.roo.model.JavaType)72 SpringJavaType (org.springframework.roo.model.SpringJavaType)40 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)39 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)38 JdkJavaType (org.springframework.roo.model.JdkJavaType)28 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)27 RooJavaType (org.springframework.roo.model.RooJavaType)22 ServiceMetadata (org.springframework.roo.addon.layers.service.addon.ServiceMetadata)18 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)18 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)17 RelationInfo (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo)16 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)16 JpaJavaType (org.springframework.roo.model.JpaJavaType)15