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

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

Description

Search the method in the class and it's parent classes.

License

Open Source License

Parameter

Parameter Description
clazz a parameter

Declaration

public static Method getMethod(Class clazz, String methodName, Class<?>... parameterTypes)
        throws NoSuchMethodException 

Method Source Code

//package com.java2s;
/*//from ww  w.  j av  a 2  s .c  o m
 *  This file is part of AtlasMapper server and clients.
 *
 *  Copyright (C) 2011 Australian Institute of Marine Science
 *
 *  Contact: Gael Lafond <g.lafond@aims.org.au>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.lang.reflect.Method;

public class Main {
    /**
     * Search the method in the class and it's parent classes.
     * @param clazz
     * @return
     */
    public static Method getMethod(Class clazz, String methodName, Class<?>... parameterTypes)
            throws NoSuchMethodException {
        return _getMethod(null, clazz, methodName, parameterTypes);
    }

    private static Method _getMethod(NoSuchMethodException noSuchMethodException, Class clazz, String methodName,
            Class<?>... parameterTypes) throws NoSuchMethodException {
        if (clazz == null) {
            // The method is realy not found...
            throw noSuchMethodException;
        }

        Method foundMethod = null;
        try {
            foundMethod = clazz.getDeclaredMethod(methodName, parameterTypes);
        } catch (NoSuchMethodException ex) {
            // Don't panic, the method may be in the parent class
            if (noSuchMethodException == null) {
                // Keep the first exception, it's the more explicit.
                noSuchMethodException = ex;
            }
        }

        if (foundMethod == null) {
            foundMethod = _getMethod(noSuchMethodException, clazz.getSuperclass(), methodName, parameterTypes);
        }

        return foundMethod;
    }
}

Related

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