Get bean property getter or setter by reflection?

前端 未结 1 382
太阳男子
太阳男子 2020-12-30 05:47

Suppose I have a handle on an object of type , and I\'m told by configuration that it has a bean property of type int with the name age. How can I

1条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-30 06:14

    Take a look at java.beans.Introspector. This class allows you to get the list of properties on a class.

    If you know property name you can call

    Method getter = new PropertyDescriptor(propertyName, beanClass).getReadMethod();
    

    See Also:

    • java.beans.PropertyDescriptor
    • java.lang.reflect.Method

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