简体   繁体   中英

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 . I tried commons-beanutil's BeanUtils.describe(obj) , but it needs an object instance. But what if I only have that class , without a public no-arg constructor . I cannot use clazz.newInstance() to generate an object.

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. Hava a look at the java doc of Class .

For example using reflection Demo.class.getMethods(); to get all getter methods of a Class called Demo (without instanciating it.)

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)可能正是您想要的。

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