给定一个 java.lang.reflect.Method 对象,是否有一种简单的方法来获取它覆盖的方法的层次结构 [英] Given a java.lang.reflect.Method object, is there an easy way to obtain a hierachry of the methods it overrides
问题描述
给定一个 java.lang.reflect.Method 对象,是否有一种简单的方法来获取它覆盖的方法的层次结构.例如:
Given a java.lang.reflect.Method object, is there an easy way to obtain a hierachry of the methods it overrides. For example:
class A {
public void someMethod() { ... }
}
interface B {
public void someMethod();
}
class C extends A {
public void someMethod() { ... }
}
class D extends C implements B {
public void someMethod() { ... }
}
给定 D#someMethod 的 java.lang.reflect.Method 对象,我是否可以轻松获取树:
Given the java.lang.reflect.Method object for D#someMethod, can I easily get the tree:
D#someMethod
|
+- C#someMethod
| |
| +- A#someMethod
|
+- B#someMethod
我猜一定有一种简单的方法可以做到这一点,也许是现有的图书馆?
Am guessing there must be a simple way of doing this, perhaps an existing library?
推荐答案
最终没有找到有用的库,所以按照 assylias 的注释干脆用反射来遍历继承并构建树(包括超类和接口).我没有构建方法树,而是构建了随后可以遍历"的类树.
Ultimately did not find a useful library, so as per assylias comment simply used reflection to traverse the inheritance and build a tree (including superclasses and interfaces). I didn't build a method tree but rather a class tree which can subsequently be 'walked'.
这篇关于给定一个 java.lang.reflect.Method 对象,是否有一种简单的方法来获取它覆盖的方法的层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!