use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.
the class JpaEntityFactoryMetadata method getEmbeddedIdMutatorMethodName.
public JavaSymbolName getEmbeddedIdMutatorMethodName() {
final List<JavaSymbolName> fieldNames = new ArrayList<JavaSymbolName>();
for (final FieldMetadata field : fieldInitializers.keySet()) {
fieldNames.add(field.getFieldName());
}
int index = -1;
JavaSymbolName embeddedIdField;
while (true) {
// Compute the required field name
index++;
embeddedIdField = new JavaSymbolName("embeddedIdClass" + StringUtils.repeat("_", index));
if (!fieldNames.contains(embeddedIdField)) {
// Found a usable name
break;
}
}
return BeanInfoUtils.getMutatorMethodName(embeddedIdField);
}
use of org.springframework.roo.classpath.details.FieldMetadata 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.FieldMetadata 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.FieldMetadata 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.FieldMetadata 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;
}
Aggregations