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 reflectionDemo.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);
}
}