Java Reflection Method Name getMethodFromClassWithInheritance(Class cls, String methodName)

Here you can find the source of getMethodFromClassWithInheritance(Class cls, String methodName)

Description

get Method From Class With Inheritance

License

Open Source License

Declaration

private static Method getMethodFromClassWithInheritance(Class<?> cls, String methodName) 

Method Source Code

//package com.java2s;
/*/* w  w  w.  j a  va  2s. c o m*/
 * The contents of this file are subject to the Automated Business Logic Public License Version 1.0 (the "License"),
 * which is derived from the Mozilla Public License version 1.1. You may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at http://www.automatedbusinesslogic.com/license/public-license
 *
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, 
 * either express or implied. See the License for the specific language governing rights and limitations under the License.
 */

import java.lang.reflect.Method;

public class Main {
    private static Method getMethodFromClassWithInheritance(Class<?> cls, String methodName) {

        Method theMethod = getMethodFromClass(cls, methodName);
        while (theMethod == null && (!cls.getName().equals("java.lang.Object"))) {
            cls = cls.getSuperclass();
            theMethod = getMethodFromClass(cls, methodName);
        }
        return theMethod;
    }

    private static Method getMethodFromClass(Class<?> cls, String methodName) {
        Method[] allMethods = cls.getDeclaredMethods();
        for (Method meth : allMethods) {
            if (!meth.getName().equals(methodName))
                continue;
            return meth;
        }
        return null;
    }
}

Related

  1. getMethodCount(Class clazz, String methodName)
  2. getMethodCountForName(Class clazz, String methodName)
  3. getMethodExceptionType(Class cls, String methodName, Class[] argTypes, int methodPosition, int classPosition, Class defaultType)
  4. getMethodFromClass(Class cls, String methodName, Class argClass, boolean onlyProtectedAndHigher)
  5. getMethodFromClassHierarchy(Class clazz, String methodName)
  6. getMethodFullName(Method method)
  7. getMethodIfAvailable(Class clazz, String methodName, Class... paramTypes)
  8. getMethodIfAvailable(Class clazz, String methodName, Class... paramTypes)
  9. getMethodIfAvailable(Class clazz, String methodName, Class... paramTypes)