开发者

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 ? 开发者_JAVA技巧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.


I've never used it (or anything in java.beans, for that matter), but java.beans.Introspector.getBeanInfo(Class) may be what you're looking for.


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);
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