java 通过反射获取 bean 属性 getter 或 setter?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6845592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get bean property getter or setter by reflection?
提问by C. Ross
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?
假设我有一个类型为 的对象的句柄,并且配置告诉我它有一个类型int
为 name的 bean 属性age
。如何检索此文档的 getter?
Is there a better way than prepending "get" and capitalizing the "a" in age
, and looking for a method of that name via reflection?
有没有比在 前面加上“get”并大写“a”age
并通过反射寻找该名称的方法更好的方法?
回答by Sergey Aslanov
Take a look at java.beans.Introspector
. This class allows you to get the list of properties on a class.
看看java.beans.Introspector
。此类允许您获取类的属性列表。
If you know property name you can call
如果你知道财产名称,你可以打电话
Method getter = new PropertyDescriptor(propertyName, beanClass).getReadMethod();
See Also:
也可以看看: