use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.
the class JavaBeanMetadata method getSingularEntityAccessor.
private InvocableMemberBodyBuilder getSingularEntityAccessor(final FieldMetadata field, final JavaSymbolName hiddenIdFieldName) {
final String entityName = field.getFieldName().getSymbolName();
final String entityIdName = hiddenIdFieldName.getSymbolName();
final String simpleFieldTypeName = field.getFieldType().getSimpleTypeName();
final String identifierMethodName = getIdentifierMethodName(field).getSymbolName();
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
bodyBuilder.appendFormalLine("if (this." + entityIdName + " != null) {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine("if (this." + entityName + " == null || this." + entityName + "." + identifierMethodName + "() != this." + entityIdName + ") {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine("this." + entityName + " = " + simpleFieldTypeName + ".find" + simpleFieldTypeName + "(this." + entityIdName + ");");
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("} else {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine("this." + entityName + " = null;");
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
bodyBuilder.appendFormalLine("return this." + entityName + ";");
return bodyBuilder;
}
use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.
the class JavaBeanMetadata method getSingularEntityMutator.
private InvocableMemberBodyBuilder getSingularEntityMutator(final FieldMetadata field, final JavaSymbolName hiddenIdFieldName) {
final String entityName = field.getFieldName().getSymbolName();
final String entityIdName = hiddenIdFieldName.getSymbolName();
final String identifierMethodName = getIdentifierMethodName(field).getSymbolName();
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
bodyBuilder.appendFormalLine("if (" + entityName + " != null) {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine("if (" + entityName + "." + identifierMethodName + " () == null) {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine(entityName + ".persist();");
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
bodyBuilder.appendFormalLine("this." + entityIdName + " = " + entityName + "." + identifierMethodName + "();");
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("} else {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine("this." + entityIdName + " = null;");
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
bodyBuilder.appendFormalLine("this." + entityName + " = " + entityName + ";");
return bodyBuilder;
}
use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.
the class JavaBeanMetadata method generateGettersAndSetters.
protected void generateGettersAndSetters(final Map<FieldMetadata, JavaSymbolName> declaredFields, List<? extends MethodMetadata> interfaceMethods) {
// Add getters and setters
for (final Entry<FieldMetadata, JavaSymbolName> entry : declaredFields.entrySet()) {
final FieldMetadata field = entry.getKey();
final MethodMetadataBuilder accessorMethod = getDeclaredGetter(field);
final MethodMetadataBuilder mutatorMethod = getDeclaredSetter(field);
// Check to see if GAE is interested
if (entry.getValue() != null) {
JavaSymbolName hiddenIdFieldName;
if (field.getFieldType().isCommonCollectionType()) {
hiddenIdFieldName = governorTypeDetails.getUniqueFieldName(field.getFieldName().getSymbolName() + "Keys");
builder.getImportRegistrationResolver().addImport(GAE_DATASTORE_KEY_FACTORY);
builder.addField(getMultipleEntityIdField(hiddenIdFieldName));
} else {
hiddenIdFieldName = governorTypeDetails.getUniqueFieldName(field.getFieldName().getSymbolName() + "Id");
builder.addField(getSingularEntityIdField(hiddenIdFieldName));
}
processGaeAnnotations(field);
InvocableMemberBodyBuilder gaeAccessorBody = getGaeAccessorBody(field, hiddenIdFieldName);
accessorMethod.setBodyBuilder(gaeAccessorBody);
InvocableMemberBodyBuilder gaeMutatorBody = getGaeMutatorBody(field, hiddenIdFieldName);
mutatorMethod.setBodyBuilder(gaeMutatorBody);
}
// Add to mutators and accesors list and build
if (accessorMethod != null) {
this.accesorMethods.put(field.getFieldName(), accessorMethod.build());
builder.addMethod(accessorMethod);
}
if (mutatorMethod != null) {
this.mutatorMethods.put(field.getFieldName(), mutatorMethod.build());
builder.addMethod(mutatorMethod);
}
}
// Implements interface methods if exists
implementsInterfaceMethods(interfaceMethods);
}
use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.
the class JavaBeanMetadata method getDeclaredSetter.
/**
* Obtains the specific mutator method that is either contained within the
* normal Java compilation unit or will be introduced by this add-on via an
* ITD.
*
* @param field
* that already exists on the type either directly or via
* introduction (required; must be declared by this type to be
* located)
* @return the method corresponding to a mutator, or null if not found
*/
private MethodMetadataBuilder getDeclaredSetter(final FieldMetadata field) {
Validate.notNull(field, "Field required");
// Compute the mutator method name
final JavaSymbolName methodName = BeanInfoUtils.getMutatorMethodName(field);
// Compute the mutator method parameters
final JavaType parameterType = field.getFieldType();
// See if the type itself declared the mutator
if (governorHasMethod(methodName, parameterType)) {
return null;
}
// Compute the mutator method parameter names
final List<JavaSymbolName> parameterNames = Arrays.asList(field.getFieldName());
// final fields as per ROO-36)
if (annotationValues.isSettersByDefault() && !Modifier.isTransient(field.getModifier()) && !Modifier.isStatic(field.getModifier()) && !Modifier.isFinal(field.getModifier())) {
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
bodyBuilder.appendFormalLine("this." + field.getFieldName().getSymbolName() + " = " + field.getFieldName().getSymbolName() + ";");
return new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, AnnotatedJavaType.convertFromJavaTypes(parameterType), parameterNames, bodyBuilder);
}
return null;
}
use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.
the class JavaBeanMetadata method getInterfaceMethod.
/**
* Obtains a valid MethodMetadataBuilder with necessary configuration
*
* @param method
* @return MethodMetadataBuilder
*/
private MethodMetadataBuilder getInterfaceMethod(final MethodMetadata method) {
// Compute the method name
final JavaSymbolName methodName = method.getMethodName();
// See if the type itself declared the accessor
if (governorHasMethod(methodName)) {
return null;
}
// Getting return type
JavaType returnType = method.getReturnType();
// Generating body
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// If return type is not primitive, return null
if (returnType.isPrimitive()) {
JavaType baseType = returnType.getBaseType();
if (baseType.equals(JavaType.BOOLEAN_PRIMITIVE)) {
bodyBuilder.appendFormalLine("return false;");
} else if (baseType.equals(JavaType.BYTE_PRIMITIVE)) {
bodyBuilder.appendFormalLine("return 0;");
} else if (baseType.equals(JavaType.SHORT_PRIMITIVE)) {
bodyBuilder.appendFormalLine("return 0;");
} else if (baseType.equals(JavaType.INT_PRIMITIVE)) {
bodyBuilder.appendFormalLine("return 0;");
} else if (baseType.equals(JavaType.LONG_PRIMITIVE)) {
bodyBuilder.appendFormalLine("return 0;");
} else if (baseType.equals(JavaType.FLOAT_PRIMITIVE)) {
bodyBuilder.appendFormalLine("return 0;");
} else if (baseType.equals(JavaType.DOUBLE_PRIMITIVE)) {
bodyBuilder.appendFormalLine("return 0.00;");
} else if (baseType.equals(JavaType.CHAR_PRIMITIVE)) {
bodyBuilder.appendFormalLine("return '\0';");
}
} else {
bodyBuilder.appendFormalLine("return null;");
}
return new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, returnType, bodyBuilder);
}
Aggregations