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

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

Description

get Method

License

Open Source License

Declaration

private static Method getMethod(Class clazz, String name, Class[] parameters) throws NoSuchMethodException 

Method Source Code

//package com.java2s;
/*//w  w  w . j  a v a2 s .  c om
 *  Copyright (C) 2013, Peter Decsi.
 * 
 *  This library is free software: you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public 
 *  License as published by the Free Software Foundation, either 
 *  version 3 of the License, or (at your option) any later version.
 * 
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.lang.reflect.Method;

public class Main {
    private static Method getMethod(Class clazz, String name, Class[] parameters) throws NoSuchMethodException {
        for (Method method : clazz.getMethods()) {
            if (name.equals(method.getName()) && isAssignable(method.getParameterTypes(), parameters))
                return method;
        }
        return clazz.getMethod(name, parameters);
    }

    private static boolean isAssignable(Class[] constructor, Class[] parameters) {
        int size = parameters.length;
        if (size != constructor.length)
            return false;
        for (int i = 0; i < size; i++)
            if (!constructor[i].isAssignableFrom(parameters[i]))
                return false;
        return true;
    }
}

Related

  1. getMethod(Class aClass, String name, Class... parameters)
  2. getMethod(Class cl, String methodName, String obfName, Class... parameterTypes)
  3. getMethod(Class clazz, String methodName, Class... parameterTypes)
  4. getMethod(Class clazz, String methodName, Class... parameterTypes)
  5. getMethod(Class clazz, String name, Class... parameterTypes)
  6. getMethod(Class declaringClass, String name, Class... parameterTypes)
  7. getMethod(Class klass, String methodName, Class[] parameterTypes)
  8. getMethod(Class target, String methodName, Class[] parameterTypes)
  9. getMethod(Class type, String methodName, Class... parameterTypes)