Check if Class has Annotation - Android java.lang.reflect

Android examples for java.lang.reflect:Annotation

Description

Check if Class has Annotation

Demo Code


//package com.java2s;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

public class Main {
    public static boolean hasAnnotation(
            Class<? extends Annotation> annotation, Object object,
            String methodName) {//  w  w w .  ja  v  a 2 s  .  c  o  m
        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