Java introspection: object to map

后端 未结 7 947
无人及你
无人及你 2020-11-28 05:52

I have a Java object obj that has attributes obj.attr1, obj.attr2 etc. The attributes are possibly accessed through an extra level of

相关标签:
7条回答
  • 2020-11-28 06:32

    Use Apache Commons BeanUtils: http://commons.apache.org/beanutils/.

    An implementation of Map for JavaBeans which uses introspection to get and put properties in the bean:

    Map<Object, Object> introspected = new org.apache.commons.beanutils.BeanMap(object); 
    

    Note: despite the fact the API returns Map<Object, Object> (since 1.9.0), the actual class for keys in the returned map is java.lang.String

    0 讨论(0)
提交回复
热议问题