use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.
the class JpaUnitTestMetadata method getRemoveFromRelationMethod.
/**
* Creates a method to test item removes from a relationship.
*
* @param removeMethod the {@link MethodMetadata} to test.
* @param relationFieldName a {@link String} with the field name
* representing the relation on the parent entity.
* @param childEntity the {@link JavaType} of child entity.
* @param field the {@link FieldMetadata} of relation field.
* @return the {@link MethodMetadata}
*/
private MethodMetadata getRemoveFromRelationMethod(RelationInfo relationInfo) {
// Initialize local variables
String relationFieldName = relationInfo.fieldName;
JavaType childEntity = relationInfo.childType;
FieldMetadata relationField = relationInfo.fieldMetadata;
// Create test method name using target method name, child entity name and relation field
JavaSymbolName methodName = new JavaSymbolName(String.format("%sShouldRemoveThe%sFromThe%sRelationship", relationInfo.addMethod.getMethodName().getSymbolName(), childEntity.getSimpleTypeName(), relationFieldName));
MethodMetadata method = getGovernorMethod(methodName);
if (method != null) {
return method;
}
// Create body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// Setup
bodyBuilder.appendFormalLine("// Setup");
// Entity entity = entityFactory.create(0);
FieldMetadata entityFactory = entityFactories.get(this.entityFactory);
bodyBuilder.appendFormalLine("%s %s = %s().create(0);", getNameOfJavaType(this.entity), this.entityVar, getAccessorMethod(entityFactory).getMethodName());
// ChildEntity childEntity1 = childEntityFactory.create(0);
FieldMetadata childEntityFactory = entityFactories.get(this.entityAndItsFactoryMap.get(childEntity));
Validate.notNull(childEntityFactory, "Unable to locate the entity factory of %s in JpaUnitTestMetadata", childEntity.getSimpleTypeName());
String childEntityVar1 = String.format("%s1", StringUtils.uncapitalize(childEntity.getSimpleTypeName()));
bodyBuilder.appendFormalLine("%s %s = %s().create(0);", getNameOfJavaType(childEntity), childEntityVar1, getAccessorMethod(childEntityFactory).getMethodName());
// ChildEntity childEntity2 = childEntityFactory.create(1);
String childEntityVar2 = String.format("%s2", StringUtils.uncapitalize(childEntity.getSimpleTypeName()));
bodyBuilder.appendFormalLine("%s %s = %s().create(1);", getNameOfJavaType(childEntity), childEntityVar2, getAccessorMethod(childEntityFactory).getMethodName());
// entity.ADD_METHOD(Arrays.asList(childEntity1, childEntity2));
bodyBuilder.appendFormalLine("%s.%s(%s.asList(%s, %s));", entityVar, relationInfo.addMethod.getMethodName(), getNameOfJavaType(JavaType.ARRAYS), childEntityVar1, childEntityVar2);
bodyBuilder.newLine();
// Exercise
bodyBuilder.appendFormalLine("// Exercise");
// entity.REMOVE_METHOD(Collections.singleton(childEntity1));
bodyBuilder.appendFormalLine("%s.%s(%s.singleton(%s));", this.entityVar, relationInfo.removeMethod.getMethodName(), getNameOfJavaType(JavaType.COLLECTIONS), childEntityVar1);
bodyBuilder.newLine();
// Verify
bodyBuilder.appendFormalLine("// Verify");
// assertThat(childEntity1.PARENT_ENTITY_ACCESSOR()).as("Check 'REMOVE_METHOD' updates the CHILD_ENTITY relationship side")
JavaSymbolName parentEntityAccessor = BeanInfoUtils.getAccessorMethodName(new JavaSymbolName(relationInfo.mappedBy), this.entity);
bodyBuilder.appendFormalLine("%s(%s.%s()).as(\"Check '%s' updates the %s relationship side\")", getNameOfJavaType(new JavaType("org.assertj.core.api.Assertions.assertThat"), true), childEntityVar1, parentEntityAccessor, relationInfo.removeMethod.getMethodName(), getNameOfJavaType(childEntity));
// .isNull();
bodyBuilder.indent();
bodyBuilder.appendFormalLine(".isNull();");
bodyBuilder.indentRemove();
// assertThat(entity.RELATION_FIELD_ACCESSOR()).as("Check 'REMOVE_METHOD' removes a CHILD_ENTITY from the relationship")
JavaSymbolName relationFieldAccessorName = BeanInfoUtils.getAccessorMethodName(relationField);
bodyBuilder.appendFormalLine("%s(%s.%s()).as(\"Check '%s' removes a %s from the relationship\")", getNameOfJavaType(new JavaType("org.assertj.core.api.Assertions.assertThat"), true), this.entityVar, relationFieldAccessorName, relationInfo.removeMethod.getMethodName(), getNameOfJavaType(childEntity));
// .doesNotContain(childEntity1).contains(childEntity2);
bodyBuilder.indent();
bodyBuilder.appendFormalLine(".doesNotContain(%s).contains(%s);", childEntityVar1, childEntityVar2);
bodyBuilder.reset();
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(this.getId(), Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, bodyBuilder);
// Add @Test
methodBuilder.addAnnotation(new AnnotationMetadataBuilder(TEST));
// Add throws types
methodBuilder.addThrowsType(JdkJavaType.EXCEPTION);
return methodBuilder.build();
}
use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.
the class JpaUnitTestMetadata method getAddToRelationMethod.
/**
* Creates a method to test item additions to a relationship.
*
* @param addMethod the {@link MethodMetadata} to test.
* @param relationFieldName a {@link String} with the field name
* representing the relation on the parent entity.
* @param childEntity the {@link JavaType} of child entity.
* @param field the {@link FieldMetadata} of relation field.
* @param relationInfo the {@link RelationInfo} of the relation field.
* @return the {@link MethodMetadata}
*/
private MethodMetadata getAddToRelationMethod(RelationInfo relationInfo) {
// Initialize local variables
MethodMetadata addMethod = relationInfo.addMethod;
String relationFieldName = relationInfo.fieldName;
JavaType childEntity = relationInfo.childType;
FieldMetadata relationField = relationInfo.fieldMetadata;
// Create test method name using target method name, child entity name and relation field
JavaSymbolName methodName = new JavaSymbolName(String.format("%sShouldAddThe%sToThe%sRelationship", addMethod.getMethodName().getSymbolName(), childEntity.getSimpleTypeName(), relationFieldName));
MethodMetadata method = getGovernorMethod(methodName);
if (method != null) {
return method;
}
// Create body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// Setup
bodyBuilder.appendFormalLine("// Setup");
// Entity entity = entityFactory.create(0);
FieldMetadata entityFactory = entityFactories.get(this.entityFactory);
bodyBuilder.appendFormalLine("%s %s = %s().create(0);", getNameOfJavaType(this.entity), this.entityVar, getAccessorMethod(entityFactory).getMethodName());
// ChildEntity childEntity1 = childEntityFactory.create(0);
FieldMetadata childEntityFactory = entityFactories.get(this.entityAndItsFactoryMap.get(childEntity));
Validate.notNull(childEntityFactory, "Unable to locate the entity factory of %s in JpaUnitTestMetadata", childEntity.getSimpleTypeName());
String childEntityVar1 = String.format("%s1", StringUtils.uncapitalize(childEntity.getSimpleTypeName()));
bodyBuilder.appendFormalLine("%s %s = %s().create(0);", getNameOfJavaType(childEntity), childEntityVar1, getAccessorMethod(childEntityFactory).getMethodName());
// ChildEntity childEntity2 = childEntityFactory.create(1);
String childEntityVar2 = String.format("%s2", StringUtils.uncapitalize(childEntity.getSimpleTypeName()));
bodyBuilder.appendFormalLine("%s %s = %s().create(1);", getNameOfJavaType(childEntity), childEntityVar2, getAccessorMethod(childEntityFactory).getMethodName());
bodyBuilder.newLine();
// Exercise
bodyBuilder.appendFormalLine("// Exercise");
// entity.ADD_METHOD(Arrays.asList(childEntity1, childEntity2));
bodyBuilder.appendFormalLine("%s.%s(%s.asList(%s, %s));", entityVar, addMethod.getMethodName(), getNameOfJavaType(JavaType.ARRAYS), childEntityVar1, childEntityVar2);
bodyBuilder.newLine();
// Verify
bodyBuilder.appendFormalLine("// Verify");
// assertThat(entity.RELATION_FIELD_ACCESSOR()).as("Check 'ADD_METHOD_NAME' adds the FIELDNAME to the relationship")
JavaSymbolName relationFieldAccessorName = BeanInfoUtils.getAccessorMethodName(relationField);
bodyBuilder.appendFormalLine("%s(%s.%s()).as(\"Check '%s' adds the %s to the relationship\")", getNameOfJavaType(new JavaType("org.assertj.core.api.Assertions.assertThat"), true), this.entityVar, relationFieldAccessorName, addMethod.getMethodName(), relationFieldName);
// .contains(childEntity1).contains(childEntity2);
bodyBuilder.indent();
bodyBuilder.appendFormalLine(".contains(%s).contains(%s);", childEntityVar1, childEntityVar2);
bodyBuilder.indentRemove();
// assertThat(entity).as("Check 'ADD_METHOD_NAME' updates the CHILD_ENTITY relationship side")
bodyBuilder.appendFormalLine("%s(%s).as(\"Check '%s' updates the %s relationship side\")", getNameOfJavaType(new JavaType("org.assertj.core.api.Assertions.assertThat"), true), this.entityVar, addMethod.getMethodName(), getNameOfJavaType(childEntity));
// .isEqualTo(childEntity1.PARENT_ENTITY_ACCESSOR()).isEqualTo(childEntity2.PARENT_ENTITY_ACCESSOR());
JavaSymbolName parentEntityAccessor = BeanInfoUtils.getAccessorMethodName(new JavaSymbolName(relationInfo.mappedBy), this.entity);
bodyBuilder.indent();
bodyBuilder.appendFormalLine(".isEqualTo(%1$s.%3$s()).isEqualTo(%2$s.%3$s());", childEntityVar1, childEntityVar2, parentEntityAccessor);
bodyBuilder.reset();
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(this.getId(), Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, bodyBuilder);
// Add @Test
methodBuilder.addAnnotation(new AnnotationMetadataBuilder(TEST));
// Add throws types
methodBuilder.addThrowsType(JdkJavaType.EXCEPTION);
return methodBuilder.build();
}
use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.
the class JpaDataOnDemandMetadata method getDataField.
/**
* @return the "data" field to use, which is either provided by the user or
* produced on demand (never returns null)
*/
private FieldMetadataBuilder getDataField() {
final List<JavaType> parameterTypes = Arrays.asList(entity);
final JavaType listType = new JavaType(LIST.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, parameterTypes);
int index = -1;
while (true) {
// Compute the required field name
index++;
// The type parameters to be used by the field type
final JavaSymbolName fieldName = new JavaSymbolName("data" + StringUtils.repeat("_", index));
dataFieldName = fieldName;
final FieldMetadata candidate = governorTypeDetails.getField(fieldName);
if (candidate != null) {
// Verify if candidate is suitable
if (!Modifier.isPrivate(candidate.getModifier())) {
// next possible name)
continue;
}
if (!candidate.getFieldType().equals(listType)) {
// The equals method also verifies type params are present
continue;
}
// we assume the user knows what they're doing and have made one
return new FieldMetadataBuilder(candidate);
}
// Candidate not found, so let's create one
FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(getId(), Modifier.PRIVATE, new ArrayList<AnnotationMetadataBuilder>(), fieldName, listType);
CommentStructure comment = new CommentStructure();
comment.addComment(new JavadocComment("List of created entities."), CommentLocation.BEGINNING);
fieldBuilder.setCommentStructure(comment);
return fieldBuilder;
}
}
use of org.springframework.roo.classpath.details.FieldMetadata 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.details.FieldMetadata in project spring-roo by spring-projects.
the class JavaBeanMetadataProviderImpl method getMetadata.
@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
final JavaBeanAnnotationValues annotationValues = new JavaBeanAnnotationValues(governorPhysicalTypeMetadata);
if (!annotationValues.isAnnotationFound()) {
return null;
}
ClassOrInterfaceTypeDetails currentClassDetails = governorPhysicalTypeMetadata.getMemberHoldingTypeDetails();
final Map<FieldMetadata, JavaSymbolName> declaredFields = new LinkedHashMap<FieldMetadata, JavaSymbolName>();
final List<FieldMetadata> declaredFieldsList = new ArrayList<FieldMetadata>();
for (final FieldMetadata field : currentClassDetails.getDeclaredFields()) {
declaredFields.put(field, getIdentifierAccessorMethodName(field, metadataIdentificationString));
declaredFieldsList.add(field);
}
// In order to handle switching between GAE and JPA produced MIDs need
// to be remembered so they can be regenerated on JPA <-> GAE switch
producedMids.add(metadataIdentificationString);
// Getting implements
List<JavaType> interfaces = currentClassDetails.getImplementsTypes();
List<? extends MethodMetadata> interfaceMethods = null;
if (!interfaces.isEmpty()) {
for (JavaType currentInterface : interfaces) {
ClassOrInterfaceTypeDetails currentInterfaceDetails = getTypeLocationService().getTypeDetails(currentInterface);
if (currentInterfaceDetails != null) {
interfaceMethods = currentInterfaceDetails.getDeclaredMethods();
}
}
}
final JavaType target = governorPhysicalTypeMetadata.getType();
// Prepare Equals
final EqualsAnnotationValues equalsAnnotationValues = new EqualsAnnotationValues(governorPhysicalTypeMetadata);
List<FieldMetadata> equalsFields = null;
FieldMetadata identifier = null;
if (equalsAnnotationValues.isAnnotationFound()) {
EqualsMetadataProvider equalsProvider = getEqualsMetadataProvider();
equalsFields = equalsProvider.locateFields(target, equalsAnnotationValues.getExcludeFields(), declaredFieldsList, metadataIdentificationString);
identifier = equalsProvider.getIdentifier(governorPhysicalTypeMetadata);
}
// Prepare ToString
final ToStringAnnotationValues toStringAnnotationValues = new ToStringAnnotationValues(governorPhysicalTypeMetadata);
List<FieldMetadata> toStringFields = null;
if (toStringAnnotationValues.isAnnotationFound()) {
ToStringMetadataProvider toStringProvider = getToStringMetadataProvider();
toStringFields = toStringProvider.getToStringFields(governorPhysicalTypeMetadata, declaredFieldsList);
}
return new JavaBeanMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, annotationValues, declaredFields, interfaceMethods, getMemberDetailsScanner(), equalsAnnotationValues, equalsFields, identifier, toStringAnnotationValues, toStringFields);
}
Aggregations