/** Create {@link JavaType} based on {@link AnnotatedType} instance. */ public static JavaType of(AnnotatedType annotatedType) { if (annotatedType instanceof AnnotatedArrayType) { return JavaTypes.of((AnnotatedArrayType) annotatedType); } if (annotatedType instanceof AnnotatedParameterizedType) { return JavaTypes.of((AnnotatedParameterizedType) annotatedType); } if (annotatedType instanceof AnnotatedTypeVariable) { return JavaTypes.of((AnnotatedTypeVariable) annotatedType); } if (annotatedType instanceof AnnotatedWildcardType) { return JavaTypes.of((AnnotatedWildcardType) annotatedType); } // default case: use underlying raw type JavaType result = of(annotatedType.getType()); result.getAnnotations().addAll(Annotation.of(annotatedType.getAnnotations())); return result; }
public static boolean containsTypeAnnotation(AnnotatedType type, Class<? extends Annotation> annotation) { if (type.isAnnotationPresent(annotation)) { return true; } if (type instanceof AnnotatedParameterizedType) { AnnotatedParameterizedType parameterizedType = ((AnnotatedParameterizedType) type); return Arrays.stream(parameterizedType.getAnnotatedActualTypeArguments()) .anyMatch(param -> containsTypeAnnotation(param, annotation)); } if (type instanceof AnnotatedTypeVariable) { AnnotatedTypeVariable variable = ((AnnotatedTypeVariable) type); return Arrays.stream(variable.getAnnotatedBounds()) .anyMatch(bound -> containsTypeAnnotation(bound, annotation)); } if (type instanceof AnnotatedWildcardType) { AnnotatedWildcardType wildcard = ((AnnotatedWildcardType) type); return Stream.concat( Arrays.stream(wildcard.getAnnotatedLowerBounds()), Arrays.stream(wildcard.getAnnotatedUpperBounds())) .anyMatch(param -> containsTypeAnnotation(param, annotation)); } if (type instanceof AnnotatedArrayType) { return containsTypeAnnotation(((AnnotatedArrayType) type).getAnnotatedGenericComponentType(), annotation); } return false; }
private void verifyMapFieldTypeAnnotations(Class c) throws NoSuchFieldException, NoSuchMethodException { Annotation anno; AnnotatedType atBase; AnnotatedType atParameter; atBase = c.getDeclaredField("typeAnnotatedMap").getAnnotatedType(); anno = atBase.getAnnotations()[0]; verifyTestAnn(mapTA[0], anno, "map1"); mapTA[0] = anno; atParameter = ((AnnotatedParameterizedType) atBase). getAnnotatedActualTypeArguments()[0]; anno = ((AnnotatedWildcardType) atParameter).getAnnotations()[0]; verifyTestAnn(mapTA[1], anno, "map2"); mapTA[1] = anno; anno = ((AnnotatedWildcardType) atParameter). getAnnotatedUpperBounds()[0].getAnnotations()[0]; verifyTestAnn(mapTA[2], anno, "map3"); mapTA[2] = anno; atParameter = ((AnnotatedParameterizedType) atBase). getAnnotatedActualTypeArguments()[1]; anno = ((AnnotatedParameterizedType) atParameter).getAnnotations()[0]; verifyTestAnn(mapTA[3], anno, "map4"); mapTA[3] = anno; anno = ((AnnotatedParameterizedType) atParameter). getAnnotatedActualTypeArguments()[0].getAnnotations()[0]; verifyTestAnn(mapTA[4], anno, "map5"); mapTA[4] = anno; }