java 尝试获取简单的属性值时 PropertyUtils.getProperty 失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7529411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:21:01  来源:igfitidea点击:

PropertyUtils.getProperty fails when trying to get a simple property value

javaapache-commons-beanutils

提问by nadouani

I've a strange issue with the PropertyUtils.getProperty(bean, fieldName)method, where I got a java.lang.NoShuchMethodException.

我的PropertyUtils.getProperty(bean, fieldName)方法有一个奇怪的问题,我得到了一个java.lang.NoShuchMethodException.

Suppose we have a simple java class called pojo:

假设我们有一个名为 pojo 的简单 java 类:

public class Pojo {
    public java.util.Date aDate;
    public java.util.Date theDate;

    public Pojo(){}
}

and a caller class like

和一个呼叫者类

public class TestPojo{
    public static void main(String[] args){
        Pojo p = new Pojo();
        p.setADate(new Date());
        p.setTheDate(new Date());

        PropertyUtils.getProperty(p, "theDate");
        PropertyUtils.getProperty(p, "aDate");
    }
}

The first PropertyUtils.getPropertycall works fine, and the second one will throwthe NoSuchMethodExeption.

第一次PropertyUtils.getProperty调用工作正常,而第二个会throwNoSuchMethodExeption

I would like to know if I'm missing something stupid or it's really a bug :)

我想知道我是否遗漏了一些愚蠢的东西或者它真的是一个错误:)

采纳答案by Fred

I don't understand how PropertyUtils.getProperty(p, "TheDate");could work since the name of the property is not correct.

我不明白如何PropertyUtils.getProperty(p, "TheDate");工作,因为属性的名称不正确。

Try this:

试试这个:

public class TestPojo{
    public static void main(String[] args){
        Pojo p = new Pojo();
        p.setADate(new Date());
        p.setTheDate(new Date());

        PropertyUtils.getProperty(p, "theDate");
        PropertyUtils.getProperty(p, "aDate");
    }
}

Link to the PropertyUtils method

链接到PropertyUtils 方法

To Solve your problem, two solutions:

要解决您的问题,有两种解决方案:

  • use property name "ADate" instead
  • change your accessors method names to getaDate() and setaDate(Date dateToSet)
  • 改用属性名称“ADate”
  • 将访问器方法名称更改为 getaDate() 和 setaDate(Date dateToSet)

As Xavi said it is a reported bug

正如哈维所说,这是一个已报告的错误

回答by Xavi López

Take a look at this bug report

看看这个错误报告

The Java Bean Specification states in section "8.8 Capitalization of inferred names" that when the first character is converted to lowercase unless the first two characters are both uppercase then the property name is "unchanged".

Java Bean 规范在“8.8 推断名称的大写”一节中指出,当第一个字符被转换为小写时,除非前两个字符都是大写,那么属性名称是“不变的”。

Adapting the rest for you (in italics):

为您调整其余部分(斜体):

So when you have a getter method named "getADate"this is translated into property name "ADate"and not "aDate".

So to resolve your issue you have two choices:

  • use property name "ADate"instead or
  • change you method names to "getaDate"and "setaDate"

因此,当您有一个名为"getADate"的 getter 方法时,它会被转换为属性名称"ADate"而不是"aDate"

因此,要解决您的问题,您有两种选择:

  • 使用属性名称“ADate”代替或
  • 将您的方法名称更改为“getaDate”“setaDate”

回答by Ankar

May be you need using:

可能你需要使用:

PropertyUtils.getProperty(p, "ADate");

where A in UPPERCASE

其中 A 大写

回答by Jesper

Try

尝试

PropertyUtils.getProperty(p, "ADate");

instead of

代替

PropertyUtils.getProperty(p, "aDate");