use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.
the class JavaBeanMetadata method getEntityCollectionMutatorBody.
private InvocableMemberBodyBuilder getEntityCollectionMutatorBody(final FieldMetadata field, final JavaSymbolName entityIdsFieldName) {
final String entityCollectionName = field.getFieldName().getSymbolName();
final String entityIdsName = entityIdsFieldName.getSymbolName();
final JavaType collectionElementType = field.getFieldType().getParameters().get(0);
final String localEnitiesName = "local" + StringUtils.capitalize(entityCollectionName);
JavaType collectionType = field.getFieldType();
builder.getImportRegistrationResolver().addImport(collectionType);
final String collectionName = field.getFieldType().getNameIncludingTypeParameters().replace(field.getFieldType().getPackage().getFullyQualifiedPackageName() + ".", "");
String instantiableCollection = collectionName;
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().addImports(collectionType, LIST, ARRAY_LIST);
final String identifierMethodName = getIdentifierMethodName(field).getSymbolName();
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
bodyBuilder.appendFormalLine(collectionName + " " + localEnitiesName + " = new " + instantiableCollection + "();");
bodyBuilder.appendFormalLine("List<Long> longIds = new ArrayList<Long>();");
bodyBuilder.appendFormalLine("for (Key key : " + entityIdsName + ") {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine("if (!longIds.contains(key.getId())) {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine("longIds.add(key.getId());");
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
bodyBuilder.appendFormalLine("for (" + collectionElementType.getSimpleTypeName() + " entity : " + entityCollectionName + ") {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine("if (!longIds.contains(entity." + identifierMethodName + "())) {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine("longIds.add(entity." + identifierMethodName + "());");
bodyBuilder.appendFormalLine(entityIdsName + ".add(KeyFactory.createKey(" + collectionElementType.getSimpleTypeName() + ".class.getName(), entity." + identifierMethodName + "()));");
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
bodyBuilder.appendFormalLine(localEnitiesName + ".add(entity);");
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
bodyBuilder.appendFormalLine("this." + entityCollectionName + " = " + localEnitiesName + ";");
return bodyBuilder;
}
use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder 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.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.
the class EqualsMetadata method generateEqualsMethod.
/**
* Generate equals method
*
* @param metadataId
* @param target
* @param annotationValues
* @param identifierAccessor
* @param locatedFields
* @param builder
* @return
*/
protected static MethodMetadataBuilder generateEqualsMethod(String metadataId, JavaType target, EqualsAnnotationValues annotationValues, JavaSymbolName identifierAccessor, List<FieldMetadata> locatedFields, ItdTypeDetailsBuilder builder) {
final List<JavaSymbolName> parameterNames = Arrays.asList(new JavaSymbolName(OBJECT_NAME));
// Create the method body depending on destination class properties
InvocableMemberBodyBuilder bodyBuilder = null;
if (annotationValues.isJpaEntity()) {
bodyBuilder = getJpaEntityEqualsMethodBody(target, identifierAccessor, builder.getImportRegistrationResolver());
} else {
bodyBuilder = generateDefaultEqualsMethodBody(target, annotationValues.isAppendSuper(), locatedFields, builder.getImportRegistrationResolver());
}
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(metadataId, Modifier.PUBLIC, EQUALS_METHOD_NAME, BOOLEAN_PRIMITIVE, AnnotatedJavaType.convertFromJavaTypes(OBJECT), parameterNames, bodyBuilder);
if (annotationValues.isJpaEntity()) {
CommentStructure commentStructure = new CommentStructure();
commentStructure.addComment(new JavadocComment("This `equals` implementation is specific for JPA entities and uses ".concat(IOUtils.LINE_SEPARATOR).concat("the entity identifier for it, following the article in ").concat(IOUtils.LINE_SEPARATOR).concat("https://vladmihalcea.com/2016/06/06/how-to-implement-equals-and-hashcode-using-the-jpa-entity-identifier/")), CommentLocation.BEGINNING);
methodBuilder.setCommentStructure(commentStructure);
}
return methodBuilder;
}
use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.
the class JpaEntityMetadata method getAddValueMethod.
/**
* Create add method to handle referenced relation
*
* @param addMethodName
* @param field
* @param cardinality
* @param childType
* @param mappedBy
* @param removeMethodName
* @param importResolver
* @return method metadata or null if method already in class
*/
private MethodMetadata getAddValueMethod(final JavaSymbolName addMethodName, final FieldMetadata field, final Cardinality cardinality, final JavaType childType, final String mappedBy, final JavaSymbolName removeMethodName, ImportRegistrationResolver importResolver) {
// Identify parameters type and name
final List<JavaType> parameterTypes = new ArrayList<JavaType>(1);
final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>(1);
if (cardinality == Cardinality.ONE_TO_ONE) {
parameterTypes.add(childType);
parameterNames.add(field.getFieldName());
} else {
parameterTypes.add(JavaType.iterableOf(childType));
parameterNames.add(new JavaSymbolName(field.getFieldName().getSymbolName() + ADD_PARAMETER_SUFFIX));
}
// See if the type itself declared the method
MethodMetadata existingMethod = getGovernorMethod(addMethodName, parameterTypes);
if (existingMethod != null) {
return existingMethod;
}
final InvocableMemberBodyBuilder builder = new InvocableMemberBodyBuilder();
if (cardinality == Cardinality.ONE_TO_ONE) {
buildAddOneToOneBody(field, mappedBy, parameterNames.get(0), childType, removeMethodName, builder);
} else if (cardinality == Cardinality.ONE_TO_MANY) {
buildAddOneToManyBody(field, mappedBy, parameterNames.get(0), childType, builder, importResolver);
} else {
buildAddManyToManyBody(field, mappedBy, parameterNames.get(0), childType, builder, importResolver);
}
return new MethodMetadataBuilder(getId(), Modifier.PUBLIC, addMethodName, JavaType.VOID_PRIMITIVE, AnnotatedJavaType.convertFromJavaTypes(parameterTypes), parameterNames, builder).build();
}
use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.
the class JpaEntityMetadata method getRemoveMethod.
/**
* Create remove method to handle referenced relation
*
* @param removeMethodName
* @param field
* @param cardinality
* @param childType
* @param mappedBy
* @param importResolver
* @return method metadata or null if method already in class
*/
private MethodMetadata getRemoveMethod(final JavaSymbolName removeMethodName, final FieldMetadata field, final Cardinality cardinality, final JavaType childType, final String mappedBy, ImportRegistrationResolver importResolver) {
// Identify parameters types and names (if any)
final List<JavaType> parameterTypes = new ArrayList<JavaType>(1);
final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>(1);
if (cardinality != Cardinality.ONE_TO_ONE) {
parameterTypes.add(JavaType.iterableOf(childType));
parameterNames.add(new JavaSymbolName(field.getFieldName().getSymbolName() + REMOVE_PARAMETER_SUFFIX));
}
// See if the type itself declared the method
MethodMetadata existingMethod = getGovernorMethod(removeMethodName, parameterTypes);
if (existingMethod != null) {
return existingMethod;
}
final InvocableMemberBodyBuilder builder = new InvocableMemberBodyBuilder();
if (cardinality == Cardinality.ONE_TO_ONE) {
buildRemoveOneToOneBody(field, mappedBy, builder);
} else if (cardinality == Cardinality.ONE_TO_MANY) {
buildRemoveOneToManyBody(field, mappedBy, parameterNames.get(0), childType, builder, importResolver);
} else {
// ManyToMany
buildRemoveManyToManyBody(field, mappedBy, parameterNames.get(0), childType, builder, importResolver);
}
return new MethodMetadataBuilder(getId(), Modifier.PUBLIC, removeMethodName, JavaType.VOID_PRIMITIVE, AnnotatedJavaType.convertFromJavaTypes(parameterTypes), parameterNames, builder).build();
}
Aggregations