use of org.springframework.roo.classpath.details.MethodMetadataBuilder in project spring-roo by spring-projects.
the class JpaEntityFactoryMetadata method getEmbeddedClassMutatorMethod.
private MethodMetadataBuilder getEmbeddedClassMutatorMethod(final EmbeddedHolder embeddedHolder) {
final JavaSymbolName methodName = getEmbeddedFieldMutatorMethodName(embeddedHolder.getEmbeddedField().getFieldName());
final JavaType[] parameterTypes = { entity, JavaType.INT_PRIMITIVE };
// Locate user-defined method
if (governorHasMethod(methodName, parameterTypes)) {
// Method found in governor so do not create method in ITD
return null;
}
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// Create constructor for embedded class
final JavaType embeddedFieldType = embeddedHolder.getEmbeddedField().getFieldType();
builder.getImportRegistrationResolver().addImport(embeddedFieldType);
bodyBuilder.appendFormalLine(embeddedFieldType.getSimpleTypeName() + " embeddedClass = new " + embeddedFieldType.getSimpleTypeName() + "();");
for (final FieldMetadata field : embeddedHolder.getFields()) {
bodyBuilder.appendFormalLine(getEmbeddedFieldMutatorMethodName(embeddedHolder.getEmbeddedField().getFieldName(), field.getFieldName()).getSymbolName() + "(embeddedClass, " + INDEX_VAR + ");");
}
bodyBuilder.appendFormalLine(OBJ_VAR + "." + embeddedHolder.getEmbeddedMutatorMethodName() + "(embeddedClass);");
final List<JavaSymbolName> parameterNames = Arrays.asList(OBJ_SYMBOL, INDEX_SYMBOL);
return new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, AnnotatedJavaType.convertFromJavaTypes(parameterTypes), parameterNames, bodyBuilder);
}
use of org.springframework.roo.classpath.details.MethodMetadataBuilder in project spring-roo by spring-projects.
the class JpaEntityFactoryMetadata method getEmbeddedIdMutatorMethod.
private MethodMetadataBuilder getEmbeddedIdMutatorMethod(final Set<ClassOrInterfaceTypeDetails> dataOnDemandClasses) {
if (!hasEmbeddedIdentifier()) {
return null;
}
final JavaSymbolName embeddedIdMutator = embeddedIdHolder.getEmbeddedIdMutator();
final JavaSymbolName methodName = getEmbeddedIdMutatorMethodName();
final JavaType[] parameterTypes = { entity, JavaType.INT_PRIMITIVE };
// Locate user-defined method
if (governorHasMethod(methodName, parameterTypes)) {
// Method found in governor so do not create method in ITD
return null;
}
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// Create constructor for embedded id class
final JavaType embeddedIdFieldType = embeddedIdHolder.getEmbeddedIdField().getFieldType();
builder.getImportRegistrationResolver().addImport(embeddedIdFieldType);
final StringBuilder sb = new StringBuilder();
final List<FieldMetadata> identifierFields = embeddedIdHolder.getIdFields();
for (int i = 0, n = identifierFields.size(); i < n; i++) {
if (i > 0) {
sb.append(", ");
}
final FieldMetadata field = identifierFields.get(i);
final String fieldName = field.getFieldName().getSymbolName();
final JavaType fieldType = field.getFieldType();
builder.getImportRegistrationResolver().addImport(fieldType);
final String initializer = getFieldInitializer(field, null, dataOnDemandClasses);
bodyBuilder.append(getFieldValidationBody(field, initializer, null, true));
sb.append(fieldName);
}
bodyBuilder.appendFormalLine("");
bodyBuilder.appendFormalLine(embeddedIdFieldType.getSimpleTypeName() + " embeddedIdClass = new " + embeddedIdFieldType.getSimpleTypeName() + "(" + sb.toString() + ");");
bodyBuilder.appendFormalLine(OBJ_VAR + "." + embeddedIdMutator + "(embeddedIdClass);");
final List<JavaSymbolName> parameterNames = Arrays.asList(OBJ_SYMBOL, INDEX_SYMBOL);
return new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, AnnotatedJavaType.convertFromJavaTypes(parameterTypes), parameterNames, bodyBuilder);
}
use of org.springframework.roo.classpath.details.MethodMetadataBuilder in project spring-roo by spring-projects.
the class JpaEntityFactoryMetadata method getCreateMethod.
private MethodMetadata getCreateMethod() {
// Define methodName
final JavaSymbolName methodName = CREATE_FACTORY_METHOD_NAME;
List<JavaType> parameterTypes = new ArrayList<JavaType>();
parameterTypes.add(JavaType.INT_PRIMITIVE);
// Check if method exists
MethodMetadata existingMethod = getGovernorMethod(methodName, parameterTypes);
if (existingMethod != null) {
return existingMethod;
}
List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
parameterNames.add(INDEX_SYMBOL);
// Add body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// Entity obj = new Entity();
bodyBuilder.appendFormalLine("%1$s %2$s = new %1$s();", getNameOfJavaType(this.entity), OBJ_SYMBOL);
// Set values for transient object
for (final Map.Entry<FieldMetadata, JpaEntityFactoryMetadata> entry : locatedFields.entrySet()) {
bodyBuilder.appendFormalLine("%s(%s, %s);", BeanInfoUtils.getMutatorMethodName(entry.getKey()), OBJ_SYMBOL, INDEX_SYMBOL);
}
// return obj;
bodyBuilder.appendFormalLine("return %s;", OBJ_SYMBOL);
// Create method
MethodMetadataBuilder method = new MethodMetadataBuilder(this.getId(), Modifier.PUBLIC, methodName, this.entity, AnnotatedJavaType.convertFromJavaTypes(parameterTypes), parameterNames, bodyBuilder);
CommentStructure commentStructure = new CommentStructure();
List<String> paramsInfo = new ArrayList<String>();
paramsInfo.add(String.format("%s position of the %s", INDEX_VAR, this.entity.getSimpleTypeName()));
JavadocComment comment = new JavadocComment(String.format("Creates a new {@link %s} with the given %s.", this.entity.getSimpleTypeName(), INDEX_VAR), paramsInfo, String.format("a new transient %s", this.entity.getSimpleTypeName()), null);
commentStructure.addComment(comment, CommentLocation.BEGINNING);
method.setCommentStructure(commentStructure);
return method.build();
}
use of org.springframework.roo.classpath.details.MethodMetadataBuilder in project spring-roo by spring-projects.
the class JpaEntityFactoryMetadata method getFieldMutatorMethods.
private List<MethodMetadataBuilder> getFieldMutatorMethods() {
final List<MethodMetadataBuilder> fieldMutatorMethods = new ArrayList<MethodMetadataBuilder>();
final List<JavaSymbolName> parameterNames = Arrays.asList(OBJ_SYMBOL, INDEX_SYMBOL);
final JavaType[] parameterTypes = { entity, JavaType.INT_PRIMITIVE };
for (final Map.Entry<FieldMetadata, String> entry : fieldInitializers.entrySet()) {
final FieldMetadata field = entry.getKey();
final JavaSymbolName mutatorName = BeanInfoUtils.getMutatorMethodName(field.getFieldName());
// Locate user-defined method
if (governorHasMethod(mutatorName, parameterTypes)) {
// Method found in governor so do not create method in ITD
continue;
}
// Method not on governor so need to create it
final String initializer = entry.getValue();
if (!StringUtils.isBlank(initializer)) {
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
bodyBuilder.append(getFieldValidationBody(field, initializer, mutatorName, false));
fieldMutatorMethods.add(new MethodMetadataBuilder(getId(), Modifier.PUBLIC, mutatorName, JavaType.VOID_PRIMITIVE, AnnotatedJavaType.convertFromJavaTypes(parameterTypes), parameterNames, bodyBuilder));
}
}
return fieldMutatorMethods;
}
use of org.springframework.roo.classpath.details.MethodMetadataBuilder in project spring-roo by spring-projects.
the class RepositoryJpaCustomMetadata method getFindAllMethodByReferencedField.
/**
* Method that generates the findAll method for provided referenced field on current interface.
*
* @param referencedField
* @param identifierType
*
* @return
*/
private MethodMetadata getFindAllMethodByReferencedField(FieldMetadata referencedField, RelationInfo relationInfo) {
// Define method name
String findPattern = "findBy%s";
if (relationInfo.cardinality == Cardinality.MANY_TO_MANY) {
findPattern = "findBy%sContains";
}
// Method name
JavaSymbolName methodName = new JavaSymbolName(String.format(findPattern, referencedField.getFieldName().getSymbolNameCapitalisedFirstLetter()));
// Define method parameter types and parameter names
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
final JavaType paramType = referencedField.getFieldType().getBaseType();
parameterTypes.add(AnnotatedJavaType.convertFromJavaType(paramType));
parameterTypes.add(GLOBAL_SEARCH_PARAMETER);
parameterTypes.add(PAGEABLE_PARAMETER);
List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
parameterNames.add(new JavaSymbolName(StringUtils.uncapitalize(referencedField.getFieldName().getSymbolName())));
parameterNames.add(GOBAL_SEARCH_PARAMETER_NAME);
parameterNames.add(PAGEABLE_PARAMETER_NAME);
// Return type
JavaType returnType = new JavaType(SpringJavaType.PAGE.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Arrays.asList(defaultReturnType));
// Use the MethodMetadataBuilder for easy creation of MethodMetadata
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC + Modifier.ABSTRACT, methodName, returnType, parameterTypes, parameterNames, null);
// Build and return a MethodMetadata
return methodBuilder.build();
}
Aggregations