Java Reflection Method Parameter getMethodAnnotations(String className, String methodName, Class[] parameterTypes)

Here you can find the source of getMethodAnnotations(String className, String methodName, Class[] parameterTypes)

Description

Returns all annotations on the given method

License

Open Source License

Declaration


public static Annotation[] getMethodAnnotations(String className, String methodName,
        Class<?>[] parameterTypes) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class Main {
    /**//w ww .  j ava2 s .  c  o m
     * Returns all annotations on the given method
     */
    // TODO: Squander should be fetching annotations through the central mechanism that also examines auxiliary files
    public static Annotation[] getMethodAnnotations(String className, String methodName,
            Class<?>[] parameterTypes) {
        try {
            Class<?> cls = Class.forName(className);
            if ("<init>".equals(methodName)) {
                Constructor<?> c = cls.getDeclaredConstructor(parameterTypes);
                c.setAccessible(true);
                return c.getAnnotations();
            } else {
                try {
                    // look for method declared exactly in this class
                    final Method m = cls.getDeclaredMethod(methodName, parameterTypes);
                    m.setAccessible(true);
                    return m.getAnnotations();
                } catch (final NoSuchMethodException e) {
                    // look for a public method declared in some super-class
                    final Method m = cls.getMethod(methodName, parameterTypes);
                    m.setAccessible(true);
                    return m.getAnnotations();
                }
            }
        } catch (NoSuchMethodException e) {
            String msg = String.format("couldn't find a method named %s.%s to extract annotations from", className,
                    methodName);
            throw new RuntimeException(msg, e);
        } catch (Exception e) {
            throw new RuntimeException("error getting annotations", e);
        }
    }
}

Related

  1. getMethod(final Class receiver, final String methodName, final Class... parameterTypes)
  2. getMethod(final Field visitee, final String methodName, final Class... parameterTypes)
  3. getMethod(String classFullName, String methodName, Class... parameterTypes)
  4. getMethod0(Class c, String name, Class... parameterTypes)
  5. getMethod0(Class target, String name, Class[] parameterTypes)
  6. getMethodByName(Class clazz, String name, Class... parameterTypes)
  7. getMethodByParametersWithAnnotation(final Class source, final Class[] params, final Class annotation)
  8. getMethodFromClass(String className, String methodName, Class... parameterTypes)
  9. getMethodFromClassName(String classAndMethodName, Class... parameterTypes)