Java Reflection Method Annotation getMethodAnnotation(final Method method, final Class annoClass)

Here you can find the source of getMethodAnnotation(final Method method, final Class annoClass)

Description

get Method Annotation

License

Apache License

Declaration

public static Annotation getMethodAnnotation(final Method method, final Class annoClass) 

Method Source Code

//package com.java2s;
/**/*ww  w .  j a  v a 2  s. com*/
 * @author John DeRegnaucourt (john@cedarsoftware.com)
 *         <br>
 *         Copyright (c) Cedar Software LLC
 *         <br><br>
 *         Licensed under the Apache License, Version 2.0 (the "License");
 *         you may not use this file except in compliance with the License.
 *         You may obtain a copy of the License at
 *         <br><br>
 *         http://www.apache.org/licenses/LICENSE-2.0
 *         <br><br>
 *         Unless required by applicable law or agreed to in writing, software
 *         distributed under the License is distributed on an "AS IS" BASIS,
 *         WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *         See the License for the specific language governing permissions and
 *         limitations under the License.
 */

import java.lang.annotation.Annotation;

import java.lang.reflect.Method;

import java.util.HashSet;
import java.util.LinkedList;

import java.util.Set;

public class Main {
    public static Annotation getMethodAnnotation(final Method method, final Class annoClass) {
        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;
    }

    public static Method getMethod(Class c, String method, Class... types) {
        try {
            return c.getMethod(method, types);
        } catch (Exception nse) {
            return null;
        }
    }

    private static void addInterfaces(final Class classToCheck, final LinkedList<Class> stack) {
        for (Class interFace : classToCheck.getInterfaces()) {
            stack.push(interFace);
        }
    }
}

Related

  1. getMethod(Class annotationClass, Class aClass)
  2. getMethod(Class compClassOrSuper, Class annotation)
  3. getMethodAnnotatedWith(Class toIntrospect, Class c)
  4. getMethodAnnotation(Class a, Method m)
  5. getMethodAnnotation(final Method method, final Class annotationType)
  6. getMethodAnnotation(Method method, Class annotation)
  7. getMethodAnnotation(Method method, Class annotationType)
  8. getMethodAnnotation(Method method, Class clazz)