Search in sources :

Example 91 with MethodMetadata

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

the class RepositoryJpaIntegrationTestMetadata method getRandomIdMethod.

/**
 * Builds a method to obtain the id value of a random generated entity
 * instance generated with data-on-demand.
 *
 * @return {@link MethodMetadata}
 */
private MethodMetadata getRandomIdMethod() {
    MethodMetadata method = getGovernorMethod(this.getRandomIdMethodName);
    if (method != null) {
        return method;
    }
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // Pet pet = dod.getRandomPet();
    bodyBuilder.appendFormalLine("%s %s = %s().%s();", getNameOfJavaType(this.entity), this.entityVar, getAccessorMethod(this.dodField).getMethodName(), this.getRandomMethodName);
    // Long id = pet.getId();
    bodyBuilder.appendFormalLine("%s id = %s.%s();", getNameOfJavaType(this.identifierType), this.entityVar, this.identifierAccessorMethodName);
    // assertThat(id).as("Check the Data on demand generated a 'Pet' with an identifier").isNotNull();
    bodyBuilder.appendFormalLine("%s(id).as(\"Check the Data on demand generated a '%s' with an identifier\").isNotNull();", getNameOfJavaType(ASSERT_THAT), getNameOfJavaType(this.entity));
    // return id;
    bodyBuilder.appendFormalLine("return id;");
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PRIVATE, this.getRandomIdMethodName, this.identifierType, bodyBuilder);
    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)

Example 92 with MethodMetadata

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

the class RepositoryJpaIntegrationTestMetadata method getBeforeMethod.

/**
 * Builds a method annotated with `@Before` which checks if data-on-demand
 * field has initialized correctly.
 *
 * @return {@link MethodMetadata}
 */
private MethodMetadata getBeforeMethod() {
    MethodMetadata method = getGovernorMethod(BEFORE_METHOD_NAME);
    if (method != null) {
        return method;
    }
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // assertThat(dod.getRandomPet())
    bodyBuilder.appendFormalLine("%s(%s().%s())", getNameOfJavaType(ASSERT_THAT, true), getAccessorMethod(this.dodField).getMethodName(), this.getRandomMethodName);
    // .as("Check data on demand for 'Pet' initializes correctly by getting a random Pet")
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine(".as(\"Check data on demand for '%1$s' initializes correctly by getting a random %1$s\")", getNameOfJavaType(this.entity));
    // .isNotNull();
    bodyBuilder.appendFormalLine(".isNotNull();");
    bodyBuilder.reset();
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, BEFORE_METHOD_NAME, JavaType.VOID_PRIMITIVE, bodyBuilder);
    // Add @Before
    methodBuilder.addAnnotation(new AnnotationMetadataBuilder(BEFORE));
    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 93 with MethodMetadata

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

the class ServiceImplMetadata method removeMethodFromPendingList.

/**
 * Removes metadataToRemove from list
 *
 * Compares, method name, number params and params types to decide it method
 * match and should be removed from list.
 *
 * @param list
 * @param metadataToRemove
 */
private void removeMethodFromPendingList(List<MethodMetadata> list, MethodMetadata metadataToRemove) {
    final Iterator<MethodMetadata> iter = list.iterator();
    MethodMetadata current;
    boolean matchTypes;
    while (iter.hasNext()) {
        current = iter.next();
        matchTypes = true;
        // Check name
        if (!current.getMethodName().equals(metadataToRemove.getMethodName())) {
            continue;
        }
        // Check number of params
        if (current.getParameterTypes().size() != metadataToRemove.getParameterTypes().size()) {
            continue;
        }
        // Check params types
        for (int i = 0; i < current.getParameterTypes().size(); i++) {
            if (!current.getParameterTypes().get(i).equals(metadataToRemove.getParameterTypes().get(i))) {
                matchTypes = false;
                break;
            }
        }
        // all matches
        if (matchTypes) {
            // remove item
            iter.remove();
        }
    }
}
Also used : MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata)

Example 94 with MethodMetadata

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

the class ServiceImplMetadata method getEntityTypeGetterMethod.

/**
 * Builds a method which returns the class of entity JavaType.
 *
 * @return MethodMetadataBuilder
 */
