集册 Java实例教程 返回给定objClass上给定方法名称和参数类型的Method对象。

返回给定objClass上给定方法名称和参数类型的Method对象。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

507
返回给定objClass上给定方法名称和参数类型的Method对象。

/**

 * 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;
/**来自 
 nowjava - 时  代  Java**/

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();/**来 自 时 代 J a v a 公 众 号 - N o w J a v  a . c o m**/

        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

                && (paramet
展开阅读全文