Java Reflection Method Name getMethod(Class clz, String methodName, Class expectedTypes[])

Here you can find the source of getMethod(Class clz, String methodName, Class expectedTypes[])

Description

get Method

License

Open Source License

Declaration

public static Method getMethod(Class clz, String methodName, Class expectedTypes[])
            throws NoSuchMethodException 

Method Source Code

//package com.java2s;

import java.lang.reflect.Method;

public class Main {
    public static final Class EMPTY_CLASS_ARRAY[] = new Class[0];

    public static Method getMethod(Class clz, String methodName, Class expectedTypes[])
            throws NoSuchMethodException {
        Method method = null;//  w  w  w.j  a v a2  s.  c o m
        try {
            method = clz.getMethod(methodName, expectedTypes);
        } catch (NoSuchMethodException e) {
            Method methods[] = clz.getMethods();
            for (int i = 0; i < methods.length; i++) {
                Method _method = methods[i];
                if (!_method.getName().equals(methodName)
                        || !isAssignable(expectedTypes, _method.getParameterTypes()))
                    continue;
                if (method == null) {
                    method = _method;
                    continue;
                }
                if (isAssignable(_method.getParameterTypes(), method.getParameterTypes()))
                    method = _method;
            }

            if (method == null)
                throw e;
        }
        return method;
    }

    public static boolean isAssignable(Class cls, Class toClass) {
        if (toClass == null)
            return false;
        if (cls == null)
            return !toClass.isPrimitive();
        if (cls.equals(toClass))
            return true;
        if (cls.isPrimitive()) {
            if (!toClass.isPrimitive())
                return false;
            if (Integer.TYPE.equals(cls))
                return Long.TYPE.equals(toClass) || Float.TYPE.equals(toClass) || Double.TYPE.equals(toClass);
            if (Long.TYPE.equals(cls))
                return Float.TYPE.equals(toClass) || Double.TYPE.equals(toClass);
            if (Boolean.TYPE.equals(cls))
                return false;
            if (Double.TYPE.equals(cls))
                return false;
            if (Float.TYPE.equals(cls))
                return Double.TYPE.equals(toClass);
            if (Character.TYPE.equals(cls))
                return Integer.TYPE.equals(toClass) || Long.TYPE.equals(toClass) || Float.TYPE.equals(toClass)
                        || Double.TYPE.equals(toClass);
            if (Short.TYPE.equals(cls))
                return Integer.TYPE.equals(toClass) || Long.TYPE.equals(toClass) || Float.TYPE.equals(toClass)
                        || Double.TYPE.equals(toClass);
            if (Byte.TYPE.equals(cls))
                return Short.TYPE.equals(toClass) || Integer.TYPE.equals(toClass) || Long.TYPE.equals(toClass)
                        || Float.TYPE.equals(toClass) || Double.TYPE.equals(toClass);
            else
                return false;
        } else {
            return toClass.isAssignableFrom(cls);
        }
    }

    public static boolean isAssignable(Class classArray[], Class toClassArray[]) {
        if (classArray.length != toClassArray.length)
            return false;
        if (classArray == null)
            classArray = EMPTY_CLASS_ARRAY;
        if (toClassArray == null)
            toClassArray = EMPTY_CLASS_ARRAY;
        for (int i = 0; i < classArray.length; i++)
            if (!isAssignable(classArray[i], toClassArray[i]))
                return false;

        return true;
    }

    public static boolean isAssignableFrom(Class original, Class checkedClasses[]) {
        if (checkedClasses == null)
            return false;
        for (int i = 0; i < checkedClasses.length; i++)
            if (original.isAssignableFrom(checkedClasses[i]))
                return true;

        return false;
    }
}

Related

  1. getMethod(Class clazz, String methodName, Class[] classes)
  2. getMethod(Class clazz, String methodName, Class[] params)
  3. getMethod(Class clazz, String name)
  4. getMethod(Class clazz, String name, Class... args)
  5. getMethod(Class cls, String methodName, Class[] params)
  6. getMethod(Class klazz, String[] methodNames, int argCount)
  7. getMethod(Class objClass, String methodName, Class argClass)
  8. getMethod(Class serviceClass, String methodName, Class... mapClass)
  9. getMethod(Class targetClass, String methodName, Class[] paramTypes)