Check if the object class has the annotation in the specified method - Android java.lang.reflect

Android examples for java.lang.reflect:Annotation

Description

Check if the object class has the annotation in the specified method

Demo Code


import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

public class Main{
    /**//from  w w w.  j ava2 s. co  m
     * Check if the object class has the annotation in the specified method
     * 
     * @param object
     * @param methodName
     * @return true if the object class has the annotation {@link OnUiThread}
     */
    public static boolean hasUiAnnotation(Object object, String methodName) {
        return hasAnnotation(OnUiThread.class, object, methodName);
    }
    public static boolean hasAnnotation(
            Class<? extends Annotation> annotation, Object object,
            String methodName) {
        try {
            Class<? extends Object> c = object.getClass();

            for (Method m : c.getMethods()) {
                if (m.getName().equals(methodName)) {
                    if (m.isAnnotationPresent(annotation)) {
                        return true;
                    }
                }
            }
        } catch (Exception e) {
        }
        return false;
    }
}

Related Tutorials