use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.
the class JavaBeanMetadata method getInterfaceMethodBody.
private InvocableMemberBodyBuilder getInterfaceMethodBody(JavaType returnType) {
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
bodyBuilder.appendFormalLine("// Interface Implementation");
bodyBuilder.appendFormalLine("return 0;");
return bodyBuilder;
}
use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.
the class JavaBeanMetadata method getEntityCollectionAccessorBody.
private InvocableMemberBodyBuilder getEntityCollectionAccessorBody(final FieldMetadata field, final JavaSymbolName entityIdsFieldName) {
final String entityCollectionName = field.getFieldName().getSymbolName();
final String entityIdsName = entityIdsFieldName.getSymbolName();
final String localEnitiesName = "local" + StringUtils.capitalize(entityCollectionName);
final JavaType collectionElementType = field.getFieldType().getParameters().get(0);
final String simpleCollectionElementTypeName = collectionElementType.getSimpleTypeName();
JavaType collectionType = field.getFieldType();
builder.getImportRegistrationResolver().addImport(collectionType);
final String collectionName = field.getFieldType().getNameIncludingTypeParameters().replace(field.getFieldType().getPackage().getFullyQualifiedPackageName() + ".", "");
String instantiableCollection = collectionName;
// need a concrete implementation of either.
if (collectionType.getFullyQualifiedTypeName().equals(LIST.getFullyQualifiedTypeName())) {
collectionType = new JavaType(ARRAY_LIST.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, collectionType.getParameters());
instantiableCollection = collectionType.getNameIncludingTypeParameters().replace(collectionType.getPackage().getFullyQualifiedPackageName() + ".", "");
} else if (collectionType.getFullyQualifiedTypeName().equals(SET.getFullyQualifiedTypeName())) {
collectionType = new JavaType(HASH_SET.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, collectionType.getParameters());
instantiableCollection = collectionType.getNameIncludingTypeParameters().replace(collectionType.getPackage().getFullyQualifiedPackageName() + ".", "");
}
builder.getImportRegistrationResolver().addImport(collectionType);
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
bodyBuilder.appendFormalLine(collectionName + " " + localEnitiesName + " = new " + instantiableCollection + "();");
bodyBuilder.appendFormalLine("for (Key key : " + entityIdsName + ") {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine(simpleCollectionElementTypeName + " entity = " + simpleCollectionElementTypeName + ".find" + simpleCollectionElementTypeName + "(key.getId());");
bodyBuilder.appendFormalLine("if (entity != null) {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine(localEnitiesName + ".add(entity);");
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
bodyBuilder.appendFormalLine("this." + entityCollectionName + " = " + localEnitiesName + ";");
bodyBuilder.appendFormalLine("return " + localEnitiesName + ";");
return bodyBuilder;
}
use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.
the class JavaBeanMetadata method getDeclaredGetter.
/**
* Obtains the specific accessor 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 an accessor, or null if not found
*/
private MethodMetadataBuilder getDeclaredGetter(final FieldMetadata field) {
Validate.notNull(field, "Field required");
// Compute the mutator method name
final JavaSymbolName methodName = BeanInfoUtils.getAccessorMethodName(field);
// See if the type itself declared the accessor
if (governorHasMethod(methodName)) {
return null;
}
// for reason we allow a getter for a final field)
if (annotationValues.isGettersByDefault() && !Modifier.isTransient(field.getModifier()) && !Modifier.isStatic(field.getModifier())) {
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
bodyBuilder.appendFormalLine("return this." + field.getFieldName().getSymbolName() + ";");
return new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, field.getFieldType(), bodyBuilder);
}
return null;
}
use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.
the class EqualsMetadata method generateDefaultEqualsMethodBody.
/**
* Returns the default `equals` method body. Used for not-entity classes.
*
* @return {@link InvocableMemberBodyBuilder}
*/
private static InvocableMemberBodyBuilder generateDefaultEqualsMethodBody(JavaType target, boolean appendSuper, List<FieldMetadata> fields, ImportRegistrationResolver importRegistrationResolver) {
String typeName = target.getSimpleTypeName();
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
bodyBuilder.appendFormalLine("if (!(" + OBJECT_NAME + " instanceof " + typeName + ")) {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine("return false;");
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
bodyBuilder.appendFormalLine("if (this == " + OBJECT_NAME + ") {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine("return true;");
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
bodyBuilder.appendFormalLine(typeName + " rhs = (" + typeName + ") " + OBJECT_NAME + ";");
final StringBuilder builder = new StringBuilder(String.format("return new %s()", EQUALS_BUILDER.getNameIncludingTypeParameters(false, importRegistrationResolver)));
if (appendSuper) {
builder.append(".appendSuper(super.equals(" + OBJECT_NAME + "))");
}
for (final FieldMetadata field : fields) {
builder.append(".append(" + field.getFieldName() + ", rhs." + field.getFieldName() + ")");
}
builder.append(".isEquals();");
bodyBuilder.appendFormalLine(builder.toString());
return bodyBuilder;
}
use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.
the class RepositoryJpaIntegrationTestMetadata method getPersistGenerateIdTestMethod.
/**
* Builds a method to test the generation of id when persisting entities.
*
* @return {@link MethodMetadata}
*/
private MethodMetadata getPersistGenerateIdTestMethod() {
// Check if method exists on governor
MethodMetadata method = getGovernorMethod(PERSIST_SHOULD_GENERATE_ID_METHOD_NAME);
if (method != null) {
return method;
}
// Build method body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// Setup
bodyBuilder.appendFormalLine("// Setup");
// Exercise
bodyBuilder.appendFormalLine("// Exercise");
// Pet pet = dod.getNewRandomTransientPet();
bodyBuilder.appendFormalLine("%1$s %2$s = %3$s().getNewRandomTransient%1$s();", getNameOfJavaType(this.entity), this.entityVar, getAccessorMethod(this.dodField).getMethodName());
// Verify
bodyBuilder.newLine();
bodyBuilder.appendFormalLine("// Verify");
// assertThat(pet).as("Check the Data on demand generated a new non null 'Pet'").isNotNull();
bodyBuilder.appendFormalLine("%s(%s).as(\"Check the Data on demand generated a new non null '%s'\").isNotNull();", getNameOfJavaType(ASSERT_THAT), this.entityVar, getNameOfJavaType(this.entity));
// assertThat(pet.getId()).as("Check the Data on demand generated a new 'Pet' whose id is null")
bodyBuilder.appendFormalLine("%s(%s.%s()).as(\"Check the Data on demand generated a new '%s' whose id is null\")", getNameOfJavaType(ASSERT_THAT), this.entityVar, this.identifierAccessorMethodName, getNameOfJavaType(this.entity));
// .isNull();
bodyBuilder.indent();
bodyBuilder.appendFormalLine(".isNull();");
bodyBuilder.indentRemove();
// try {
bodyBuilder.appendFormalLine("try {");
// pet = repository.saveAndFlush(pet);
bodyBuilder.indent();
bodyBuilder.appendFormalLine("%1$s = %2$s().saveAndFlush(%1$s);", this.entityVar, getAccessorMethod(this.repositoryField).getMethodName());
// } catch (final ConstraintViolationException e) {
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("} catch (final %s e) {", getNameOfJavaType(Jsr303JavaType.CONSTRAINT_VIOLATION_EXCEPTION));
// final StringBuilder msg = new StringBuilder();
bodyBuilder.indent();
bodyBuilder.appendFormalLine("final %1$s msg = new %1$s();", getNameOfJavaType(JdkJavaType.STRING_BUILDER));
// for (Iterator<ConstraintViolation<?>> iter = e.getConstraintViolations().iterator(); iter
bodyBuilder.appendFormalLine("for (%s<%s<?>> iter = e.getConstraintViolations().iterator(); iter", getNameOfJavaType(JdkJavaType.ITERATOR), getNameOfJavaType(Jsr303JavaType.CONSTRAINT_VIOLATION));
// .hasNext();) {
bodyBuilder.indent();
bodyBuilder.appendFormalLine(".hasNext();) {");
// final ConstraintViolation<?> cv = iter.next();
bodyBuilder.appendFormalLine("final %s<?> cv = iter.next();", getNameOfJavaType(Jsr303JavaType.CONSTRAINT_VIOLATION));
// msg.append("[").append(cv.getRootBean().getClass().getName()).append(".")
bodyBuilder.appendFormalLine("msg.append(\"[\").append(cv.getRootBean().getClass().getName()).append(\".\")");
// .append(cv.getPropertyPath()).append(": ").append(cv.getMessage())
bodyBuilder.indent();
bodyBuilder.appendFormalLine(".append(cv.getPropertyPath()).append(\": \").append(cv.getMessage())");
// .append(" (invalid value = ").append(cv.getInvalidValue()).append(")").append("]");
bodyBuilder.appendFormalLine(".append(\" (invalid value = \").append(cv.getInvalidValue()).append(\")\").append(\"]\");");
// }
bodyBuilder.indentRemove();
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
// throw new IllegalStateException(msg.toString(), e);
bodyBuilder.appendFormalLine("throw new %s(msg.toString(), e);", getNameOfJavaType(JdkJavaType.ILLEGAL_STATE_EXCEPTION));
bodyBuilder.indentRemove();
// }
bodyBuilder.appendFormalLine("}");
// assertThat(pet.getId()).as("Check a 'Pet' (%s) id is not null after been persisted", pet)
bodyBuilder.appendFormalLine("%1$s(%2$s.%3$s()).as(\"Check a '%4$s' (%5$s) id is not null after been persisted\", %2$s)", getNameOfJavaType(ASSERT_THAT), this.entityVar, this.identifierAccessorMethodName, getNameOfJavaType(this.entity), "%s");
// .isNotNull();
bodyBuilder.indent();
bodyBuilder.appendFormalLine(".isNotNull();");
bodyBuilder.reset();
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, PERSIST_SHOULD_GENERATE_ID_METHOD_NAME, JavaType.VOID_PRIMITIVE, bodyBuilder);
// Add @Test
methodBuilder.addAnnotation(new AnnotationMetadataBuilder(TEST));
return methodBuilder.build();
}
Aggregations