[英]How to get property names of a pojo class without public no-arg constructor?
I want to get an array(or list) of a POJO's property names . 我想要一个POJO属性名称的数组(或列表)。 I tried commons-beanutil's
BeanUtils.describe(obj)
, but it needs an object instance. 我尝试了commons-beanutil的
BeanUtils.describe(obj)
,但是它需要一个对象实例。 But what if I only have that class , without a public no-arg constructor . 但是,如果我只有那个类,而没有公共的无参数构造函数,该怎么办? I cannot use
clazz.newInstance()
to generate an object. 我无法使用
clazz.newInstance()
生成对象。
How should I solve it ? 我该如何解决? Is there any libraries that can dig into a class and pass property names ?
是否有任何库可以挖掘一个类并传递属性名?
(I know I can use reflection to manually parse the class structure , but I am looking for a handy library) (我知道我可以使用反射来手动解析类结构,但是我正在寻找一个方便的库)
Thanks. 谢谢。
Java has its build in reflection utils - which you can use. Java具有反射实用程序的构建-您可以使用。 Hava a look at the java doc of Class .
让我们看看Class的Java文档 。
For example using reflection Demo.class.getMethods();
例如,使用反射
Demo.class.getMethods();
to get all getter methods of a Class called Demo
(without instanciating it.) 获取名为
Demo
的类的所有getter方法(无需实例化)。
List<Method> allGetterMethodsOfClassDemo() = new ArrayList<Method>();
for(Method method : Demo.class.getMethods()){
if(method.getName().startsWith("get") || method.getName().startsWith("is")) {
allGetterMethodsOfClassDemo.add(method);
}
}
我从来没有使用过它(或者关于java.beans
任何东西),但是java.beans.Introspector.getBeanInfo(Class)
可能正是您想要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.