private MethodMetadata getEntityTypeGetterMethod() {
    List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    MethodMetadata existingMethod = getGovernorMethod(GET_ENTITY_TYPE_METHOD_NAME, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
    if (existingMethod != null) {
        return existingMethod;
    }
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // return ENTITY_TYPE.class;
    bodyBuilder.appendFormalLine("return %s.class;", this.entity.getNameIncludingTypeParameters(false, importResolver));
    // Use the MethodMetadataBuilder for easy creation of MethodMetadata
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, GET_ENTITY_TYPE_METHOD_NAME, JavaType.wrapperOf(JavaType.CLASS, this.entity), 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 95 with MethodMetadata

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

the class ServiceImplMetadataProviderImpl method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
    final ServiceImplAnnotationValues annotationValues = new ServiceImplAnnotationValues(governorPhysicalTypeMetadata);
    // Getting service interface
    JavaType serviceInterface = annotationValues.getService();
    ClassOrInterfaceTypeDetails serviceInterfaceDetails = getTypeLocationService().getTypeDetails(serviceInterface);
    final ServiceMetadata serviceMetadata = getServiceMetadata(metadataIdentificationString, serviceInterfaceDetails);
    AnnotationMetadata serviceAnnotation = serviceInterfaceDetails.getAnnotation(RooJavaType.ROO_SERVICE);
    Validate.notNull(serviceAnnotation, "ERROR: Provided service should be annotated with @RooService");
    JavaType entity = (JavaType) serviceAnnotation.getAttribute("entity").getValue();
    ClassOrInterfaceTypeDetails entityDetails = getTypeLocationService().getTypeDetails(entity);
    final String entityMetadataId = JpaEntityMetadata.createIdentifier(entityDetails.getType(), PhysicalTypeIdentifier.getPath(entityDetails.getDeclaredByMetadataId()));
    final JpaEntityMetadata entityMetadata = (JpaEntityMetadata) getMetadataService().get(entityMetadataId);
    // Add dependencies between modules
    if (serviceMetadata != null) {
        for (MethodMetadata method : serviceMetadata.getAllMethods()) {
            List<JavaType> types = new ArrayList<JavaType>();
            types.add(method.getReturnType());
            types.addAll(method.getReturnType().getParameters());
            for (AnnotatedJavaType parameter : method.getParameterTypes()) {
                types.add(AnnotatedJavaType.convertFromAnnotatedJavaType(parameter));
                types.addAll(AnnotatedJavaType.convertFromAnnotatedJavaType(parameter).getParameters());
            }
            for (JavaType parameter : types) {
                getTypeLocationService().addModuleDependency(governorPhysicalTypeMetadata.getType().getModule(), parameter);
            }
        }
    }
    // Getting associated repository
    ClassOrInterfaceTypeDetails repositoryDetails = getRepositoryJpaLocator().getRepository(entity);
    final String repositoryMetadataId = RepositoryJpaMetadata.createIdentifier(repositoryDetails.getType(), PhysicalTypeIdentifier.getPath(repositoryDetails.getDeclaredByMetadataId()));
    final RepositoryJpaMetadata repositoryMetadata = (RepositoryJpaMetadata) getMetadataService().get(repositoryMetadataId);
    // Locate related services API types required by relations
    Map<JavaType, ServiceMetadata> requiredServicesByEntity = new HashMap<JavaType, ServiceMetadata>();
    ClassOrInterfaceTypeDetails relatedService;
    for (RelationInfo info : entityMetadata.getRelationInfos().values()) {
        if (info.cardinality != Cardinality.ONE_TO_ONE && !requiredServicesByEntity.containsKey(info.childType)) {
            relatedService = getServiceLocator().getFirstService(info.childType);
            if (relatedService != null) {
                requiredServicesByEntity.put(info.childType, getServiceMetadata(metadataIdentificationString, relatedService));
            }
        }
    }
    // Get child relations info
    List<Pair<FieldMetadata, RelationInfo>> childRelationsInfo = getJpaOperations().getFieldChildPartOfRelation(entityDetails);
    return new ServiceImplMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, serviceInterface, repositoryDetails.getType(), repositoryMetadata, entity, entityMetadata, serviceMetadata, requiredServicesByEntity, childRelationsInfo);
}
Also used : RepositoryJpaMetadata(org.springframework.roo.addon.layers.repository.jpa.addon.RepositoryJpaMetadata) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) RelationInfo(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) JpaEntityMetadata(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata) Pair(org.apache.commons.lang3.tuple.Pair)

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