use of org.springframework.roo.classpath.details.MethodMetadataBuilder in project spring-roo by spring-projects.
the class RepositoryJpaMetadata method getCountMethodByField.
/**
* Method that generates method "countByField" method.
*
* @param relationInfo
* @param identifierType
*
* @return field
*/
public MethodMetadata getCountMethodByField(FieldMetadata field, RelationInfo relationInfo) {
// Define method name
String countPattern = "countBy%s";
if (relationInfo.cardinality == Cardinality.MANY_TO_MANY) {
countPattern = "countBy%sContains";
}
final JavaSymbolName methodName = new JavaSymbolName(String.format(countPattern, field.getFieldName().getSymbolNameCapitalisedFirstLetter()));
// Define method parameter type and name
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
final JavaType paramType = field.getFieldType().getBaseType();
parameterTypes.add(AnnotatedJavaType.convertFromJavaType(paramType));
parameterNames.add(new JavaSymbolName(StringUtils.uncapitalize(field.getFieldName().getSymbolName())));
MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
if (existingMethod != null) {
return existingMethod;
}
// Use the MethodMetadataBuilder for easy creation of MethodMetadata
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC + Modifier.ABSTRACT, methodName, JavaType.LONG_PRIMITIVE, parameterTypes, parameterNames, null);
return methodBuilder.build();
}
use of org.springframework.roo.classpath.details.MethodMetadataBuilder in project spring-roo by spring-projects.
the class RepositoryJpaMetadata method getFinderMethod.
/**
* Method that generates finder method on current interface
*
* @param finderMethod
* @return
*/
private MethodMetadataBuilder getFinderMethod(FinderMethod finderMethod) {
// Define method parameter types and parameter names
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
for (FinderParameter param : finderMethod.getParameters()) {
parameterTypes.add(AnnotatedJavaType.convertFromJavaType(param.getType()));
parameterNames.add(param.getName());
}
// Add additional Pageable method
parameterTypes.add(AnnotatedJavaType.convertFromJavaType(SpringJavaType.PAGEABLE));
parameterNames.add(new JavaSymbolName("pageable"));
// Use the MethodMetadataBuilder for easy creation of MethodMetadata
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC + Modifier.ABSTRACT, finderMethod.getMethodName(), finderMethod.getReturnType(), parameterTypes, parameterNames, null);
// Build and return a MethodMetadata
return methodBuilder;
// instance
}
use of org.springframework.roo.classpath.details.MethodMetadataBuilder 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();
}
use of org.springframework.roo.classpath.details.MethodMetadataBuilder in project spring-roo by spring-projects.
the class RepositoryJpaMetadata method getCountMethod.
/**
* Method that generates finder method on current interface
*
* @param finderMethod
* @return
*/
private MethodMetadataBuilder getCountMethod(FinderMethod finderMethod) {
// Define method parameter types and parameter names
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
for (FinderParameter param : finderMethod.getParameters()) {
parameterTypes.add(AnnotatedJavaType.convertFromJavaType(param.getType()));
parameterNames.add(param.getName());
}
// Create count method name
String countName = finderMethod.getMethodName().getSymbolName();
if (StringUtils.startsWith(countName, "find")) {
countName = StringUtils.removeStart(countName, "find");
} else if (StringUtils.startsWith(countName, "query")) {
countName = StringUtils.removeStart(countName, "query");
} else if (StringUtils.startsWith(countName, "read")) {
countName = StringUtils.removeStart(countName, "read");
}
countName = "count".concat(countName);
JavaSymbolName countMethodName = new JavaSymbolName(countName);
// Use the MethodMetadataBuilder for easy creation of MethodMetadata
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC + Modifier.ABSTRACT, countMethodName, JavaType.LONG_PRIMITIVE, parameterTypes, parameterNames, null);
// Build and return a MethodMetadata
return methodBuilder;
// instance
}
use of org.springframework.roo.classpath.details.MethodMetadataBuilder 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();
}
Aggregations