use of org.springframework.roo.classpath.details.annotations.AnnotationMetadata in project spring-roo by spring-projects.
the class ItdSourceFileComposer method appendFields.
private void appendFields() {
final List<? extends FieldMetadata> fields = itdTypeDetails.getDeclaredFields();
if (fields == null || fields.isEmpty()) {
return;
}
content = true;
for (final FieldMetadata field : fields) {
// ROO-3447: Append comments if exists
CommentStructure commentStructure = field.getCommentStructure();
if (commentStructure != null && commentStructure.getBeginComments() != null) {
List<AbstractComment> comments = commentStructure.getBeginComments();
String commentString = "";
for (AbstractComment comment : comments) {
// Join all JavadocComment's
if (comment instanceof JavadocComment) {
commentString = commentString.concat(comment.getComment()).concat(IOUtils.LINE_SEPARATOR);
} else {
// Not JavadocComment, write comment as it is, unchanged
appendFormalLine(comment.getComment());
}
}
// Write JavadocComment's to ITD, in a single JavadocComment instance
String[] commentLines = commentString.split(IOUtils.LINE_SEPARATOR);
for (String commentLine : commentLines) {
appendFormalLine(commentLine);
}
} else {
// ROO-3834: Append default Javadoc if not exists a comment structure
JavadocComment javadocComment = new JavadocComment("TODO Auto-generated attribute documentation");
// Now lines should be formatted, so write them
String[] comment = javadocComment.getComment().split(IOUtils.LINE_SEPARATOR);
for (String line : comment) {
appendFormalLine(line);
}
}
// Append annotations
for (final AnnotationMetadata annotation : field.getAnnotations()) {
appendIndent();
outputAnnotation(annotation);
this.newLine(false);
}
// Append "<modifier> <fieldType> <fieldName>" portion
appendIndent();
if (field.getModifier() != 0) {
append(Modifier.toString(field.getModifier()));
append(" ");
}
append(field.getFieldType().getNameIncludingTypeParameters(false, resolver));
append(" ");
append(introductionTo.getSimpleTypeName());
append(".");
append(field.getFieldName().getSymbolName());
// Append initializer, if present
if (field.getFieldInitializer() != null) {
append(" = ");
append(field.getFieldInitializer());
}
// Complete the field declaration
append(";");
this.newLine(false);
this.newLine();
}
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadata in project spring-roo by spring-projects.
the class AbstractIdentifiableAnnotatedJavaStructureBuilder method updateTypeAnnotation.
public boolean updateTypeAnnotation(final AnnotationMetadata annotation, final Set<JavaSymbolName> attributesToDeleteIfPresent) {
boolean hasChanged = false;
// We are going to build a replacement AnnotationMetadata.
// This variable tracks the new attribute values the replacement will
// hold.
final Map<JavaSymbolName, AnnotationAttributeValue<?>> replacementAttributeValues = new LinkedHashMap<JavaSymbolName, AnnotationAttributeValue<?>>();
final AnnotationMetadataBuilder existingBuilder = getDeclaredTypeAnnotation(annotation.getAnnotationType());
if (existingBuilder == null) {
// Not already present, so just go and add it
for (final JavaSymbolName incomingAttributeName : annotation.getAttributeNames()) {
// attributesToDeleteIfPresent Set
if (attributesToDeleteIfPresent == null || !attributesToDeleteIfPresent.contains(incomingAttributeName)) {
final AnnotationAttributeValue<?> incomingValue = annotation.getAttribute(incomingAttributeName);
replacementAttributeValues.put(incomingAttributeName, incomingValue);
}
}
final AnnotationMetadataBuilder replacement = new AnnotationMetadataBuilder(annotation.getAnnotationType(), new ArrayList<AnnotationAttributeValue<?>>(replacementAttributeValues.values()));
addAnnotation(replacement);
return true;
}
final AnnotationMetadata existing = existingBuilder.build();
// Copy the existing attributes into the new attributes
for (final JavaSymbolName existingAttributeName : existing.getAttributeNames()) {
if (attributesToDeleteIfPresent != null && attributesToDeleteIfPresent.contains(existingAttributeName)) {
hasChanged = true;
} else {
final AnnotationAttributeValue<?> existingValue = existing.getAttribute(existingAttributeName);
replacementAttributeValues.put(existingAttributeName, existingValue);
}
}
// Now we ensure every incoming attribute replaces the existing
for (final JavaSymbolName incomingAttributeName : annotation.getAttributeNames()) {
final AnnotationAttributeValue<?> incomingValue = annotation.getAttribute(incomingAttributeName);
// already present
if (replacementAttributeValues.keySet().contains(incomingAttributeName)) {
// There was already an attribute. Need to determine if this new
// attribute value is materially different
final AnnotationAttributeValue<?> existingValue = replacementAttributeValues.get(incomingAttributeName);
Validate.notNull(existingValue, "Existing value should have been provided by earlier loop");
if (!existingValue.equals(incomingValue)) {
replacementAttributeValues.put(incomingAttributeName, incomingValue);
hasChanged = true;
}
} else if (attributesToDeleteIfPresent == null || !attributesToDeleteIfPresent.contains(incomingAttributeName)) {
// This is a new attribute that does not already exist, so add
// it to the end of the replacement attributes
replacementAttributeValues.put(incomingAttributeName, incomingValue);
hasChanged = true;
}
}
// Were there any material changes?
if (!hasChanged) {
return false;
}
// Make a new AnnotationMetadata representing the replacement
final AnnotationMetadataBuilder replacement = new AnnotationMetadataBuilder(annotation.getAnnotationType(), new ArrayList<AnnotationAttributeValue<?>>(replacementAttributeValues.values()));
annotations.remove(existingBuilder);
addAnnotation(replacement);
return true;
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadata in project spring-roo by spring-projects.
the class AbstractIdentifiableAnnotatedJavaStructureBuilder method setAnnotations.
public final void setAnnotations(final Collection<AnnotationMetadata> annotations) {
final List<AnnotationMetadataBuilder> annotationBuilders = new ArrayList<AnnotationMetadataBuilder>();
for (final AnnotationMetadata annotationMetadata : annotations) {
annotationBuilders.add(new AnnotationMetadataBuilder(annotationMetadata));
}
setAnnotations(annotationBuilders);
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadata in project spring-roo by spring-projects.
the class LayerTypeMatcher method getTagValue.
@Override
public Object getTagValue(final MemberHoldingTypeDetails type) {
final AnnotationMetadata layerAnnotation = MemberFindingUtils.getAnnotationOfType(type.getAnnotations(), layerAnnotationType);
if (layerAnnotation == null || layerAnnotation.getAttribute(domainTypesAttribute) == null) {
return null;
}
final AnnotationAttributeValue<?> value = layerAnnotation.getAttribute(domainTypesAttribute);
final List<JavaType> domainTypes = new ArrayList<JavaType>();
if (value instanceof ClassAttributeValue) {
domainTypes.add(((ClassAttributeValue) value).getValue());
} else if (value instanceof ArrayAttributeValue<?>) {
final ArrayAttributeValue<?> castValue = (ArrayAttributeValue<?>) value;
for (final AnnotationAttributeValue<?> val : castValue.getValue()) {
if (val instanceof ClassAttributeValue) {
domainTypes.add(((ClassAttributeValue) val).getValue());
}
}
}
return domainTypes;
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadata in project spring-roo by spring-projects.
the class DtoOperationsImpl method addFieldsToProjection.
/**
* Removes persistence annotations of provided fields and adds them to a
* ClassOrInterfaceTypeDetailsBuilder representing a Projection in construction.
* Also adds final modifier to fields if required.
*
* @param projectionBuilder the ClassOrInterfaceTypeDetailsBuilder for building the
* Projection class.
* @param fieldsToAdd the List<FieldMetadata> to add.
*/
private void addFieldsToProjection(ClassOrInterfaceTypeDetailsBuilder projectionBuilder, Map<String, FieldMetadata> fieldsToAdd) {
Iterator<Entry<String, FieldMetadata>> iterator = fieldsToAdd.entrySet().iterator();
while (iterator.hasNext()) {
Entry<String, FieldMetadata> entry = iterator.next();
FieldMetadata field = entry.getValue();
// List and Set types require special management
FieldMetadataBuilder fieldBuilder = null;
FieldDetails fieldDetails = null;
if (field.getFieldType().getFullyQualifiedTypeName().equals("java.util.Set")) {
JavaType fieldType = field.getFieldType().getParameters().get(0);
fieldDetails = new SetField(projectionBuilder.getDeclaredByMetadataId(), new JavaType(JdkJavaType.SET.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Arrays.asList(fieldType)), field.getFieldName(), fieldType, null, null, true);
fieldBuilder = new FieldMetadataBuilder(fieldDetails);
fieldBuilder.setModifier(field.getModifier());
fieldBuilder.setAnnotations(field.getAnnotations());
} else if (field.getFieldType().getFullyQualifiedTypeName().equals("java.util.List")) {
JavaType fieldType = field.getFieldType().getParameters().get(0);
fieldDetails = new ListField(projectionBuilder.getDeclaredByMetadataId(), new JavaType(JdkJavaType.LIST.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Arrays.asList(fieldType)), field.getFieldName(), fieldType, null, null, true);
fieldBuilder = new FieldMetadataBuilder(fieldDetails);
fieldBuilder.setModifier(field.getModifier());
fieldBuilder.setAnnotations(field.getAnnotations());
} else {
// Can't just copy FieldMetadata because field.declaredByMetadataId will be the same
fieldBuilder = new FieldMetadataBuilder(projectionBuilder.getDeclaredByMetadataId(), field);
}
// Add dependency between modules
typeLocationService.addModuleDependency(projectionBuilder.getName().getModule(), field.getFieldType());
// Set new fieldName
fieldBuilder.setFieldName(new JavaSymbolName(entry.getKey()));
// If it is a CollectionField it needs an initializer
String initializer = null;
if (fieldDetails instanceof CollectionField) {
final CollectionField collectionField = (CollectionField) fieldDetails;
initializer = "new " + collectionField.getInitializer() + "()";
} else if (fieldDetails instanceof DateField && fieldDetails.getFieldName().getSymbolName().equals("created")) {
initializer = "new Date()";
}
fieldBuilder.setFieldInitializer(initializer);
// Remove persistence annotations
List<AnnotationMetadata> annotations = field.getAnnotations();
for (AnnotationMetadata annotation : annotations) {
if (annotation.getAnnotationType().getFullyQualifiedTypeName().contains("javax.persistence")) {
fieldBuilder.removeAnnotation(annotation.getAnnotationType());
} else if (annotation.getAnnotationType().getFullyQualifiedTypeName().startsWith("javax.validation")) {
// Add validation dependency
projectOperations.addDependency(projectionBuilder.getName().getModule(), new Dependency("javax.validation", "validation-api", null));
} else if (annotation.getAnnotationType().equals(RooJavaType.ROO_JPA_RELATION)) {
fieldBuilder.removeAnnotation(annotation.getAnnotationType());
}
}
fieldBuilder.setModifier(Modifier.PRIVATE);
// Build field
FieldMetadata projectionField = fieldBuilder.build();
// Add field to DTO
projectionBuilder.addField(projectionField);
}
}
Aggregations