如何在 Eclipse 中列出 Java 类及其祖先公开的所有属性?

给定一个 Java 类,我希望能够列出所有祖先中公开的所有属性,并在同样的方式.

Given a single Java class I'd like to be able to list all properties that are exposed in all ancestors and recursively traverse all their exposed properties (i.e. public or with getters/setters) in the same way.

用一个简单的例子更容易解释:

Easier to explain with a simple example:

public class BaseClass1 {
    private int intProperty; // has getter and setter (not shown)
}
public class SubClass1 extends BaseClass1 {
    private int privateSoNotListed;
    public SubClass2 subClass2Property;
}
public class BaseClass2 {
    public String stringProperty;
}
public class SubClass2 extends BaseClass2 {
    private long longProperty; // has getter and setter (not shown)
}

给定上面的 SubClass1 作为输入,输出将是这样的:

Given SubClass1 above as input, the output would be something like this:

intProperty                      - int    [from BaseClass1]
subClass2Property.stringProperty - String [from BaseClass2]
subClass2Property.longProperty   - long   [from SubClass2]

应该可以使用一些巧妙的反射来编写这样的东西,但我宁愿不重新发明轮子 - 有没有可以做到这一点的现有工具(也许是 Eclipse 插件?)

It should be possible to write something like this using a bit of clever reflection but I'd rather not reinvent the wheel - is there an existing tool that can do this (perhaps an Eclipse plugin?)

Eclipse 的类型层次结构在显示单个类的属性方面做得很好 - 在我看来,理想的解决方案是如果这是一个树视图(类似于包资源管理器),具有以下能力扩展本身就是类的属性.

Eclipse's Type Hierarchy does a nice job of displaying properties for a single class - the ideal solution in my mind would be if this were a tree view (similar to Package Explorer) with the ability to expand properties that are themselves classes.

推荐答案

刚刚找到了一种有用的方法来实现与最初通过 Eclipse 的类型层次结构所要求的非常相似的东西.

Have just found a useful way of achieving something fairly similar to what was originally asked via Eclipse's Type Hierarchy.

有一个名为显示所有继承的成员"的切换按钮,如下面的红色箭头所示:

There is a toggle named "Show All Inherited Members" as shown by the red arrow below:

选择此项后,除了所选类的字段和方法外,还会显示所有超类的字段和方法(清楚地表明每个超类的来源),如下所示:

After selecting this, the fields and methods from all superclasses are displayed in addition to those for the selected class (with a clear indication of where each one came from), as shown below:

(当然,这不仅包括属性,但由于 getter 按字母顺序显示,并且有公共/私有/受保护的图标,因此可以很容易地用于获取此信息.)

相关文章