|
4 | 4 | import com.google.common.base.Optional; |
5 | 5 | import com.google.common.collect.*; |
6 | 6 | import com.samskivert.mustache.Template; |
| 7 | +import com.sun.tools.javac.code.Symbol; |
| 8 | +import com.sun.tools.javac.code.Type; |
7 | 9 | import restx.RestxLogLevel; |
8 | 10 | import restx.StdRestxRequestMatcher; |
9 | 11 | import restx.annotations.*; |
@@ -191,18 +193,40 @@ public String transformSingleValueToExpression(Object value, AnnotationField ann |
191 | 193 |
|
192 | 194 | private AnnotationDescription createAnnotationDescriptionFrom(AnnotationMirror methodAnnotation, ExecutableElement element) { |
193 | 195 | ImmutableList.Builder<AnnotationField> annotationFieldsBuilder = ImmutableList.builder(); |
| 196 | + ImmutableSet.Builder<String> annotationFieldNamesBuilder = ImmutableSet.builder(); |
194 | 197 | 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()); |
196 | 199 | TypeMirror type = fieldEntry.getKey().getReturnType(); |
197 | 200 | TypeMirror componentType = AnnotationFieldKind.componentTypeOf(type); |
198 | 201 | boolean arrayed = AnnotationFieldKind.isArrayed(type); |
199 | 202 | AnnotationFieldKind annotationFieldKind = AnnotationFieldKind.valueOf(processingEnv, type); |
200 | 203 |
|
201 | 204 | annotationFieldsBuilder.add(new AnnotationField( |
202 | | - fieldName.substring(0, fieldName.length() - "()".length()), |
203 | | - fieldEntry.getValue().getValue(), |
| 205 | + fieldName, fieldEntry.getValue().getValue(), |
204 | 206 | componentType, annotationFieldKind, arrayed)); |
| 207 | + annotationFieldNamesBuilder.add(fieldName); |
205 | 208 | } |
| 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 | + |
206 | 230 | AnnotationDescription annotationDescription = new AnnotationDescription(methodAnnotation.getAnnotationType().toString(), annotationFieldsBuilder.build()); |
207 | 231 | return annotationDescription; |
208 | 232 | } |
|
0 commit comments