如何列出Java类及其祖先在Eclipse中公开的所有属性? [英] How to list all properties exposed by a Java class and its ancestors in Eclipse?
问题描述
public
或getter / setters)以同样的方式。 简单的例子说明一下:
public class BaseClass1 {
private int intProperty; //有getter和setter(未显示)
}
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; //有getter和setter(未显示)
}
给定 SubClass1
作为输入,输出将是这样的:
intProperty - int [from BaseClass1]
subClass2Property.stringProperty - String [from BaseClass2]
subClass2Property.longProperty - long [from SubClass2]
应该可以使用一些聪明的反思来写这样的东西,但我宁愿不重新发明 - 有没有现有的工具可以做到这一点一个Eclipse插件?)
编辑: Eclipse的类型层次结构对于显示单个类的属性做出了不错的工作 - 在我心中是理想的解决方案将是如果这是一个树视图(类似于Package Explorer),具有展开本身类的属性的能力。
有呃st发现了一种有用的方法,可以通过Eclipse的类型层次结构实现与原始问题相似的东西。
有一个名为显示所有继承的成员的切换,如以下红色箭头:
选择此项后,除了所选类别的字段和方法外,还显示了所有超类的字段和方法(清楚地说明每个类的位置),如下所示:
< a href =https://i.stack.imgur.com/KaHC2.png =nofollow noreferrer>
(当然,这不仅仅包括属性,而是由于getter以字母顺序显示,是公共/私人/保护的图标,可以用来获取这个信息很容易。)
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)
}
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]
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?)
EDIT: 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.
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:
(Of course, this includes more than just properties but since the getters are displayed in alphabetical order and there are icons for public/private/protected, it can be used to obtain this information easily enough.)
这篇关于如何列出Java类及其祖先在Eclipse中公开的所有属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!