Java Reflection Method Get from Object getMethodIfAny(final Object instance, final String name, final Class[] params)

Here you can find the source of getMethodIfAny(final Object instance, final String name, final Class[] params)

Description

get Method If Any

License

LGPL

Declaration

public static Method getMethodIfAny(final Object instance, final String name, final Class[] params) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.lang.reflect.Method;

import java.util.*;

public class Main {
    @SuppressWarnings("unchecked")
    public static Method getMethodIfAny(final Class clazz, final String name) {
        try {//from   w  ww  .j  a v  a2 s  .c  om
            return clazz.getMethod(name, new Class[0]);
        } catch (Exception e) {
            return null;
        }
    }

    public static Method getMethodIfAny(final Object instance, final String name, final Class[] params) {
        if (null != params && params.length > 0) {
            return getMethodIfAny(instance.getClass(), name, params);
        } else {
            return getMethodIfAny(instance.getClass(), name);
        }
    }

    public static Method getMethodIfAny(Object instance, String name, Object[] params) {
        if (null != params && params.length > 0) {
            Class[] classParams = toClassArray(params);
            return getMethodIfAny(instance.getClass(), name, classParams);
        } else {
            return getMethodIfAny(instance.getClass(), name);
        }
    }

    public static Method getMethodIfAny(Class clazz, String name, Object[] params) {
        if (null != params && params.length > 0) {
            Class[] classParams = toClassArray(params);
            return getMethodIfAny(clazz, name, classParams);
        } else {
            return getMethodIfAny(clazz, name);
        }
    }

    public static Method getMethodIfAny(Class clazz, String name, Class[] params) {
        return getMethodIfAny(clazz, name, params, true, false);
    }

    @SuppressWarnings("unchecked")
    private static Method getMethodIfAny(final Class clazz, final String name, final Class[] params,
            final boolean nearest, final boolean atleastone) {
        Method candidate = null;
        try {
            // try with standard method
            try {
                candidate = clazz.getMethod(name, params);
            } catch (Throwable ignored) {
            }
            if (null == candidate) {
                final Method[] methods = clazz.getMethods();
                for (Method method : methods) {
                    // has same name?
                    if (method.getName().equals(name)) {
                        Class[] methodParams = method.getParameterTypes();
                        // Are parameters compatible?
                        if (methodParams.length == params.length) {
                            boolean isAssignable = parametersMatch(methodParams, params, nearest);
                            if (isAssignable) {
                                candidate = method;
                                break; // found a good candidate. stop searching
                            } else if (atleastone) {
                                candidate = method; // at least has same name and parameters number
                            }
                        } else {
                            if (atleastone && null == candidate) {
                                candidate = method;
                            } // at least has same name
                        }
                    }
                }
            }
        } catch (Throwable t) {
        }

        return candidate;
    }

    public static Class[] toClassArray(Object[] params) {
        if (null == params) {
            return new Class[0];
        }
        Class[] classParams = new Class[params.length];
        for (int i = 0; i < params.length; i++) {
            classParams[i] = params[i].getClass();
        }
        return classParams;
    }

    public static Method[] getMethods(final Class clazz, final int modifier) {
        final List<Method> result = new LinkedList<Method>();
        final Method[] methods = clazz.getDeclaredMethods();
        if (null != methods && methods.length > 0) {
            for (final Method method : methods) {
                if (method.getModifiers() == modifier) {
                    result.add(method);
                }
            }
        }
        return result.toArray(new Method[result.size()]);
    }

    private static boolean parametersMatch(Class<?>[] params1, Class<?>[] params2, boolean nearest) {
        if (params1.length != params2.length) {
            return false;
        }
        for (int i = 0; i < params1.length; i++) {
            final Class<?> param1 = params1[i];
            final Class<?> param2 = params2[i];
            if (null == param1 || null == param2) {
                return true;
            } else if (!param1.isAssignableFrom(param2)) {
                // parameter are not assignable
                if (nearest) {
                    if (!allowCast(param1, param2)) {
                        return false;
                    }
                } else {
                    return false;
                }
            }
        } // for - on parameters
        return true;
    }

    private static boolean allowCast(final Class<?> methodParamType, final Class<?> paramType) {
        try {
            if (methodParamType.isAssignableFrom(String.class)) {
                return true;
            } else if (methodParamType.isAssignableFrom(Long.class)
                    || methodParamType.isAssignableFrom(Integer.class)) {
                if (paramType.isAssignableFrom(Long.class) || paramType.isAssignableFrom(Integer.class)
                        || paramType.isAssignableFrom(Double.class) || paramType.isAssignableFrom(Float.class)) {
                    return true;
                }
            } else if (methodParamType.isAssignableFrom(Boolean.class)
                    || methodParamType.isAssignableFrom(boolean.class)) {
                if (paramType.isAssignableFrom(Boolean.class) || paramType.isAssignableFrom(boolean.class)) {
                    return true;
                }
            }
        } catch (Exception ex) {
            return false;
        }
        return false;
    }
}

Related

  1. getMethodAnnotatedWith(final Class type, final Class annotation, String fieldName, Object fieldValue)
  2. getMethodByName(Object target, String methodName)
  3. getMethodConfigByAnnotaton(Object instance, Class declaringClass, Class annotationType, Class type)
  4. getMethodDescriptor(Object instance, String methodName, Class aClass, Class... parameters)
  5. getMethodFirstParamType(final String methodName, final Object o)
  6. getMethodIgnoreCaseWithNoParams(Object o, String p)
  7. getMethodInHolder(String methodName, Object holder)
  8. getMethodInstace(Class c, Object inst, String name, Class[] types, boolean access)
  9. getMethodName(AccessibleObject method)