Java Reflection Method Name getMethod(Class klass, String methodName, Class requestClass)

Here you can find the source of getMethod(Class klass, String methodName, Class requestClass)

Description

Gets method with expected methodName, and with one parameter whose type is requestClass or its super class.

License

Open Source License

Parameter

Parameter Description
klass the target class to find method
methodName the name of the method
requestClass the parameter type for the expected emthod

Return

the match method object, or null if not found

Declaration

public static Method getMethod(Class<?> klass, String methodName, Class<?> requestClass) 

Method Source Code


//package com.java2s;
/* Copyright 2011 Google Inc. All Rights Reserved.
 * /*from ww  w .j  a v  a 2  s.  c  o m*/
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.lang.reflect.Method;

public class Main {
    /**
     * Gets method with expected methodName, and with one parameter whose type is requestClass or its
     * super class.
     * <p>
     * This method is used to find right method on evaluator and action classes. See
     * {@code GitEvaluator} and {@code UserStatusAction} as an example.
     * @param klass the target class to find method
     * @param methodName the name of the method
     * @param requestClass the parameter type for the expected emthod
     * @return the match method object, or null if not found
     */
    public static Method getMethod(Class<?> klass, String methodName, Class<?> requestClass) {
        Class<?> theRequestClass = requestClass;
        while (theRequestClass != Object.class) {
            try {
                return klass.getMethod(methodName, theRequestClass);
            } catch (SecurityException e) {
            } catch (NoSuchMethodException e) {
            }
            theRequestClass = theRequestClass.getSuperclass();
        }
        return null;
    }
}

Related

  1. getMethod(Class cls, String name)
  2. getMethod(Class cls, String name, Class... types)
  3. getMethod(Class clss, String name, Class... params)
  4. getMethod(Class clz, String name)
  5. getMethod(Class klass, String methodName)
  6. getMethod(Class klass, String methodName, Class... paramTypes)
  7. getMethod(Class klass, String name, String name2)
  8. getMethod(Class theClass, String methodName, Class[] paramTypes)
  9. getMethod(Class c, String name, Class... argTypes)