Java Utililty Methods Reflection Annotation Find

List of utility methods to do Reflection Annotation Find

Description

The list of methods to do Reflection Annotation Find are organized into topic(s).

Method

AnnotationfindAnnotation(Set annotations, Class annotationClass)
find Annotation
if (annotations.isEmpty())
    return null; 
for (Annotation annotation : annotations) {
    if (annotation.annotationType() == annotationClass) {
        return annotation;
return null;
...
ClassfindAnnotationClass(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);
...
ClassfindAnnotationDeclaringClass(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());
ClassfindAnnotationDeclaringClass(Class 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;
ClassfindAnnotationDeclaringClass(Class 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());
AnnotationfindAnnotationFromClass(Class target, Class annotation)
find Annotation From Class
for (Annotation targetAnnotation : target.getAnnotations()) {
    if (annotation.isAssignableFrom(targetAnnotation.annotationType())) {
        return targetAnnotation;
    } else {
        continue;
return null;
...
TfindAnnotationFromMethodOrClass(final Method method, final Class annotationClass)
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);
ClassfindAnnotationHelper(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;
...
OptionalfindAnnotationIn(List modifiers, Class 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();
AfindAnnotationInAnnotations(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;
...