集册 Java实例教程 检查该方法是否被传递的方法集合中的任何方法重写。

检查该方法是否被传递的方法集合中的任何方法重写。

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

473
检查该方法是否被传递的方法集合中的任何方法重写。

/*

 * JBoss, Home of Professional Open Source.

 * Copyright 2007, Red Hat Middleware LLC, and individual contributors

 * as indicated by the @author tags. See the copyright.txt file in the

 * distribution for a full listing of individual contributors.

 *

 * This is free software; you can redistribute it and/or modify it

 * under the terms of the GNU Lesser General Public License as

 * published by the Free Software Foundation; either version 2.1 of

 * the License, or (at your option) any later version.

 *

 * This software is distributed in the hope that it will be useful,

 * but WITHOUT ANY WARRANTY; without even the implied warranty of

 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU

 * Lesser General Public License for more details.

 *

 * You should have received a copy of the GNU Lesser General Public

 * License along with this software; if not, write to the Free

 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA

 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.

 */

import java.lang.reflect.Method;

import java.lang.reflect.Modifier;

import java.util.ArrayList;/**来 自 nowjava.com - 时代Java**/

import java.util.Arrays;

import java.util.HashMap;

import java.util.LinkedHashSet;

import java.util.List;

import java.util.Map;

import java.util.Set;


public class Main{

    private static Map<Class<?>, Class<?>> primitiveWrapperMap = new HashMap<Class<?>, Class<?>>();

    public static boolean isOverridden(Class<?> icptr, Method method) {

        if (Modifier.isPrivate(method.getModifiers()))

            return false;

        try {

            Method bottomMethod = getMethod(icptr, method.getName(),

                    method.getParameterTypes());

            if (bottomMethod.getDeclaringClass() == method

                    .getDeclaringClass()) {

                return false;/** 来自 时 代 J a v a**/

            }

            return true;

        } catch (NoSuchMethodException e) {

            throw new RuntimeException(e);

        }

    }

    /**

     * Checks whether the <code>method</code> is overriden by any of the methods

     * *in the passed collection of <code>methods</code>*. Returns true if the method

     * is overriden by any of the passed methods, else returns false.

     *

     * @param method

     * @param methods

     * @return

     */

    public static boolean isOverridden(Method method, Method... methods) {


        for (Method someMethod : methods) {

            // first compare the names, if not equal then no

            // point in do other checks.

            if (!method.getName().equals(someMethod.getName())) {

                // skip and move to next

                continue;

            }


            // Now check the params

            if (method.getParameterTypes().length != someMethod

                    .getParameterTypes().length) {

                // skip

                continue;

            }

            // param types

            if (!checkParameters(method.getParameterTypes(),

                    someMethod.getParameterTypes())) {

                // skip

                continue;

            }

            // check the declaring class, if it's the same, then

            // we are checking the method on the same class, so NOT overridden

            if (method.getDeclaringClass().equals(

                    someMethod.getDeclaringClass())) {

                // skip

                continue;

            }

            // the final check to see whether the declaring class

            // of the method being compared is a superclass of the other method.

            // If yes, then the method is overridden

            if (method.getDeclaringClass().isAssignableFrom(

                    someMethod.getDeclaringClass())) {

                // yes, this method is overridden

                return true;

            }

        }

        // the method is not overridden

        return false;

    }

    /**

     * Returns the method with the specified method name.

     *

     * (Slow method)

     *

     * @param methodName

     * @return

     * @throws NoSuchMethodException

     */

    public static Method getMethod(Class<?> cls, String methodName)

            throws NoSuchMethodException {

        if (cls == null)

            throw new NoSuchMethodException(methodName);

        Method methods[] = cls.getDeclaredMethods();

        for (Method method : methods) {

            if (method.getName().equals(methodName))

                return method;

            // TODO: shall we continue search for ambiguous match?

        }

        try {

            return getMethod(cls.getSuperclass(), methodName);

        } catch (NoSuchMethodException e) {

            throw new NoSuchMethodException("No method named " + methodName

                    + " in " + cls + " (or super classes)");

        }

    }

    /**

     * Returns the method with the specified method name and parameters.

     *

     * @param cls

     * @param methodName

     * @param params

     * @return

     * @throws NoSuchMethodException

     */

    public static Method getMethod(Class<?> cls, String methodName,

            Class<?>... params) throws NoSuchMethodException {

        if (cls == null) {

            throw new NoSuchMethodException("Class is null " + methodName);

        }


        Method m = getDeclaredMethod(cls, methodName, params);

        if (m == null) {

            throw new NoSuchMethodException("No method named " + methodName

                    + "(" + (params != null ? Arrays.toString(params) : "")

                    + ") in " + cls + " (or super classes)");

        }

        return m;

    }

    private static boolean checkParameters(Class<?> parameterTypes[],

            Class<?> methodParameterTypes[]) {

        if (parameterTypes.length != methodParameterTypes.length)

            return false;

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

            Class<?> methodParameterType = methodParameterTypes[i];

            if (methodParameterType.isPrimitive())

                methodParameterType = primitiveWrapperMap

                        .get(methodParameterType);

            Class<?> parameterType = parameterTypes[i];

            if (parameterType.isPrimitive())

                parameterType = primitiveWrapperMap.get(parameterType);

            if (!methodParameterType.isAssignabl
展开阅读全文