get All Methods Include Super Class - Java Reflection

Java examples for Reflection:Field Get

Description

get All Methods Include Super Class

Demo Code


//package com.java2s;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

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

    public static List<Method> getAllMethodsIncludeSuperClass(
            final Class<?> clazz) {
        List<Method> lf = new ArrayList<Method>();
        for (Class<?> superClass = clazz; superClass != null
                && superClass != Object.class; superClass = superClass
                .getSuperclass()) {
            Method[] f = superClass.getDeclaredMethods();
            for (int j = 0; j < f.length; j++) {
                lf.add(f[j]);
            }
        }
        return lf;
    }
}

Related Tutorials