Java Reflection Method Parameter getMethod0(Class c, String name, Class... parameterTypes)

Here you can find the source of getMethod0(Class c, String name, Class... parameterTypes)

Description

Invokes private Class.getMethod0() without throwing NoSuchMethodException.

License

Open Source License

Parameter

Parameter Description
c class to inspect
name name of method to find
parameterTypes parameter types

Return

founded method, or null

Declaration

public static Method getMethod0(Class c, String name, Class... parameterTypes) 

Method Source Code

//package com.java2s;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

public class Main {
    private static Method _getMethod0;

    /**/*from w w w  .j a v a2s. c  o  m*/
     * Invokes private <code>Class.getMethod0()</code> without throwing <code>NoSuchMethodException</code>.
     * Returns only public methods or <code>null</code> if method not found. Since no exception is
     * throwing, it works faster.
     *
     * @param c               class to inspect
     * @param name            name of method to find
     * @param parameterTypes   parameter types
     * @return founded method, or null
     */
    public static Method getMethod0(Class c, String name, Class... parameterTypes) {
        try {
            return (Method) _getMethod0.invoke(c, name, parameterTypes);
        } catch (Exception ignore) {
            return null;
        }
    }

    /**
     * Invokes accessible method of an object.
     *
     * @param c            class that contains method
     * @param obj          object to execute
     * @param method       method to invoke
     * @param paramClasses   classes of parameters
     * @param params       parameters
     */
    public static Object invoke(Class c, Object obj, String method, Class[] paramClasses, Object[] params)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Method m = c.getMethod(method, paramClasses);
        return m.invoke(obj, params);
    }

    /**
     * Invokes static method.
     */
    public static Object invoke(Class c, String method, Class[] paramClasses, Object[] params)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Method m = c.getMethod(method, paramClasses);
        return m.invoke(null, params);
    }

    /**
     * Invokes accessible method of an object.
     *
     * @param obj          object
     * @param method       name of the objects method
     * @param params       method parameters
     * @param paramClasses   method parameter types
     */
    public static Object invoke(Object obj, String method, Class[] paramClasses, Object[] params)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Method m = obj.getClass().getMethod(method, paramClasses);
        return m.invoke(obj, params);
    }

    /**
     * Invokes accessible method of an object without specifying parameter types.
     * @param obj    object
     * @param method method of an object
     * @param params method parameters
     */
    public static Object invoke(Object obj, String method, Object... params)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Class[] paramClass = getClasses(params);
        return invoke(obj, method, paramClass, params);
    }

    public static Object invoke(Class c, Object obj, String method, Object... params)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Class[] paramClass = getClasses(params);
        return invoke(c, obj, method, paramClass, params);
    }

    /**
     * Invokes static method.
     */
    public static Object invoke(Class c, String method, Object... params)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Class[] paramClass = getClasses(params);
        return invoke(c, null, method, paramClass, params);
    }

    /**
     * Returns classes from array of specified objects.
     */
    public static Class[] getClasses(Object... objects) {
        if (objects == null) {
            return null;
        }
        Class[] result = new Class[objects.length];
        for (int i = 0; i < objects.length; i++) {
            if (objects[i] != null) {
                result[i] = objects[i].getClass();
            }
        }
        return result;
    }
}

Related

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