use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.
the class ToStringMetadata method generateToStringMethod.
/**
* Generates toString method
*
* @param metadataId
* @param target
* @param annotationValues
* @param fields
* @return
*/
protected static MethodMetadataBuilder generateToStringMethod(String metadataId, JavaType target, ToStringAnnotationValues annotationValues, List<FieldMetadata> fields) {
final JavaSymbolName methodName = new JavaSymbolName(annotationValues.getToStringMethod());
// Get excludeFields attribute value
final String[] excludeFields = annotationValues.getExcludeFields();
// Get all fields from class
List<FieldMetadata> affectedFields = new ArrayList<FieldMetadata>();
for (FieldMetadata field : fields) {
// Check if field must be excluded manually by "excludeFields" attribute
if (Modifier.isStatic(field.getModifier()) || !isCommonJavaField(field) || isCollectionField(field) || isRelationField(field) || isExcluded(excludeFields, field)) {
continue;
}
affectedFields.add(field);
}
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// Build toString method body
// return "Entity {" + "fieldName1='" + fieldName1 + '\'' + ", fieldName2='" + fieldName2 + '\''+ "}" + super.toString();
bodyBuilder.appendFormalLine(String.format("return \"%s {\" + ", target.getSimpleTypeName()));
for (int i = 0; i < affectedFields.size(); i++) {
bodyBuilder.appendIndent();
StringBuilder fieldString = new StringBuilder();
fieldString.append("\"");
if (i != 0) {
fieldString.append(", ");
}
FieldMetadata fieldMetadata = affectedFields.get(i);
String fieldName = fieldMetadata.getFieldName().getSymbolName();
String fieldValue = fieldName;
if (isDateField(fieldMetadata)) {
fieldValue = fieldName + " == null ? null : java.text.DateFormat.getDateTimeInstance().format(" + fieldName + ")";
} else if (isCalendarField(fieldMetadata)) {
fieldValue = fieldName + " == null ? null : java.text.DateFormat.getDateTimeInstance().format(" + fieldName + ".getTime())";
}
fieldString.append(fieldName).append("='\"").append(" + ").append(fieldValue).append(" + '\\''").append(" + ");
if (i == affectedFields.size() - 1) {
fieldString.append("\"}\" + ").append("super.toString();");
}
// Append next field line
bodyBuilder.appendFormalLine(fieldString.toString());
}
if (affectedFields.isEmpty()) {
bodyBuilder.appendFormalLine("\"}\" + super.toString();");
}
return new MethodMetadataBuilder(metadataId, Modifier.PUBLIC, methodName, STRING, bodyBuilder);
}
use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.
the class JpaOperationsImpl method getFieldChildPartOfCompositionRelation.
@Override
public Pair<FieldMetadata, RelationInfo> getFieldChildPartOfCompositionRelation(ClassOrInterfaceTypeDetails entityCdi) {
JavaType domainType = entityCdi.getType();
List<Pair<FieldMetadata, RelationInfo>> relations = getFieldChildPartOfRelation(entityCdi);
if (relations.isEmpty()) {
return null;
}
JpaEntityMetadata parent;
JavaType parentType;
RelationInfo info;
List<Pair<FieldMetadata, RelationInfo>> compositionRelation = new ArrayList<Pair<FieldMetadata, RelationInfo>>();
for (Pair<FieldMetadata, RelationInfo> field : relations) {
if (field.getRight().type == JpaRelationType.COMPOSITION) {
compositionRelation.add(field);
}
}
Validate.isTrue(compositionRelation.size() <= 1, "Entity %s has more than one relations of composition as child part: ", domainType, StringUtils.join(getFieldNamesOfRelationList(compositionRelation), ","));
if (compositionRelation.isEmpty()) {
return null;
}
return compositionRelation.get(0);
}
use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.
the class EqualsMetadataProviderImpl method getMetadata.
@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
final EqualsAnnotationValues annotationValues = new EqualsAnnotationValues(governorPhysicalTypeMetadata);
if (!annotationValues.isAnnotationFound()) {
return null;
}
final MemberDetails memberDetails = getMemberDetails(governorPhysicalTypeMetadata);
if (memberDetails == null) {
return null;
}
AnnotationMetadata javaBeanAnnotation = memberDetails.getAnnotation(ROO_JAVA_BEAN);
if (javaBeanAnnotation != null) {
// Return an empty metadata as @RooJavaBean do the work
return new EqualsMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, annotationValues, new ArrayList<FieldMetadata>(), null, true);
}
final JavaType javaType = governorPhysicalTypeMetadata.getMemberHoldingTypeDetails().getName();
final List<FieldMetadata> equalityFields = locateFields(javaType, annotationValues.getExcludeFields(), memberDetails.getFields(), metadataIdentificationString);
FieldMetadata identifierField = getIdentifier(governorPhysicalTypeMetadata);
return new EqualsMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, annotationValues, equalityFields, identifierField, false);
}
use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.
the class EqualsMetadataProviderImpl method locateFields.
/**
* Locates class fields needed to generate `equals` and `hashCode` methods.
* Also fills id field info if class is an entity.
*
* @param javaType the {@link JavaType} of the governor class.
* @param excludeFields the {@link String[]} with field names to exclude.
* @param memberDetails the {@link MemberDetails} of the class.
* @param metadataIdentificationString
* @return a {@link List<FieldMetadata>}
*/
public List<FieldMetadata> locateFields(final JavaType javaType, final String[] excludeFields, final List<FieldMetadata> fields, final String metadataIdentificationString) {
final SortedSet<FieldMetadata> locatedFields = new TreeSet<FieldMetadata>(new Comparator<FieldMetadata>() {
public int compare(final FieldMetadata l, final FieldMetadata r) {
return l.getFieldName().compareTo(r.getFieldName());
}
});
final List<?> excludeFieldsList = CollectionUtils.arrayToList(excludeFields);
final FieldMetadata versionField = getPersistenceMemberLocator().getVersionField(javaType);
for (final FieldMetadata field : fields) {
if (excludeFieldsList.contains(field.getFieldName().getSymbolName())) {
continue;
}
if (Modifier.isStatic(field.getModifier()) || Modifier.isTransient(field.getModifier()) || field.getFieldType().isCommonCollectionType() || field.getFieldType().isArray()) {
continue;
}
if (versionField != null && field.getFieldName().equals(versionField.getFieldName())) {
continue;
}
locatedFields.add(field);
getMetadataDependencyRegistry().registerDependency(field.getDeclaredByMetadataId(), metadataIdentificationString);
}
return new ArrayList<FieldMetadata>(locatedFields);
}
use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.
the class ToStringMetadataProviderImpl method getToStringFields.
/* (non-Javadoc)
* @see org.springframework.roo.addon.javabean.addon.ToStringMetadataProvider#getToStringFields(org.springframework.roo.classpath.PhysicalTypeMetadata, java.util.List)
*/
public List<FieldMetadata> getToStringFields(final PhysicalTypeMetadata governorPhysicalTypeMetadata, final List<FieldMetadata> declaredFields) {
ClassOrInterfaceTypeDetails superclass = governorPhysicalTypeMetadata.getMemberHoldingTypeDetails().getSuperclass();
List<FieldMetadata> toStringFields = new ArrayList<FieldMetadata>();
if (superclass != null && superclass != JavaType.OBJECT) {
List<FieldMetadata> superclassFields = getMemberDetailsScanner().getMemberDetails(this.getClass().getName(), superclass).getFields();
for (FieldMetadata field : declaredFields) {
boolean alreadyInSuperclass = false;
for (FieldMetadata superclassField : superclassFields) {
if (superclassField.getDeclaredByMetadataId().equals(field.getDeclaredByMetadataId()) && superclassField.getFieldName().equals(field.getFieldName())) {
alreadyInSuperclass = true;
break;
}
}
if (!alreadyInSuperclass) {
toStringFields.add(field);
}
}
} else {
toStringFields.addAll(declaredFields);
}
return toStringFields;
}
Aggregations