java中使用反射查找复合类的所有属性
我有一个要求,我将获得一个对象,并且我必须准备该对象中所有属性的列表。该对象可以是复合对象。这个想法是获取 String、Integer、boolean 和 Date 类型的所有属性的列表。有一种方法可以使用反射机制并递归地执行此操作。然而,这可能会变得相当复杂。所以我只是想知道是否有任何第三方库可以使用。
I have requirement where in I will be given an object and I have to prepare a list of all the attributes in that object. The object can be a composite object. The idea is to get the list of all the attributes of type String, Integer, boolean and Date. There is a way to use reflection mechanism and doing this recursively. However, this can become quite complex. So I was just wondering, if there is any third party library that I can use.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试查看 Apache BeanUtils。特别是 PropertyUtils .getPropertyDescriptors()。警告,这仅适用于具有 getter 和 setter 的字段。
然后,您必须为在父类中找到的每个属性描述符递归地调用它。
Try looking at Apache BeanUtils. In particular PropertyUtils.getPropertyDescriptors(). Caveat, this only works for fields with getters and setters.
You'd then have to call this recursively for each property descriptor you find in the parent class.