遍历对象层次结构
我正在寻找在 Java 中遍历任意对象层次结构的方法。显然,直接使用反射 API 是一种选择,但是在其之上构建哪些库可以使任务变得更容易?
具体来说,我想获取从实现给定接口的基础对象直接或间接引用的所有对象。层次结构可以包含循环,尽管我正在查找的对象将形成 DAG,因此理想情况下我希望它们按拓扑顺序返回。
I'm looking for ways to traverse an arbitrary object hierarchy in Java. Obviously, using the reflection API directly is one option but what libraries are built on top of it that could make the task easier?
Specifically I want to grab all the objects referenced directly or indirectly from a base object that implement a given interface. The hierarchy can contain loops, although the objects I'm looking for will form a DAG, so ideally I'd want them to be returned in topological order.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你看过 Apache BeanUtils 了吗? -> http://commons.apache.org/beanutils/
我不知道它们是否涵盖了您的所有内容需要,但它非常好且易于使用。
Did you look at Apache BeanUtils yet? -> http://commons.apache.org/beanutils/
I don't know if they cover everything you need but it's quite good and easy to use.
这是一个建议。如果您的对象实现了
MyObject
接口,则以下代码将遍历所有组件并将其提取到map
中。Here is a suggestion. If you have your objects implement the
MyObject
interface, the following will traverse and extract all components to amap
.