Checks whether the method is overriden by any of the methods *in the passed collection of methods*. - Java Reflection

Java examples for Reflection:Method

Description

Checks whether the method is overriden by any of the methods *in the passed collection of methods*.

Demo Code

/*/*from   ww  w .  j a v  a  2 s  .  c  om*/
 * 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;
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;
            }
            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.isAssignableFrom(parameterType)) {
                return false;
            }
        }
        return true;
    }
    private static Method getDeclaredMethod(Class<?> cls,
            String methodName, Class<?>... params) {
        Method methods[] = SecurityActions.getDeclaredMethods(cls);
        for (Method method : methods) {
            if (method.getName().equals(methodName)) {
                if (params == null)
                    return method;
                Class<?> methodParameterTypes[] = method
                        .getParameterTypes();
                if (params.length != methodParameterTypes.length)
                    continue;
                if (checkParameters(params, methodParameterTypes))
                    return method;
            }
        }

        try {
            return SecurityActions.getDeclaredMethod(cls, methodName,
                    params);
        } catch (NoSuchMethodException e1) {
        }

        if (cls == Object.class) {
            return null;
        }

        return getDeclaredMethod(cls.getSuperclass(), methodName, params);
    }
}

Related Tutorials