Java Reflection Method Annotation getMethodAnnotations(Method m)

Here you can find the source of getMethodAnnotations(Method m)

Description

For a give Method m determine Annotation s in the following manner:
  • Check if the given Method has Annotation s.

    License

    Open Source License

    Parameter

    Parameter Description
    m The Method for which to determine the Annotation s.

    Return

    The of for the given .

    Declaration

    public static List<Annotation> getMethodAnnotations(Method m) 
    

    Method Source Code

    
    //package com.java2s;
    //License from project: Open Source License 
    
    import java.lang.annotation.Annotation;
    import java.lang.reflect.AccessibleObject;
    import java.lang.reflect.Constructor;
    import java.lang.reflect.Field;
    import java.lang.reflect.Method;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    public class Main {
        /**// w w  w.  j  a v a2s  .c om
         * For a give {@link Method} m determine {@link Annotation}s in the following manner:
         * <ul>
         * <li>Check if the given {@link Method} has {@link Annotation}s. If <code>true</code>, return them.</li>
         * <li>If <code>false</code> recurse on the super-class.</li>
         * If the super-class is <code>null</code>, return <code>null</code>.
         * </ul>
         * 
         * @param m
         *            The {@link Method} for which to determine the {@link Annotation}s.
         * @return The {@link List} of {@link Annotation} for the given {@link Method}.
         */
        public static List<Annotation> getMethodAnnotations(Method m) {
            Method method = (Method) getFirstAnnotatedAccessibleObject(m.getDeclaringClass(), m);
    
            if (m != null) {
                return Arrays.asList(method.getDeclaredAnnotations());
            }
            return new ArrayList<Annotation>();
        }
    
        private static AccessibleObject getFirstAnnotatedAccessibleObject(Class<?> type, AccessibleObject ao) {
            if (type == null) {
                return null;
            } else if (ao.getDeclaredAnnotations() != null) {
                return ao;
            } else {
                Class<?> superClass = type.getSuperclass();
                try {
                    if (superClass != null) {
                        if (ao instanceof Field) {
                            Field f = (Field) ao;
    
                            return getFirstAnnotatedAccessibleObject(superClass, superClass.getField(f.getName()));
                        } else if (ao instanceof Method) {
                            Method m = (Method) ao;
    
                            return getFirstAnnotatedAccessibleObject(superClass,
                                    superClass.getMethod(m.getName(), m.getParameterTypes()));
                        } else if (ao instanceof Constructor<?>) {
                            Constructor<?> c = (Constructor<?>) ao;
    
                            return getFirstAnnotatedAccessibleObject(superClass,
                                    superClass.getConstructor(c.getParameterTypes()));
                        } else {
                            throw new RuntimeException("Unsupported Accessible Object");
                        }
                    }
                    return null;
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    

    Related

    1. getMethodAnnotation(final Method method, final Class annotationType)
    2. getMethodAnnotation(Method method, Class annotation)
    3. getMethodAnnotation(Method method, Class annotationType)
    4. getMethodAnnotation(Method method, Class clazz)
    5. getMethodAnnotationMap(Method method, Collection> annotationClasses)
    6. getMethodByAnnotation(Class cls, Class annotationClass)
    7. getMethodContainsAnnotations(Class target, Class annotation)
    8. getMethodInClassWithAnnotation(Class cls, Class annotationClass, int... modifiers)
    9. getMethodLevelAnnotations(Class clazz, Class annotation)