Skip to content

Commit b9e9cc4

Browse files
committed
endpoints - Fixed issue on annotation processing when annotation provides default value
1 parent 0745a78 commit b9e9cc4

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

restx-core-annotation-processor/src/main/java/restx/annotations/processor/RestxAnnotationProcessor.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import com.google.common.base.Optional;
55
import com.google.common.collect.*;
66
import com.samskivert.mustache.Template;
7+
import com.sun.tools.javac.code.Symbol;
8+
import com.sun.tools.javac.code.Type;
79
import restx.RestxLogLevel;
810
import restx.StdRestxRequestMatcher;
911
import restx.annotations.*;
@@ -191,18 +193,40 @@ public String transformSingleValueToExpression(Object value, AnnotationField ann
191193

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

201204
annotationFieldsBuilder.add(new AnnotationField(
202-
fieldName.substring(0, fieldName.length() - "()".length()),
203-
fieldEntry.getValue().getValue(),
205+
fieldName, fieldEntry.getValue().getValue(),
204206
componentType, annotationFieldKind, arrayed));
207+
annotationFieldNamesBuilder.add(fieldName);
205208
}
209+
210+
// Filling annotation default values (not provided in annotation declaration)
211+
ImmutableSet<String> declaredAnnotationFieldNames = annotationFieldNamesBuilder.build();
212+
for(Symbol annotationMember: ((Symbol.ClassSymbol) methodAnnotation.getAnnotationType().asElement()).members().getElements()) {
213+
if(annotationMember instanceof Symbol.MethodSymbol) {
214+
Symbol.MethodSymbol annotationMemberAsMethod = (Symbol.MethodSymbol)annotationMember;
215+
String fieldName = annotationMemberAsMethod.getSimpleName().toString();
216+
if(!declaredAnnotationFieldNames.contains(fieldName)) {
217+
Type type = annotationMemberAsMethod.getReturnType();
218+
TypeMirror componentType = AnnotationFieldKind.componentTypeOf(type);
219+
boolean arrayed = AnnotationFieldKind.isArrayed(type);
220+
AnnotationFieldKind annotationFieldKind = AnnotationFieldKind.valueOf(processingEnv, type);
221+
222+
annotationFieldsBuilder.add(new AnnotationField(
223+
fieldName,
224+
annotationMemberAsMethod.getDefaultValue()==null?null:annotationMemberAsMethod.getDefaultValue().getValue(),
225+
componentType, annotationFieldKind, arrayed));
226+
}
227+
}
228+
}
229+
206230
AnnotationDescription annotationDescription = new AnnotationDescription(methodAnnotation.getAnnotationType().toString(), annotationFieldsBuilder.build());
207231
return annotationDescription;
208232
}

0 commit comments

Comments
 (0)