Return the Method object for the given method Name and parameter Types on the given objClass. - Java Reflection

Java examples for Reflection:Method

Description

Return the Method object for the given method Name and parameter Types on the given objClass.

Demo Code

/**//from  w w w  .j ava 2  s.co  m
 * Copyright (c) 2011 eXtensible Catalog Organization
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the MIT/X11 license. The text of the license can be
 * found at http://www.opensource.org/licenses/mit-license.php.
 */
import org.apache.log4j.Logger;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;

public class Main{
    private static final Logger LOG = Logger
            .getLogger(ReflectionHelper.class);
    /**
     * Return the Method object for the given methodName and parameterTypes on the given objClass.
     * @param objClass
     * @param methodName
     * @param parameterTypes
     * @return
     */
    public static Method findMethod(Class objClass, String methodName,
            Class... parameterTypes) {

        LOG.debug("Looking for method " + methodName + "("
                + formatClassNames(parameterTypes) + ") on "
                + objClass.getName() + ".");
        Method result = null;

        Method[] methods = objClass.getDeclaredMethods();
        for (Method m : methods) {

            if (m.getName().compareToIgnoreCase(methodName) == 0) {

                Class[] methodParameterTypes = m.getParameterTypes();
                if (parametersMatch(methodParameterTypes, parameterTypes)) {

                    result = m;
                    break;
                }

            }

        }

        // Handle collections
        if (result == null) {

            methods = objClass.getDeclaredMethods();
            for (Method m : methods) {

                if (m.getName().compareToIgnoreCase(methodName + "s") == 0) {

                    Class[] methodParameterTypes = m.getParameterTypes();
                    if (parametersMatch(methodParameterTypes,
                            parameterTypes)) {

                        result = m;
                        break;
                    }

                }

            }

        }

        if (result == null) {
            LOG.debug("Method " + methodName + "("
                    + formatClassNames(parameterTypes) + ") on "
                    + objClass.getName() + " not found.");
        }
        return result;

    }
    /**
     * Format class names
     * @param classes
     * @return
     */
    public static String formatClassNames(Class... classes) {

        String result = "";
        if (classes != null && classes.length > 0) {

            StringBuilder sb = new StringBuilder();
            for (Class c : classes) {

                sb.append(c.getName()).append(", ");
            }
            result = sb.toString();
            result = result.substring(0, result.length() - 2);

        }
        return result;

    }
    /**
     * Compare two arrays of classes and return true if they are identical.
     * @param methodParameterTypes
     * @param parameterTypes
     * @return
     */
    public static boolean parametersMatch(Class[] methodParameterTypes,
            Class[] parameterTypes) {

        boolean parameterTypesMatch = true;

        if (methodParameterTypes.length == 0
                && (parameterTypes == null || parameterTypes.length == 0)) {

            // Do nothing - parameter types match

        } else if (parameterTypes != null
                && parameterTypes.length == methodParameterTypes.length) {

            for (int i = 0; i < parameterTypes.length; i++) {

                if (!methodParameterTypes[i]
                        .isAssignableFrom(parameterTypes[i])) {

                    parameterTypesMatch = false;
                    break;

                }

            }

        } else {

            parameterTypesMatch = false;

        }

        return parameterTypesMatch;

    }
}

Related Tutorials