is Overriden Method - Java Reflection

Java examples for Reflection:Method

Description

is Overriden Method

Demo Code

/**/*from   w w  w  .ja  v  a2 s.co  m*/
 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
 */
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Main{
    public static boolean isOverridenMethod(Class clazz, Method method,
            boolean checkThisClass) {
        try {
            if (checkThisClass) {
                clazz.getDeclaredMethod(method.getName(),
                        method.getParameterTypes());
                return true;
            }
        } catch (NoSuchMethodException e) {
        }
        // Check super class
        if (clazz.getSuperclass() != null) {
            if (isOverridenMethod(clazz.getSuperclass(), method, true)) {
                return true;
            }
        }
        // Check interfaces
        for (Class anInterface : clazz.getInterfaces()) {
            if (isOverridenMethod(anInterface, method, true)) {
                return true;
            }
        }
        return false;
    }
    public static Class[] getParameterTypes(String... parameterTypeNames) {
        Class[] parameterTypes = new Class[parameterTypeNames.length];
        for (int i = 0; i < parameterTypeNames.length; i++) {
            parameterTypes[i] = getClass(parameterTypeNames[i]);
        }
        return parameterTypes;
    }
    public static Class getClass(String name) {
        try {
            return ClassLoaderUtil.class.getClassLoader().loadClass(name);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}

Related Tutorials