Java Utililty Methods Reflection Method Annotation

List of utility methods to do Reflection Method Annotation

Description

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

Method

MethodgetMethod(Class annotationClass, Class aClass)
Return the first method with the given annotation.
for (Method method : aClass.getMethods()) {
    if (method.isAnnotationPresent(annotationClass)) {
        method.setAccessible(true);
        return method;
return null;
MethodgetMethod(Class compClassOrSuper, Class annotation)
Checks for annotation of the class
for (Method m : compClassOrSuper.getMethods()) {
    if (m.getAnnotation(annotation) != null) {
        return m;
return null;
ListgetMethodAnnotatedWith(Class toIntrospect, Class c)
get Method Annotated With
ArrayList<Method> result = new ArrayList<Method>();
Map<String, Method> allM = new HashMap<String, Method>();
Class<?> currentToIntrospect = toIntrospect;
while (currentToIntrospect != null) {
    for (Method me : currentToIntrospect.getMethods()) {
        if (Modifier.isPublic(me.getModifiers())) {
            if (allM.containsKey(me.getName()) == false)
                allM.put(me.getName(), me);
...
TgetMethodAnnotation(Class a, Method m)
Returns the specified annotation on the specified method.
T t = m.getAnnotation(a);
if (t != null)
    return t;
Class<?> pc = m.getDeclaringClass().getSuperclass();
if (pc != null) {
    for (Method m2 : pc.getDeclaredMethods()) {
        if (isSameMethod(m, m2)) {
            t = getMethodAnnotation(a, m2);
...
AnnotationgetMethodAnnotation(final Method method, final Class annoClass)
get Method Annotation
final Set<Class> visited = new HashSet<Class>();
final LinkedList<Class> stack = new LinkedList<Class>();
stack.add(method.getDeclaringClass());
while (!stack.isEmpty()) {
    Class classToChk = stack.pop();
    if (classToChk == null || visited.contains(classToChk)) {
        continue;
    visited.add(classToChk);
    Method m = getMethod(classToChk, method.getName(), method.getParameterTypes());
    if (m == null) {
        continue;
    Annotation a = m.getAnnotation(annoClass);
    if (a != null) {
        return a;
    stack.push(classToChk.getSuperclass());
    addInterfaces(method.getDeclaringClass(), stack);
return null;
TgetMethodAnnotation(final Method method, final Class annotationType)
Gets the Annotation for the method, null if does not exists.
final Annotation[] annotations = method.getDeclaredAnnotations();
for (Annotation annotation : annotations) {
    if (annotation.annotationType().equals(annotationType)) {
        return (T) annotation;
return null;
AnnotationgetMethodAnnotation(Method method, Class annotation)
get Method Annotation
return method.getAnnotation(annotation);
TgetMethodAnnotation(Method method, Class annotationType)
get Method Annotation
T ann = method.getAnnotation(annotationType);
if (ann == null) {
    for (Class iface : method.getDeclaringClass().getInterfaces()) {
        for (Method otherMethod : iface.getMethods()) {
            if (isMatchingMethodSignatures(method, otherMethod)) {
                return getMethodAnnotation(otherMethod, annotationType);
return ann;
TgetMethodAnnotation(Method method, Class clazz)
get Method Annotation
T t = method.getAnnotation(clazz);
return t;
Map,Annotation>getMethodAnnotationMap(Method method, Collection> annotationClasses)
get Method Annotation Map
Annotation[] methodAnnotations = method.getAnnotations();
Map<Class<? extends Annotation>, Annotation> methodAnnotationMap = new HashMap<Class<? extends Annotation>, Annotation>();
for (Annotation methodAnnotation : methodAnnotations) {
    methodAnnotationMap.put(methodAnnotation.annotationType(), methodAnnotation);
methodAnnotationMap.keySet().retainAll(annotationClasses);
return methodAnnotationMap;