简体   繁体   English

如何在没有公共no-arg构造函数的情况下获取pojo类的属性名称?

[英]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 . 让我们看看ClassJava文档

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.

相关问题 无法获得公共无参数构造函数 - Unable to get public no-arg constructor JAXB需要一个公共的无参数构造函数? - What JAXB needs a public no-arg constructor for? Java类如何没有no-arg构造函数? - How can a Java class have no no-arg constructor? IIOException如何没有no-arg构造函数? - How IIOException has no no-arg constructor? Hibernate和no-arg构造函数 - Hibernate and no-arg constructor 有效 Java。 可序列化生成器模式(如何添加公共无参数构造函数?) - Effective Java. Serializable Builder pattern (how to add public no-arg constructor?) 是否有可能在java中使用反射创建没有arg构造函数的类的“空白”实例? - Is it possible in java to create 'blank' instance of class without no-arg constructor using reflection? 由于谷歌自动服务处理器,无法编译我的 javafx 和 selenium 项目(“无法获取公共无参数构造函数”) - Can't compile my javafx and selenium project due to google autoservice processor ("Unable to get public no-arg constructor") 无参数构造函数的目的是什么? - What is the purpose of a no-arg constructor? hibernate如何使用no-arg构造函数初始化final字段? - How hibernate is initializing final fields using the no-arg constructor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM