use of org.springframework.roo.classpath.details.annotations.StringAttributeValue in project spring-roo by spring-projects.
the class AnnotationMetadataUtils method computeAttributeValue.
private static String computeAttributeValue(final AnnotationAttributeValue<?> value, final ImportRegistrationResolver resolver) {
String attributeValue = null;
if (value instanceof BooleanAttributeValue) {
attributeValue = ((BooleanAttributeValue) value).getValue().toString();
} else if (value instanceof CharAttributeValue) {
attributeValue = "'" + ((CharAttributeValue) value).getValue().toString() + "'";
} else if (value instanceof ClassAttributeValue) {
final JavaType clazz = ((ClassAttributeValue) value).getValue();
if (resolver == null || resolver.isFullyQualifiedFormRequiredAfterAutoImport(clazz)) {
attributeValue = clazz.getFullyQualifiedTypeName() + ".class";
} else {
attributeValue = clazz.getSimpleTypeName() + ".class";
}
} else if (value instanceof DoubleAttributeValue) {
final DoubleAttributeValue dbl = (DoubleAttributeValue) value;
if (dbl.isFloatingPrecisionOnly()) {
attributeValue = dbl.getValue().toString() + "F";
} else {
attributeValue = dbl.getValue().toString() + "D";
}
} else if (value instanceof EnumAttributeValue) {
final EnumDetails enumDetails = ((EnumAttributeValue) value).getValue();
final JavaType clazz = enumDetails.getType();
if (resolver == null || resolver.isFullyQualifiedFormRequiredAfterAutoImport(clazz)) {
attributeValue = clazz.getFullyQualifiedTypeName() + "." + enumDetails.getField().getSymbolName();
} else {
attributeValue = clazz.getSimpleTypeName() + "." + enumDetails.getField().getSymbolName();
}
} else if (value instanceof IntegerAttributeValue) {
attributeValue = ((IntegerAttributeValue) value).getValue().toString();
} else if (value instanceof LongAttributeValue) {
attributeValue = ((LongAttributeValue) value).getValue().toString() + "L";
} else if (value instanceof StringAttributeValue) {
attributeValue = "\"" + ((StringAttributeValue) value).getValue() + "\"";
} else if (value instanceof NestedAnnotationAttributeValue) {
final AnnotationMetadata annotationMetadata = ((NestedAnnotationAttributeValue) value).getValue();
final StringBuilder data = new StringBuilder("@");
final JavaType annotationType = annotationMetadata.getAnnotationType();
if (resolver == null || resolver.isFullyQualifiedFormRequiredAfterAutoImport(annotationType)) {
data.append(annotationType.getFullyQualifiedTypeName());
} else {
data.append(annotationType.getSimpleTypeName());
}
if (!annotationMetadata.getAttributeNames().isEmpty()) {
data.append("(");
int i = 0;
for (final JavaSymbolName attributeName : annotationMetadata.getAttributeNames()) {
i++;
if (i > 1) {
data.append(", ");
}
data.append(attributeName.getSymbolName()).append(" = ");
data.append(computeAttributeValue(annotationMetadata.getAttribute(attributeName), resolver));
}
data.append(")");
}
attributeValue = data.toString();
} else if (value instanceof ArrayAttributeValue<?>) {
final ArrayAttributeValue<?> array = (ArrayAttributeValue<?>) value;
final StringBuilder data = new StringBuilder("{ ");
int i = 0;
for (final AnnotationAttributeValue<?> val : array.getValue()) {
i++;
if (i > 1) {
data.append(", ");
}
data.append(computeAttributeValue(val, resolver));
}
data.append(" }");
attributeValue = data.toString();
}
return attributeValue;
}
use of org.springframework.roo.classpath.details.annotations.StringAttributeValue in project spring-roo by spring-projects.
the class ReferenceField method setJoinAnnotations.
/**
* Fill {@link #joinTableAttributes} for building @JoinTable annotation. The annotation
* would have some nested @JoinColumn annotations in each of its "joinColumns" and
* "inverseJoinColumns" attributes.
*
* @param joinTableName
* @param joinColumns
* @param referencedColumns
* @param inverseJoinColumns
* @param inverseReferencedColumns
*/
public void setJoinAnnotations(String joinTableName, String[] joinColumns, String[] referencedColumns, String[] inverseJoinColumns, String[] inverseReferencedColumns) {
final List<AnnotationMetadataBuilder> joinColumnsBuilders = new ArrayList<AnnotationMetadataBuilder>();
if (joinColumns != null) {
// Build joinColumns attribute
for (int i = 0; i < joinColumns.length; i++) {
// Build @JoinColumn annotation for owner side of the relation
final AnnotationMetadataBuilder joinColumnAnnotation = new AnnotationMetadataBuilder(JOIN_COLUMN);
joinColumnAnnotation.addStringAttribute("name", joinColumns[i]);
if (referencedColumns != null) {
joinColumnAnnotation.addStringAttribute("referencedColumnName", referencedColumns[i]);
}
joinColumnsBuilders.add(joinColumnAnnotation);
}
}
final List<AnnotationMetadataBuilder> inverseJoinColumnsBuilders = new ArrayList<AnnotationMetadataBuilder>();
if (inverseJoinColumns != null) {
// Build inverseJoinColumns attribute
for (int i = 0; i < inverseJoinColumns.length; i++) {
// Build @JoinColumn annotation for the not owner side of the relation
final AnnotationMetadataBuilder inverseJoinColumnsAnnotation = new AnnotationMetadataBuilder(JOIN_COLUMN);
inverseJoinColumnsAnnotation.addStringAttribute("name", inverseJoinColumns[i]);
inverseJoinColumnsAnnotation.addStringAttribute("referencedColumnName", inverseReferencedColumns[i]);
inverseJoinColumnsBuilders.add(inverseJoinColumnsAnnotation);
}
}
if (StringUtils.isNotBlank(joinTableName) || !inverseJoinColumnsBuilders.isEmpty()) {
// add @JoinTable annotation
// Add attributes for @JoinTable annotation
final List<AnnotationAttributeValue<?>> joinTableAttributes = new ArrayList<AnnotationAttributeValue<?>>();
// If name not specified, use default name value
joinTableAttributes.add(new StringAttributeValue(new JavaSymbolName("name"), joinTableName));
// If joinColumns options were not specified, use default @JoinColumn values
if (joinColumns != null) {
final List<AnnotationAttributeValue<?>> joinColumnsAnnotations = new ArrayList<AnnotationAttributeValue<?>>();
for (AnnotationMetadataBuilder joinColumnAnnotation : joinColumnsBuilders) {
joinColumnsAnnotations.add(new NestedAnnotationAttributeValue(new JavaSymbolName("joinColumns"), joinColumnAnnotation.build()));
}
joinTableAttributes.add(new ArrayAttributeValue<AnnotationAttributeValue<?>>(new JavaSymbolName("joinColumns"), joinColumnsAnnotations));
}
// If inverseJoinColumns options were not specified, use default @JoinColumn values
if (inverseJoinColumns != null) {
final List<AnnotationAttributeValue<?>> inverseJoinColumnsAnnotations = new ArrayList<AnnotationAttributeValue<?>>();
for (AnnotationMetadataBuilder inverseJoinColumnsAnnotation : inverseJoinColumnsBuilders) {
inverseJoinColumnsAnnotations.add(new NestedAnnotationAttributeValue(new JavaSymbolName("inverseJoinColumns"), inverseJoinColumnsAnnotation.build()));
}
joinTableAttributes.add(new ArrayAttributeValue<AnnotationAttributeValue<?>>(new JavaSymbolName("inverseJoinColumns"), inverseJoinColumnsAnnotations));
}
// Add @JoinTable to additonalAnnotations
additionaAnnotations.add(new AnnotationMetadataBuilder(JOIN_TABLE, joinTableAttributes));
} else if (!joinColumnsBuilders.isEmpty()) {
if (joinColumnsBuilders.size() == 1) {
// Just one @JoinColumn
additionaAnnotations.add(joinColumnsBuilders.iterator().next());
} else {
// Multiple @JoinColumn, wrap with @JoinColumns
final AnnotationMetadataBuilder joinColumnsAnnotation = new AnnotationMetadataBuilder(JOIN_COLUMNS);
final List<AnnotationAttributeValue<?>> joinColumnsAnnotations = new ArrayList<AnnotationAttributeValue<?>>();
for (AnnotationMetadataBuilder joinColumnAnnotation : joinColumnsBuilders) {
joinColumnsAnnotations.add(new NestedAnnotationAttributeValue(new JavaSymbolName("value"), joinColumnAnnotation.build()));
}
joinColumnsAnnotation.addAttribute(new ArrayAttributeValue<AnnotationAttributeValue<?>>(new JavaSymbolName("value"), joinColumnsAnnotations));
// Add @JoinColumns
additionaAnnotations.add(joinColumnsAnnotation);
}
}
}
use of org.springframework.roo.classpath.details.annotations.StringAttributeValue in project spring-roo by spring-projects.
the class ReferenceField method decorateAnnotationsList.
@Override
public void decorateAnnotationsList(final List<AnnotationMetadataBuilder> annotations) {
super.decorateAnnotationsList(annotations);
final List<AnnotationAttributeValue<?>> attributes = new ArrayList<AnnotationAttributeValue<?>>();
// Add cascade if option exists
if (cascadeType != null) {
List<EnumAttributeValue> cascadeValues = new ArrayList<EnumAttributeValue>();
for (Cascade type : cascadeType) {
cascadeValues.add(new EnumAttributeValue(new JavaSymbolName("cascade"), new EnumDetails(CASCADE_TYPE, new JavaSymbolName(type.name()))));
}
attributes.add(new ArrayAttributeValue<EnumAttributeValue>(new JavaSymbolName("cascade"), cascadeValues));
}
// Add orphanRemoval if option exists
if (getOrphanRemoval() != null) {
attributes.add(new BooleanAttributeValue(new JavaSymbolName("orphanRemoval"), getOrphanRemoval().booleanValue()));
}
if (fetch != null) {
JavaSymbolName value = new JavaSymbolName("EAGER");
if (fetch == Fetch.LAZY) {
value = new JavaSymbolName("LAZY");
}
attributes.add(new EnumAttributeValue(new JavaSymbolName("fetch"), new EnumDetails(FETCH_TYPE, value)));
}
if (mappedBy != null) {
attributes.add(new StringAttributeValue(new JavaSymbolName("mappedBy"), mappedBy.getSymbolName()));
}
switch(cardinality) {
case ONE_TO_MANY:
annotations.add(new AnnotationMetadataBuilder(ONE_TO_MANY, attributes));
break;
case MANY_TO_MANY:
annotations.add(new AnnotationMetadataBuilder(MANY_TO_MANY, attributes));
break;
case ONE_TO_ONE:
annotations.add(new AnnotationMetadataBuilder(ONE_TO_ONE, attributes));
break;
default:
annotations.add(new AnnotationMetadataBuilder(MANY_TO_ONE, attributes));
break;
}
// Add additional annotations (if any)
if (additionaAnnotations != null) {
annotations.addAll(additionaAnnotations);
}
}
use of org.springframework.roo.classpath.details.annotations.StringAttributeValue in project spring-roo by spring-projects.
the class StringField method decorateAnnotationsList.
@Override
public void decorateAnnotationsList(final List<AnnotationMetadataBuilder> annotations) {
super.decorateAnnotationsList(annotations);
if (sizeMin != null || sizeMax != null) {
final List<AnnotationAttributeValue<?>> attrs = new ArrayList<AnnotationAttributeValue<?>>();
if (sizeMin != null) {
attrs.add(new IntegerAttributeValue(new JavaSymbolName("min"), sizeMin));
}
if (sizeMax != null) {
attrs.add(new IntegerAttributeValue(new JavaSymbolName("max"), sizeMax));
}
annotations.add(new AnnotationMetadataBuilder(SIZE, attrs));
}
if (regexp != null) {
final List<AnnotationAttributeValue<?>> attrs = new ArrayList<AnnotationAttributeValue<?>>();
attrs.add(new StringAttributeValue(new JavaSymbolName("regexp"), regexp));
annotations.add(new AnnotationMetadataBuilder(PATTERN, attrs));
}
}
use of org.springframework.roo.classpath.details.annotations.StringAttributeValue in project spring-roo by spring-projects.
the class UploadedFileField method decorateAnnotationsList.
@Override
public void decorateAnnotationsList(final List<AnnotationMetadataBuilder> annotations) {
super.decorateAnnotationsList(annotations);
final List<AnnotationAttributeValue<?>> attrs = new ArrayList<AnnotationAttributeValue<?>>();
attrs.add(new StringAttributeValue(new JavaSymbolName("contentType"), contentType.getContentType()));
if (autoUpload) {
attrs.add(new BooleanAttributeValue(new JavaSymbolName("autoUpload"), autoUpload));
}
annotations.add(new AnnotationMetadataBuilder(ROO_UPLOADED_FILE, attrs));
annotations.add(new AnnotationMetadataBuilder(LOB));
}
Aggregations