Java Reflection Method Name getMethodFromClass(Class cls, String methodName, Class argClass, boolean onlyProtectedAndHigher)

Here you can find the source of getMethodFromClass(Class cls, String methodName, Class argClass, boolean onlyProtectedAndHigher)

Description

Get the method with the given name from the given class, provided that it takes one argument of the provided type.

License

Open Source License

Parameter

Parameter Description
cls The class who should have (or inherit) the method
methodName The name of the method
argClass If provided, the type of the sole argument to the method. If null, no argument is assumed.
onlyProtectedAndHigher If true, we will ignore private methods in the superclasses.

Return

The method if found, otherwise null.

Declaration

private static Method getMethodFromClass(Class<?> cls, String methodName, Class<?> argClass,
        boolean onlyProtectedAndHigher) 

Method Source Code

//package com.java2s;
/*/*  w ww .j a  v  a2  s  . com*/
 * 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;
import java.lang.reflect.Modifier;

public class Main {
    /**
     * Get the method with the given name from the given class, provided that it takes one argument
     * of the provided type.
     * @param cls The class who should have (or inherit) the method
     * @param methodName The name of the method
     * @param argClass If provided, the type of the sole argument to the method. If null, no argument is assumed.
     * @param onlyProtectedAndHigher If true, we will ignore private methods in the superclasses.
     * @return The method if found, otherwise null.
     */
    private static Method getMethodFromClass(Class<?> cls, String methodName, Class<?> argClass,
            boolean onlyProtectedAndHigher) {

        Method[] allMethods = cls.getDeclaredMethods();
        for (Method meth : allMethods) {
            if (!meth.getName().equals(methodName))
                continue;

            if (onlyProtectedAndHigher) {
                int modifiers = meth.getModifiers();
                if (Modifier.isPrivate(modifiers))
                    continue;
            }

            if (argClass != null) {
                Class<?>[] paramTypes = meth.getParameterTypes();
                if (paramTypes.length != 1)
                    continue;

                Class<?> genericType = getGenericType(paramTypes[0]);
                if (!genericType.isAssignableFrom(argClass))
                    continue;
            }

            // Note that if we're trying to set a value to null, we obviously cannot check the
            // signature for overloading, and therefore we'll return the first method which takes
            // one parameter. I think that's not that unreasonable, but it could conceivably break
            // if someone does funny things with their bean.

            return meth;
        }

        return null;
    }

    /**
     * Unfortunately, int.class.isAssignableFrom(Integer.class) returns false, so we need
     * to convert primitive types to their class equivalent.
     * @param cls Any class
     * @return If the class is a primitive class (e.g. int.class, boolean.class, etc...)
     * return the class equivalent (e.g. Integer.class, Boolean.class, etc...), otherwise
     * return the given class.
     */
    private static Class<?> getGenericType(Class<?> cls) {
        if (cls.equals(byte.class))
            return Byte.class;
        if (cls.equals(short.class))
            return Short.class;
        if (cls.equals(int.class))
            return Integer.class;
        if (cls.equals(long.class))
            return Long.class;
        if (cls.equals(float.class))
            return Float.class;
        if (cls.equals(double.class))
            return Double.class;
        if (cls.equals(boolean.class))
            return Boolean.class;
        if (cls.equals(char.class))
            return Character.class;
        return cls;
    }
}

Related

  1. getMethodByNameFromArray(Method[] methods, String methodName)
  2. getMethodByNameSimple(Class clz, String methodName)
  3. getMethodCount(Class clazz, String methodName)
  4. getMethodCountForName(Class clazz, String methodName)
  5. getMethodExceptionType(Class cls, String methodName, Class[] argTypes, int methodPosition, int classPosition, Class defaultType)
  6. getMethodFromClassHierarchy(Class clazz, String methodName)
  7. getMethodFromClassWithInheritance(Class cls, String methodName)
  8. getMethodFullName(Method method)
  9. getMethodIfAvailable(Class clazz, String methodName, Class... paramTypes)