use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.
the class JpaEntityFactoryMetadata method getMinAndMaxBody.
private String getMinAndMaxBody(final FieldMetadata field, final String suffix) {
final String fieldName = field.getFieldName().getSymbolName();
final JavaType fieldType = field.getFieldType();
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
final AnnotationMetadata minAnnotation = MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), MIN);
final AnnotationMetadata maxAnnotation = MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), MAX);
if (minAnnotation != null && maxAnnotation == null) {
final Number minValue = (Number) minAnnotation.getAttribute(VALUE).getValue();
if (fieldType.equals(BIG_INTEGER)) {
bodyBuilder.appendFormalLine("if (" + fieldName + ".compareTo(new " + BIG_INTEGER.getSimpleTypeName() + "(\"" + minValue + "\")) == -1) {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine(fieldName + " = new " + BIG_INTEGER.getSimpleTypeName() + "(\"" + minValue + "\");");
} else {
bodyBuilder.appendFormalLine("if (" + fieldName + " < " + minValue + suffix + ") {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine(fieldName + " = " + minValue + suffix + ";");
}
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
} else if (minAnnotation == null && maxAnnotation != null) {
final Number maxValue = (Number) maxAnnotation.getAttribute(VALUE).getValue();
if (fieldType.equals(BIG_INTEGER)) {
bodyBuilder.appendFormalLine("if (" + fieldName + ".compareTo(new " + BIG_INTEGER.getSimpleTypeName() + "(\"" + maxValue + "\")) == 1) {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine(fieldName + " = new " + BIG_INTEGER.getSimpleTypeName() + "(\"" + maxValue + "\");");
} else {
bodyBuilder.appendFormalLine("if (" + fieldName + " > " + maxValue + suffix + ") {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine(fieldName + " = " + maxValue + suffix + ";");
}
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
} else if (minAnnotation != null && maxAnnotation != null) {
final Number minValue = (Number) minAnnotation.getAttribute(VALUE).getValue();
final Number maxValue = (Number) maxAnnotation.getAttribute(VALUE).getValue();
Validate.isTrue(maxValue.longValue() >= minValue.longValue(), "The value of @Max must be greater or equal to the value of @Min for field %s", fieldName);
if (fieldType.equals(BIG_INTEGER)) {
bodyBuilder.appendFormalLine("if (" + fieldName + ".compareTo(new " + BIG_INTEGER.getSimpleTypeName() + "(\"" + minValue + "\")) == -1 || " + fieldName + ".compareTo(new " + BIG_INTEGER.getSimpleTypeName() + "(\"" + maxValue + "\")) == 1) {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine(fieldName + " = new " + BIG_INTEGER.getSimpleTypeName() + "(\"" + maxValue + "\");");
} else {
bodyBuilder.appendFormalLine("if (" + fieldName + " < " + minValue + suffix + " || " + fieldName + " > " + maxValue + suffix + ") {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine(fieldName + " = " + maxValue + suffix + ";");
}
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
}
return bodyBuilder.getOutput();
}
use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.
the class JpaEntityFactoryMetadata method getDigitsBody.
private String getDigitsBody(final FieldMetadata field, final AnnotationMetadata digitsAnnotation, final String suffix) {
final String fieldName = field.getFieldName().getSymbolName();
final JavaType fieldType = field.getFieldType();
final Integer integerValue = (Integer) digitsAnnotation.getAttribute(new JavaSymbolName("integer")).getValue();
final Integer fractionValue = (Integer) digitsAnnotation.getAttribute(new JavaSymbolName("fraction")).getValue();
final BigDecimal maxValue = new BigDecimal(StringUtils.rightPad("9", integerValue, '9') + "." + StringUtils.rightPad("9", fractionValue, '9'));
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
if (fieldType.equals(BIG_DECIMAL)) {
bodyBuilder.appendFormalLine("if (" + fieldName + ".compareTo(new " + BIG_DECIMAL.getSimpleTypeName() + "(\"" + maxValue + "\")) == 1) {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine(fieldName + " = new " + BIG_DECIMAL.getSimpleTypeName() + "(\"" + maxValue + "\");");
} else {
bodyBuilder.appendFormalLine("if (" + fieldName + " > " + maxValue.doubleValue() + suffix + ") {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine(fieldName + " = " + maxValue.doubleValue() + suffix + ";");
}
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
return bodyBuilder.getOutput();
}
use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.
the class JpaEntityFactoryMetadata method getColumnPrecisionAndScaleBody.
private String getColumnPrecisionAndScaleBody(final FieldMetadata field, final Map<String, Object> values, final String suffix) {
if (values == null || !values.containsKey("precision")) {
return InvocableMemberBodyBuilder.getInstance().getOutput();
}
final String fieldName = field.getFieldName().getSymbolName();
final JavaType fieldType = field.getFieldType();
Integer precision = (Integer) values.get("precision");
Integer scale = (Integer) values.get("scale");
if (precision != null && scale != null && precision < scale) {
scale = 0;
}
final BigDecimal maxValue;
if (scale == null || scale == 0) {
maxValue = new BigDecimal(StringUtils.rightPad("9", precision, '9'));
} else {
maxValue = new BigDecimal(StringUtils.rightPad("9", precision - scale, '9') + "." + StringUtils.rightPad("9", scale, '9'));
}
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
if (fieldType.equals(BIG_DECIMAL)) {
bodyBuilder.appendFormalLine("if (" + fieldName + ".compareTo(new " + BIG_DECIMAL.getSimpleTypeName() + "(\"" + maxValue + "\")) == 1) {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine(fieldName + " = new " + BIG_DECIMAL.getSimpleTypeName() + "(\"" + maxValue + "\");");
} else {
bodyBuilder.appendFormalLine("if (" + fieldName + " > " + maxValue.doubleValue() + suffix + ") {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine(fieldName + " = " + maxValue.doubleValue() + suffix + ";");
}
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
return bodyBuilder.getOutput();
}
use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.
the class JpaEntityFactoryMetadata method addEmbeddedClassFieldMutatorMethodsToBuilder.
private void addEmbeddedClassFieldMutatorMethodsToBuilder(final EmbeddedHolder embeddedHolder, final Set<ClassOrInterfaceTypeDetails> dataOnDemandClasses) {
final JavaType embeddedFieldType = embeddedHolder.getEmbeddedField().getFieldType();
final JavaType[] parameterTypes = { embeddedFieldType, JavaType.INT_PRIMITIVE };
final List<JavaSymbolName> parameterNames = Arrays.asList(OBJ_SYMBOL, INDEX_SYMBOL);
for (final FieldMetadata field : embeddedHolder.getFields()) {
final String initializer = getFieldInitializer(field, null, dataOnDemandClasses);
final JavaSymbolName fieldMutatorMethodName = BeanInfoUtils.getMutatorMethodName(field.getFieldName());
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
bodyBuilder.append(getFieldValidationBody(field, initializer, fieldMutatorMethodName, false));
final JavaSymbolName embeddedClassMethodName = getEmbeddedFieldMutatorMethodName(embeddedHolder.getEmbeddedField().getFieldName(), field.getFieldName());
if (governorHasMethod(embeddedClassMethodName, parameterTypes)) {
// Method found in governor so do not create method in ITD
continue;
}
builder.addMethod(new MethodMetadataBuilder(getId(), Modifier.PUBLIC, embeddedClassMethodName, JavaType.VOID_PRIMITIVE, AnnotatedJavaType.convertFromJavaTypes(parameterTypes), parameterNames, bodyBuilder));
}
}
use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.
the class IdentifierMetadata method getNoArgConstructor.
/**
* Locates the no-arg constructor for this class, if available.
* <p>
* If a class defines a no-arg constructor, it is returned (irrespective of
* access modifiers).
* <p>
* If a class does not define a no-arg constructor, one might be created. It
* will only be created if the {@link RooIdentifier#noArgConstructor} is
* true AND there is at least one other constructor declared in the source
* file. If a constructor is created, it will have a private access
* modifier.
*
* @return the constructor (may return null if no constructor is to be
* produced)
*/
private ConstructorMetadataBuilder getNoArgConstructor() {
// Search for an existing constructor
final List<JavaType> parameterTypes = new ArrayList<JavaType>();
final ConstructorMetadata result = governorTypeDetails.getDeclaredConstructor(parameterTypes);
if (result != null) {
// Found an existing no-arg constructor on this class
return null;
}
// Create the constructor
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
bodyBuilder.appendFormalLine("super();");
final ConstructorMetadataBuilder constructorBuilder = new ConstructorMetadataBuilder(getId());
constructorBuilder.setModifier(publicNoArgConstructor ? Modifier.PUBLIC : Modifier.PRIVATE);
constructorBuilder.setParameterTypes(AnnotatedJavaType.convertFromJavaTypes(parameterTypes));
constructorBuilder.setBodyBuilder(bodyBuilder);
return constructorBuilder;
}
Aggregations