Here you can find the source of getMethodAnnotations(Method m)
Parameter | Description |
---|---|
m | The Method for which to determine the Annotation s. |
public static List<Annotation> getMethodAnnotations(Method m)
//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); } } } }