public static <A extends Annotation> List<A> getRepeatableAnnotations(Class<?> cls, Class<A> annotationClass) { A[] annotations = getRepeatableAnnotationsArray(cls, annotationClass); if (annotations == null || annotations.length == 0) { return null; } return Arrays.asList(annotations); }
public static <A extends Annotation> A[] getRepeatableAnnotationsArray(Class<?> cls, Class<A> annotationClass) { A[] annotations = cls.getAnnotationsByType(annotationClass); if (annotations == null || annotations.length == 0) { for (Annotation metaAnnotation : cls.getAnnotations()) { annotations = metaAnnotation.annotationType().getAnnotationsByType(annotationClass); if (annotations != null && annotations.length > 0) { return annotations; } } Class<?> superClass = cls.getSuperclass(); if (superClass != null && !(superClass.equals(Object.class))) { annotations = getRepeatableAnnotationsArray(superClass, annotationClass); } } if (annotations == null || annotations.length == 0) { for (Class<?> anInterface : cls.getInterfaces()) { for (Annotation metaAnnotation : anInterface.getAnnotations()) { annotations = metaAnnotation.annotationType().getAnnotationsByType(annotationClass); if (annotations != null && annotations.length > 0) { return annotations; } } annotations = getRepeatableAnnotationsArray(anInterface, annotationClass); if (annotations != null) { return annotations; } } } return annotations; }
@Test public void getRepeatableAnnotationsArrayTest() { Tag[] annotations = ReflectionUtils.getRepeatableAnnotationsArray(InheritingClass.class, Tag.class); Assert.assertNotNull(annotations); Assert.assertTrue(annotations.length == 1); Assert.assertNotNull(annotations[0]); Assert.assertEquals("inherited tag", annotations[0].name()); }
io.swagger.v3.oas.annotations.responses.ApiResponse[] classResponses = ReflectionUtils.getRepeatableAnnotationsArray(cls, io.swagger.v3.oas.annotations.responses.ApiResponse.class); io.swagger.v3.oas.annotations.tags.Tag[] apiTags = ReflectionUtils.getRepeatableAnnotationsArray(cls, io.swagger.v3.oas.annotations.tags.Tag.class); io.swagger.v3.oas.annotations.servers.Server[] apiServers = ReflectionUtils.getRepeatableAnnotationsArray(cls, io.swagger.v3.oas.annotations.servers.Server.class);
public static <A extends Annotation> List<A> getRepeatableAnnotations(Class<?> cls, Class<A> annotationClass) { A[] annotations = getRepeatableAnnotationsArray(cls, annotationClass); if (annotations == null || annotations.length == 0) { return null; } return Arrays.asList(annotations); }
public static <A extends Annotation> A[] getRepeatableAnnotationsArray(Class<?> cls, Class<A> annotationClass) { A[] annotations = cls.getAnnotationsByType(annotationClass); if (annotations == null || annotations.length == 0) { for (Annotation metaAnnotation : cls.getAnnotations()) { annotations = metaAnnotation.annotationType().getAnnotationsByType(annotationClass); if (annotations != null && annotations.length > 0) { return annotations; } } Class<?> superClass = cls.getSuperclass(); if (superClass != null && !(superClass.equals(Object.class))) { annotations = getRepeatableAnnotationsArray(superClass, annotationClass); } } if (annotations == null || annotations.length == 0) { for (Class<?> anInterface : cls.getInterfaces()) { for (Annotation metaAnnotation : anInterface.getAnnotations()) { annotations = metaAnnotation.annotationType().getAnnotationsByType(annotationClass); if (annotations != null && annotations.length > 0) { return annotations; } } annotations = getRepeatableAnnotationsArray(anInterface, annotationClass); if (annotations != null) { return annotations; } } } return annotations; }
io.swagger.v3.oas.annotations.responses.ApiResponse[] classResponses = ReflectionUtils.getRepeatableAnnotationsArray(cls, io.swagger.v3.oas.annotations.responses.ApiResponse.class); io.swagger.v3.oas.annotations.tags.Tag[] apiTags = ReflectionUtils.getRepeatableAnnotationsArray(cls, io.swagger.v3.oas.annotations.tags.Tag.class); io.swagger.v3.oas.annotations.servers.Server[] apiServers = ReflectionUtils.getRepeatableAnnotationsArray(cls, io.swagger.v3.oas.annotations.servers.Server.class);
.getRepeatableAnnotationsArray(cls, io.swagger.v3.oas.annotations.responses.ApiResponse.class); io.swagger.v3.oas.annotations.tags.Tag[] apiTags = ReflectionUtils.getRepeatableAnnotationsArray(cls, io.swagger.v3.oas.annotations.tags.Tag.class); io.swagger.v3.oas.annotations.servers.Server[] apiServers = ReflectionUtils.getRepeatableAnnotationsArray(cls, io.swagger.v3.oas.annotations.servers.Server.class);