All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.logspace.agent.shaded.quartz.utils.ClassUtils Maven / Gradle / Ivy

There is a newer version: 0.3.2
Show newest version
package io.logspace.agent.shaded.quartz.utils;

import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

public class ClassUtils {

    
    public static boolean isAnnotationPresent(Class clazz, Class a) {
        for (Class c = clazz; c != null; c = c.getSuperclass()) {
            if (c.isAnnotationPresent(a))
                return true;
            if(isAnnotationPresentOnInterfaces(c, a))
                return true;
        }
        return false;
    }

    private static boolean isAnnotationPresentOnInterfaces(Class clazz, Class a) {
        for(Class i : clazz.getInterfaces()) {
            if( i.isAnnotationPresent(a) )
                return true;
            if(isAnnotationPresentOnInterfaces(i, a))
                return true;
        }
        
        return false;
    }

    public static  T getAnnotation(Class clazz, Class aClazz) {
        //Check class hierarchy
        for (Class c = clazz; c != null; c = c.getSuperclass()) {
            T anno = c.getAnnotation(aClazz);
            if (anno != null) {
                return anno;
            }
        }

        //Check interfaces (breadth first)
        Queue> q = new LinkedList>();
        q.add(clazz);
        while (!q.isEmpty()) {
            Class c = q.remove();
            if (c != null) {
                if (c.isInterface()) {
                    T anno = c.getAnnotation(aClazz);
                    if (anno != null) {
                        return anno;
                    }
                } else {
                    q.add(c.getSuperclass());
                }
                q.addAll(Arrays.asList(c.getInterfaces()));
            }
        }

        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy