简体   繁体   中英

Get bean property getter or setter by reflection?

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 retrieve the getter for this document?

Is there a better way than prepending "get" and capitalizing the "a" in age , and looking for a method of that name via reflection?

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM