use of org.springframework.roo.model.JavaSymbolName 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);
}
use of org.springframework.roo.model.JavaSymbolName 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);
}
use of org.springframework.roo.model.JavaSymbolName 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.model.JavaSymbolName in project spring-roo by spring-projects.
the class JpaOperationsImpl method getVersionField.
/**
* This method generates the version field using the provided values
*
* @param entity
* @param versionField
* @param versionType
* @param versionColumn
* @return
*/
private FieldMetadata getVersionField(final JavaType entity, String versionField, final JavaType versionType, final String versionColumn) {
if (StringUtils.isEmpty(versionField)) {
versionField = "version";
}
final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
annotations.add(new AnnotationMetadataBuilder(VERSION));
if (StringUtils.isNotEmpty(versionColumn)) {
final AnnotationMetadataBuilder columnBuilder = new AnnotationMetadataBuilder(COLUMN);
columnBuilder.addStringAttribute("name", versionColumn);
annotations.add(columnBuilder);
}
FieldDetails versionFieldDetails = new FieldDetails(getTypeLocationService().getPhysicalTypeIdentifier(entity), versionType, new JavaSymbolName(versionField));
versionFieldDetails.setModifiers(Modifier.PRIVATE);
versionFieldDetails.addAnnotations(annotations);
return new FieldMetadataBuilder(versionFieldDetails).build();
}
use of org.springframework.roo.model.JavaSymbolName 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;
}
Aggregations