public static <A extends Annotation> A getAnnotation(Object obj, Class<A> acl)
//package com.java2s; import java.lang.annotation.Annotation; import java.lang.reflect.Method; public class Main { public static <A extends Annotation> A getAnnotation(Object obj, Class<A> acl) { return getAnnotation(obj.getClass(), acl); }// w w w. j a va 2 s. c o m @SuppressWarnings("unchecked") public static <A extends Annotation> A getAnnotation(Class cl, Class<A> acl) { A ret = (A) cl.getAnnotation(acl); while (ret == null && cl.getSuperclass() != null) { cl = cl.getSuperclass(); ret = (A) cl.getAnnotation(acl); } return ret; } @SuppressWarnings("unchecked") public static <A extends Annotation> A getAnnotation(Class cl, Method m, Class<A> acl) { A ret = (A) m.getAnnotation(acl); while (ret == null && cl.getSuperclass() != null) { cl = cl.getSuperclass(); try { m = cl.getMethod(m.getName(), m.getParameterTypes()); ret = m.getAnnotation(acl); } catch (NoSuchMethodException e) { break; } } return ret; } }