Java Reflection Method Parameter getMethodRecursive(final Class clazz, final String methodName, final Class... parameterTypes)

Here you can find the source of getMethodRecursive(final Class clazz, final String methodName, final Class... parameterTypes)

Description

get Method Recursive

License

Open Source License

Return

the method or null if the method does not exist

Declaration

public static Method getMethodRecursive(final Class<?> clazz, final String methodName,
        final Class<?>... parameterTypes) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Portions created by Sebastian Thomschke are copyright (c) 2005-2016 Sebastian
 * Thomschke.//from   www . j  av a2  s .  co m
 *
 * All Rights Reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Sebastian Thomschke - initial implementation.
 *******************************************************************************/

import java.lang.reflect.Method;

public class Main {
    /**
     * @return the method or null if the method does not exist
     */
    public static Method getMethodRecursive(final Class<?> clazz, final String methodName,
            final Class<?>... parameterTypes) {
        final Method m = getMethod(clazz, methodName, parameterTypes);
        if (m != null)
            return m;

        final Class<?> superclazz = clazz.getSuperclass();
        if (superclazz == null)
            return null;

        return getMethodRecursive(superclazz, methodName, parameterTypes);
    }

    /**
     * @return the method or null if the method does not exist
     */
    public static Method getMethod(final Class<?> clazz, final String methodName,
            final Class<?>... parameterTypes) {
        try {
            return clazz.getDeclaredMethod(methodName, parameterTypes);
        } catch (final NoSuchMethodException ex) {
            return null;
        }
    }
}

Related

  1. getMethodParameterIndexes(final Method m)
  2. getMethodParameters(final Method method, final Map generics)
  3. getMethodParametersType(Class clazz, String methodName)
  4. getMethodParameterTypes(final Method method)
  5. getMethodQuietly(Class clazz, String methodName, Class... parameterTypes)
  6. getMethodsWith(Class c, Class... parameters)
  7. getMethodUp(Class type, String name, Class... parameterTypes)
  8. getMethodWithParameter(Class declaringClass, Class parameterClass)