Given a java.lang.reflect.Method object, is there

2019-08-24 09:13发布

问题:

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() { ... }
}

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?

回答1:

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'.