Can I get all methods of a class?

后端 未结 5 1037
梦如初夏
梦如初夏 2020-11-29 05:12

Suppose that I have a .class file, can I get all the methods included in that class ?

5条回答
  •  一个人的身影
    2020-11-29 05:47

    public static Method[] getAccessibleMethods(Class clazz) {
        List result = new ArrayList();
        while (clazz != null) {
            for (Method method : clazz.getDeclaredMethods()) {
                int modifiers = method.getModifiers();
                if (Modifier.isPublic(modifiers) || Modifier.isProtected(modifiers)) {
                    result.add(method);
                }
            }
            clazz = clazz.getSuperclass();
        }
        return result.toArray(new Method[result.size()]);
    }
    

提交回复
热议问题