Skip to content

Commit

Permalink
endpoints - Fixed issue on annotation processing when annotation prov…
Browse files Browse the repository at this point in the history
…ides default value
  • Loading branch information
fcamblor committed Feb 10, 2018
1 parent 0745a78 commit b9e9cc4
Showing 1 changed file with 27 additions and 3 deletions.
Expand Up @@ -4,6 +4,8 @@
import com.google.common.base.Optional; import com.google.common.base.Optional;
import com.google.common.collect.*; import com.google.common.collect.*;
import com.samskivert.mustache.Template; import com.samskivert.mustache.Template;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Type;
import restx.RestxLogLevel; import restx.RestxLogLevel;
import restx.StdRestxRequestMatcher; import restx.StdRestxRequestMatcher;
import restx.annotations.*; import restx.annotations.*;
Expand Down Expand Up @@ -191,18 +193,40 @@ public String transformSingleValueToExpression(Object value, AnnotationField ann


private AnnotationDescription createAnnotationDescriptionFrom(AnnotationMirror methodAnnotation, ExecutableElement element) { private AnnotationDescription createAnnotationDescriptionFrom(AnnotationMirror methodAnnotation, ExecutableElement element) {
ImmutableList.Builder<AnnotationField> annotationFieldsBuilder = ImmutableList.builder(); ImmutableList.Builder<AnnotationField> annotationFieldsBuilder = ImmutableList.builder();
ImmutableSet.Builder<String> annotationFieldNamesBuilder = ImmutableSet.builder();
for(Map.Entry<? extends ExecutableElement,? extends AnnotationValue> fieldEntry: methodAnnotation.getElementValues().entrySet()) { for(Map.Entry<? extends ExecutableElement,? extends AnnotationValue> fieldEntry: methodAnnotation.getElementValues().entrySet()) {
String fieldName = fieldEntry.getKey().toString(); String fieldName = fieldEntry.getKey().toString().substring(0, fieldEntry.getKey().toString().length() - "()".length());
TypeMirror type = fieldEntry.getKey().getReturnType(); TypeMirror type = fieldEntry.getKey().getReturnType();
TypeMirror componentType = AnnotationFieldKind.componentTypeOf(type); TypeMirror componentType = AnnotationFieldKind.componentTypeOf(type);
boolean arrayed = AnnotationFieldKind.isArrayed(type); boolean arrayed = AnnotationFieldKind.isArrayed(type);
AnnotationFieldKind annotationFieldKind = AnnotationFieldKind.valueOf(processingEnv, type); AnnotationFieldKind annotationFieldKind = AnnotationFieldKind.valueOf(processingEnv, type);


annotationFieldsBuilder.add(new AnnotationField( annotationFieldsBuilder.add(new AnnotationField(
fieldName.substring(0, fieldName.length() - "()".length()), fieldName, fieldEntry.getValue().getValue(),
fieldEntry.getValue().getValue(),
componentType, annotationFieldKind, arrayed)); componentType, annotationFieldKind, arrayed));
annotationFieldNamesBuilder.add(fieldName);
} }

// Filling annotation default values (not provided in annotation declaration)
ImmutableSet<String> declaredAnnotationFieldNames = annotationFieldNamesBuilder.build();
for(Symbol annotationMember: ((Symbol.ClassSymbol) methodAnnotation.getAnnotationType().asElement()).members().getElements()) {
if(annotationMember instanceof Symbol.MethodSymbol) {
Symbol.MethodSymbol annotationMemberAsMethod = (Symbol.MethodSymbol)annotationMember;
String fieldName = annotationMemberAsMethod.getSimpleName().toString();
if(!declaredAnnotationFieldNames.contains(fieldName)) {
Type type = annotationMemberAsMethod.getReturnType();
TypeMirror componentType = AnnotationFieldKind.componentTypeOf(type);
boolean arrayed = AnnotationFieldKind.isArrayed(type);
AnnotationFieldKind annotationFieldKind = AnnotationFieldKind.valueOf(processingEnv, type);

annotationFieldsBuilder.add(new AnnotationField(
fieldName,
annotationMemberAsMethod.getDefaultValue()==null?null:annotationMemberAsMethod.getDefaultValue().getValue(),
componentType, annotationFieldKind, arrayed));
}
}
}

AnnotationDescription annotationDescription = new AnnotationDescription(methodAnnotation.getAnnotationType().toString(), annotationFieldsBuilder.build()); AnnotationDescription annotationDescription = new AnnotationDescription(methodAnnotation.getAnnotationType().toString(), annotationFieldsBuilder.build());
return annotationDescription; return annotationDescription;
} }
Expand Down

0 comments on commit b9e9cc4

Please sign in to comment.