Returns an array of Method objects reflecting all the methods declared by the class including the methods of the superclasses as well. - Java Reflection

Java examples for Reflection:Super Class

Description

Returns an array of Method objects reflecting all the methods declared by the class including the methods of the superclasses as well.

Demo Code


//package com.java2s;

import java.lang.reflect.Method;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
    public static void main(String[] argv) throws Exception {
        Class clazz = String.class;
        System.out.println(getDeclaredMethodsInLineage(clazz));
    }/* ww  w .  j a v  a 2  s  .co  m*/

    /**
     * Returns an array of {@code Method} objects reflecting all the methods declared by the class including the methods
     * of the superclasses as well.
     * <p/>
     * This method matches the same kinds of methods that are returned by {@link Class#getDeclaredMethods()} with the
     * exception that it includes methods inherited from the superclasses. This means public, protected, default
     * (package) access, and private methods AND methods of those types inherited from the superclasses.
     * <p/>
     * The returned list is partially ordered. Results from more derived classes show up earlier in the list. Within the
     * set of fields for the same class, however, the order is undefined.
     *
     * @param clazz the class
     * @return the array of {@code Method} objects representing all the declared methods of this class
     * @see Class#getDeclaredMethods() for more details
     */
    public static List<Method> getDeclaredMethodsInLineage(Class<?> clazz) {
        List<Method> methods = new ArrayList<Method>(Arrays.asList(clazz
                .getDeclaredMethods()));
        Class<?> superClass = clazz.getSuperclass();
        if (superClass != null && !superClass.equals(Object.class)) {
            methods.addAll(getDeclaredMethodsInLineage(superClass));
        }
        return methods;
    }
}

Related Tutorials