List of utility methods to do Reflection Annotation Find
Annotation | findAnnotation(Set extends Annotation> annotations, Class extends Annotation> annotationClass) find Annotation if (annotations.isEmpty()) return null; for (Annotation annotation : annotations) { if (annotation.annotationType() == annotationClass) { return annotation; return null; ... |
Class extends Annotation> | findAnnotationClass(Class> c, Class> base) This method will find the @{code T} in public class Foo extends BaseType if (c == Object.class || c == null) return null; Class<? extends Annotation> answer = null; answer = findAnnotationHelper(base, c.getGenericSuperclass()); if (answer != null) return answer; for (Type iface : c.getGenericInterfaces()) { answer = findAnnotationHelper(base, iface); ... |
Class | findAnnotationDeclaringClass(Class annotationType, Class clazz) find Annotation Declaring Class if (clazz == null || clazz.equals(Object.class)) return null; else return isAnnotationDeclaredLocally(annotationType, clazz) ? clazz : findAnnotationDeclaringClass(annotationType, clazz.getSuperclass()); |
Class> | findAnnotationDeclaringClass(Class extends Annotation> annotationType, Class> classToFind) find Annotation Declaring Class if (hasAnnotation(annotationType, classToFind)) { return classToFind; Class clazz = classToFind; while (clazz != null && (clazz.getSuperclass() != null || clazz.getInterfaces().length > 0)) { if (hasAnnotation(annotationType, clazz.getSuperclass())) { return clazz.getSuperclass(); Class<?>[] interfaces = clazz.getInterfaces(); if (interfaces.length > 0) { for (Class declaringInterface : interfaces) { if (hasAnnotation(annotationType, declaringInterface)) { return declaringInterface; clazz = clazz.getSuperclass(); return null; |
Class> | findAnnotationDeclaringClass(Class extends Annotation> annotationType, Class> clazz) Find the first Class in the inheritance hierarchy of the specified clazz (including the specified clazz itself) which declares an annotation for the specified annotationType , or null if not found. if (clazz == null || clazz.equals(Object.class)) { return null; if (isAnnotationDeclaredLocally(annotationType, clazz)) { return clazz; return findAnnotationDeclaringClass(annotationType, clazz.getSuperclass()); |
Annotation | findAnnotationFromClass(Class> target, Class extends Annotation> annotation) find Annotation From Class for (Annotation targetAnnotation : target.getAnnotations()) { if (annotation.isAssignableFrom(targetAnnotation.annotationType())) { return targetAnnotation; } else { continue; return null; ... |
T | findAnnotationFromMethodOrClass(final Method method, final Class find Annotation From Method Or Class final T annotation = method.getAnnotation(annotationClass); if (annotation != null) { return annotation; final Class<?> originClass = method.getDeclaringClass(); return originClass.getAnnotation(annotationClass); |
Class extends Annotation> | findAnnotationHelper(Class> base, Type iface) find Annotation Helper if (iface instanceof ParameterizedType) { ParameterizedType p = (ParameterizedType) iface; if (!base.equals(p.getRawType())) return null; Type target = p.getActualTypeArguments()[0]; if (target instanceof Class<?>) { if (Annotation.class.isAssignableFrom((Class<?>) target)) { return (Class<? extends Annotation>) target; ... |
Optional | findAnnotationIn(List extends Annotation> modifiers, Class extends A> clazz) Finds an annotation from a list that is an instance of the provided class. return modifiers.stream().filter(mod -> clazz.isAssignableFrom(mod.getClass())).map(mod -> (A) mod)
.findFirst();
|
A | findAnnotationInAnnotations(AnnotatedElement annotatedElement, Class annotationClass) find Annotation In Annotations Annotation[] allAnnotations = annotatedElement.getAnnotations(); for (Annotation annotation : allAnnotations) { A result = findAnnotation(annotation, annotationClass); if (result != null) { return result; return null; ... |