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();
}
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();
}
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();
}
}
}
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();
}
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);
}
Aggregations