Java Reflection Method Parameter getMethod(Class clazz, String name, Class[] parameterTypes)

Here you can find the source of getMethod(Class clazz, String name, Class[] parameterTypes)

Description

Returns a matching method for the given name and parameters on the given class If the parameterTypes arguments is null it will return the first matching method on the class.

License

Open Source License

Parameter

Parameter Description
clazz the class to find the method on
name the method name to find
parameterTypes an array of argument types or null

Return

the Method object or null if none was found

Declaration

public static Method getMethod(Class<?> clazz, String name, Class<?>[] parameterTypes) 

Method Source Code

//package com.java2s;
/*/* ww w.  j  av a 2 s.co m*/
 * $Id$
 * --------------------------------------------------------------------------------------
 * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 *
 * The software in this package is published under the terms of the CPAL v1.0
 * license, a copy of which has been included with this distribution in the
 * LICENSE.txt file.
 */

import java.lang.reflect.Method;

public class Main {
    /**
     * Returns a matching method for the given name and parameters on the given class
     * If the parameterTypes arguments is null it will return the first matching
     * method on the class.
     *
     * @param clazz          the class to find the method on
     * @param name           the method name to find
     * @param parameterTypes an array of argument types or null
     * @return the Method object or null if none was found
     */
    public static Method getMethod(Class<?> clazz, String name, Class<?>[] parameterTypes) {
        return getMethod(clazz, name, parameterTypes, false);
    }

    public static Method getMethod(Class clazz, String name, Class[] parameterTypes, boolean acceptNulls) {
        Method[] methods = clazz.getMethods();
        for (int i = 0; i < methods.length; i++) {
            if (methods[i].getName().equals(name)) {
                if (parameterTypes == null) {
                    return methods[i];
                } else if (compare(methods[i].getParameterTypes(), parameterTypes, true, acceptNulls)) {
                    return methods[i];
                }
            }
        }
        return null;
    }

    public static boolean compare(Class[] c1, Class[] c2, boolean matchOnObject) {
        return compare(c1, c2, matchOnObject, false);
    }

    /**
     * Returns true if the types from array c2 are assignable to the types from c1
     * and the arrays are the same size. If matchOnObject argument is true and there
     * is a parameter of type Object in c1 then the method returns false. If
     * acceptNulls argument is true, null values are accepted in c2.
     * 
     * @param c1 parameter types array
     * @param c2 parameter types array
     * @param matchOnObject return false if there is a parameter of type Object in c1
     * @param acceptNulls allows null parameter types in c2
     * @return true if arrays are the same size and the types assignable from c2 to
     *         c1
     */
    public static boolean compare(Class[] c1, Class[] c2, boolean matchOnObject, boolean acceptNulls) {
        if (c1.length != c2.length) {
            return false;
        }
        for (int i = 0; i < c1.length; i++) {
            if (!acceptNulls) {
                if ((c1[i] == null) || (c2[i] == null)) {
                    return false;
                }
            } else {
                if (c1[i] == null) {
                    return false;
                }
                if ((c2[i] == null) && (c1[i].isPrimitive())) {
                    return false;
                }
                if (c2[i] == null) {
                    return true;
                }
            }
            if (c1[i].equals(Object.class) && !matchOnObject) {
                return false;
            }
            if (!c1[i].isAssignableFrom(c2[i])) {
                return false;
            }
        }
        return true;
    }

    public static Class<?>[] getParameterTypes(Object bean, String methodName) {
        if (!methodName.startsWith("set")) {
            methodName = "set" + methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
        }

        Method methods[] = bean.getClass().getMethods();

        for (int i = 0; i < methods.length; i++) {
            if (methods[i].getName().equals(methodName)) {
                return methods[i].getParameterTypes();
            }
        }

        return new Class[] {};
    }
}

Related

  1. getMethod(Class clazz, String methodName, Class... parameterTypes)
  2. getMethod(Class clazz, String methodName, Class... parameterTypes)
  3. getMethod(Class clazz, String methodName, Class... parameterTypes)
  4. getMethod(Class clazz, String name, Class... parameterTypes)
  5. getMethod(Class clazz, String name, Class... parameterTypes)
  6. getMethod(Class cls, String name, Class... parameterTypes)
  7. getMethod(Class clz, String methodName, Class... parameterTypes)
  8. getMethod(Class declaringClass, String name, Class... parameterTypes)
  9. getMethod(Class declaringType, String methodName, Class... parameterTypes)