Java Reflection Method Name getMethods(final Class cl, final String name, final int params)

Here you can find the source of getMethods(final Class cl, final String name, final int params)

Description

Gets all the methods with the given name in the given class or super classes.

License

Open Source License

Parameter

Parameter Description
cl the class where the method was declared
name the name of the method
params the number of parameters

Return

a list that contains the found methods, an empty list if no matching method was found.

Declaration

public static List getMethods(final Class cl, final String name, final int params) 

Method Source Code

//package com.java2s;
/*/*  w  w  w.  j  ava  2  s  . co m*/
 * DynamicJava - Copyright (C) 1999 Dyade
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
 * associated documentation files (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions: The above copyright notice and this
 * permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL DYADE BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
 * THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 * Except as contained in this notice, the name of Dyade shall not be used in advertising or
 * otherwise to promote the sale, use or other dealings in this Software without prior written
 * authorization from Dyade.
 */

import java.lang.reflect.Method;

import java.util.LinkedList;
import java.util.List;

public class Main {
    /**
     * Gets all the methods with the given name in the given class or super classes. Even the
     * redefined methods are returned.
     * 
     * @param cl the class where the method was declared
     * @param name the name of the method
     * @param params the number of parameters
     * @return a list that contains the found methods, an empty list if no matching method was
     *         found.
     */
    public static List getMethods(final Class cl, final String name, final int params) {
        final List result = new LinkedList();

        if (cl.isInterface()) {
            final Method[] ms = cl.getDeclaredMethods();
            for (int i = 0; i < ms.length; i++) {
                if (ms[i].getName().equals(name) && ms[i].getParameterTypes().length == params) {
                    result.add(ms[i]);
                }
            }
            final Class[] cs = cl.getInterfaces();
            for (int i = 0; i < cs.length; i++) {
                result.addAll(getMethods(cs[i], name, params));
            }
            if (cs.length == 0) {
                result.addAll(getMethods(Object.class, name, params));
            }
        } else {
            Class c = cl;
            while (c != null) {
                final Method[] ms = c.getDeclaredMethods();

                for (int i = 0; i < ms.length; i++) {
                    if (ms[i].getName().equals(name) && ms[i].getParameterTypes().length == params) {
                        result.add(ms[i]);
                    }
                }
                c = c.getSuperclass();
            }
        }
        return result;
    }
}

Related

  1. getMethodOnlyByName(Class c, String methodName)
  2. getMethodOrNull(Class cls, String name, Class[] args)
  3. getMethodPropertyName(java.lang.reflect.Method method)
  4. getMethods(Class clazz, String name, int args)
  5. getMethods(Class type, String name)
  6. getMethods(final Class clazz, final String methodName)
  7. getMethods(String classname)
  8. getMethodsByName(Class cls, String methodName)
  9. getMethodSubjectName(Method method)