use of org.springframework.roo.classpath.details.annotations.AnnotationMetadata 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.AnnotationMetadata in project spring-roo by spring-projects.
the class ItdSourceFileComposer method writeInnerTypeConstructors.
private void writeInnerTypeConstructors(final JavaType innerType, final List<? extends ConstructorMetadata> constructors, final boolean defineTarget, final boolean isInterfaceMethod) {
for (final ConstructorMetadata constructor : constructors) {
Validate.isTrue(constructor.getParameterTypes().size() == constructor.getParameterNames().size(), "One constructor has mismatched parameter names against parameter types");
// Append annotations
for (final AnnotationMetadata annotation : constructor.getAnnotations()) {
appendIndent();
outputAnnotation(annotation);
this.newLine(false);
}
// Append "<modifier> <methodName>" portion
appendIndent();
if (constructor.getModifier() != 0) {
append(Modifier.toString(constructor.getModifier()));
append(" ");
}
append(innerType.getSimpleTypeName());
// Append parameter types and names
append("(");
final List<AnnotatedJavaType> parameterTypes = constructor.getParameterTypes();
final List<JavaSymbolName> parameterNames = constructor.getParameterNames();
for (int i = 0; i < parameterTypes.size(); i++) {
final AnnotatedJavaType paramType = parameterTypes.get(i);
final JavaSymbolName paramName = parameterNames.get(i);
for (final AnnotationMetadata methodParameterAnnotation : paramType.getAnnotations()) {
outputAnnotation(methodParameterAnnotation);
append(" ");
}
append(paramType.getJavaType().getNameIncludingTypeParameters(false, resolver));
append(" ");
append(paramName.getSymbolName());
if (i < parameterTypes.size() - 1) {
append(", ");
}
}
// Add exceptions to be thrown
final List<JavaType> throwsTypes = constructor.getThrowsTypes();
if (throwsTypes.size() > 0) {
append(") throws ");
for (int i = 0; i < throwsTypes.size(); i++) {
append(throwsTypes.get(i).getNameIncludingTypeParameters(false, resolver));
if (throwsTypes.size() > i + 1) {
append(", ");
}
}
} else {
append(")");
}
if (isInterfaceMethod) {
append(";");
} else {
append(" {");
this.newLine(false);
// Add body
indent();
append(constructor.getBody());
indentRemove();
appendFormalLine("}");
}
this.newLine();
}
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadata in project spring-roo by spring-projects.
the class ItdSourceFileComposer method appendTypeAnnotations.
private void appendTypeAnnotations() {
final List<? extends AnnotationMetadata> typeAnnotations = itdTypeDetails.getAnnotations();
if (typeAnnotations == null || typeAnnotations.isEmpty()) {
return;
}
content = true;
for (final AnnotationMetadata typeAnnotation : typeAnnotations) {
appendIndent();
append("declare @type: ");
append(introductionTo.getSimpleTypeName());
append(": ");
outputAnnotation(typeAnnotation);
append(";");
this.newLine(false);
this.newLine();
}
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadata in project spring-roo by spring-projects.
the class ItdSourceFileComposer method writeMethods.
private void writeMethods(final List<? extends MethodMetadata> methods, final boolean defineTarget, final boolean isInterfaceMethod) {
for (final MethodMetadata method : methods) {
Validate.isTrue(method.getParameterTypes().size() == method.getParameterNames().size(), "Method %s has mismatched parameter names against parameter types", method.getMethodName().getSymbolName());
// ROO-3447: Append comments if exists
CommentStructure commentStructure = method.getCommentStructure();
if (commentStructure != null && commentStructure.getBeginComments() != null) {
List<AbstractComment> comments = commentStructure.getBeginComments();
String commentString = "";
boolean missingComponentsAdded = false;
for (AbstractComment comment : comments) {
// Join all JavadocComment's
if (comment instanceof JavadocComment) {
// Add JavaDoc missing components
if (!missingComponentsAdded) {
// Check params info
if (!method.getParameterNames().isEmpty() && ((JavadocComment) comment).getParamsInfo() == null) {
List<String> paramsInfo = new ArrayList<String>();
for (JavaSymbolName name : method.getParameterNames()) {
paramsInfo.add(name.getSymbolName());
}
((JavadocComment) comment).setParamsInfo(paramsInfo);
}
// Check return info
if (!method.getReturnType().equals(JavaType.VOID_OBJECT) && !method.getReturnType().equals(JavaType.VOID_PRIMITIVE) && ((JavadocComment) comment).getReturnInfo() == null) {
((JavadocComment) comment).setReturnInfo(method.getReturnType().getSimpleTypeName());
}
// Check throws info
if (!method.getThrowsTypes().isEmpty() && ((JavadocComment) comment).getThrowsInfo() == null) {
List<String> throwsInfo = new ArrayList<String>();
for (JavaType throwsType : method.getThrowsTypes()) {
throwsInfo.add(throwsType.getSimpleTypeName());
}
((JavadocComment) comment).setThrowsInfo(throwsInfo);
}
missingComponentsAdded = true;
}
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,
// including params, return and throws
CommentStructure defaultCommentStructure = new CommentStructure();
List<String> parameterNames = new ArrayList<String>();
for (JavaSymbolName name : method.getParameterNames()) {
parameterNames.add(name.getSymbolName());
}
List<String> throwsTypesNames = new ArrayList<String>();
for (JavaType type : method.getThrowsTypes()) {
throwsTypesNames.add(type.getSimpleTypeName());
}
String returnInfo = null;
JavaType returnType = method.getReturnType();
if (!returnType.equals(JavaType.VOID_OBJECT) && !returnType.equals(JavaType.VOID_PRIMITIVE)) {
returnInfo = returnType.getSimpleTypeName();
}
JavadocComment javadocComment = new JavadocComment("TODO Auto-generated method documentation", parameterNames, returnInfo, throwsTypesNames);
defaultCommentStructure.addComment(javadocComment, CommentLocation.BEGINNING);
method.setCommentStructure(defaultCommentStructure);
// 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 : method.getAnnotations()) {
appendIndent();
outputAnnotation(annotation);
this.newLine(false);
}
// Append "<modifier> <genericDefinition> <returnType> <methodName>" portion
appendIndent();
// modifier
if (method.getModifier() != 0) {
append(Modifier.toString(method.getModifier()));
append(" ");
}
// ROO-3648: genericDefinition
if (method.getGenericDefinition() != null && !method.getGenericDefinition().trim().equals("")) {
append("<".concat(method.getGenericDefinition()).concat(">"));
append(" ");
}
// return type
append(method.getReturnType().getNameIncludingTypeParameters(false, resolver));
append(" ");
if (defineTarget) {
append(introductionTo.getSimpleTypeName());
append(".");
}
append(method.getMethodName().getSymbolName());
// Append parameter types and names
append("(");
final List<AnnotatedJavaType> parameterTypes = method.getParameterTypes();
final List<JavaSymbolName> parameterNames = method.getParameterNames();
for (int i = 0; i < parameterTypes.size(); i++) {
final AnnotatedJavaType paramType = parameterTypes.get(i);
final JavaSymbolName paramName = parameterNames.get(i);
for (final AnnotationMetadata methodParameterAnnotation : paramType.getAnnotations()) {
outputAnnotation(methodParameterAnnotation);
append(" ");
}
append(paramType.getJavaType().getNameIncludingTypeParameters(false, resolver));
append(" ");
append(paramName.getSymbolName());
if (i < parameterTypes.size() - 1) {
append(", ");
}
}
// Add exceptions to be thrown
final List<JavaType> throwsTypes = method.getThrowsTypes();
if (throwsTypes.size() > 0) {
append(") throws ");
for (int i = 0; i < throwsTypes.size(); i++) {
append(throwsTypes.get(i).getNameIncludingTypeParameters(false, resolver));
if (throwsTypes.size() > i + 1) {
append(", ");
}
}
} else {
append(")");
}
if (isInterfaceMethod) {
append(";");
this.newLine(false);
} else {
append(" {");
this.newLine(false);
// Add body
indent();
append(method.getBody());
indentRemove();
appendFormalLine("}");
}
this.newLine();
}
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadata in project spring-roo by spring-projects.
the class FieldMatcher method getAttributeMap.
private Map<String, Object> getAttributeMap(final FieldMetadata field) {
final Map<String, Object> map = new HashMap<String, Object>();
final AnnotationMetadata annotationMetadata = getMatchingAnnotation(field);
if (annotationMetadata != null) {
for (final JavaSymbolName attributeName : annotationMetadata.getAttributeNames()) {
map.put(attributeName.getSymbolName(), annotationMetadata.getAttribute(attributeName).getValue());
}
}
return map;
}
Aggregations