6

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

Suppose we have a simple java class called pojo:

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.getProperty call works fine, and the second one will throw the NoSuchMethodExeption.

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

Junaid
  • 2,572
  • 6
  • 41
  • 77
nadouani
  • 115
  • 1
  • 3
  • 8

4 Answers4

6

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".

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"
Xavi López
  • 27,550
  • 11
  • 97
  • 161
4

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

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

To Solve your problem, two solutions:

  • use property name "ADate" instead
  • change your accessors method names to getaDate() and setaDate(Date dateToSet)

As Xavi said it is a reported bug

Fred
  • 4,846
  • 1
  • 23
  • 21
  • 1
    you're right, it was just a typo error, I edited the question :) – nadouani Sep 23 '11 at 13:18
  • 2
    @Fred +1 Thanks for your credit, but it's not exactly a bug. It's [java.beans.Introspector](http://java.sun.com/j2se/1.5.0/docs/api/java/beans/Introspector.html)'s behavior, following the [Java Beans Specification](http://java.sun.com/javase/technologies/desktop/javabeans/docs/spec.html), because of which the names used to access properties are derived out of the getter/setter methods no matter how's the property named in the bean. The standard states that the first letter will be converted to lowercase **except** if the first two letters are uppercase. In this case the name remains unchanged. – Xavi López Sep 23 '11 at 19:22
  • 1
    @Fred Sorry for all the fuss, but you know, when [duty calls](http://xkcd.com/386)... – Xavi López Sep 23 '11 at 19:26
2

Try

PropertyUtils.getProperty(p, "ADate");

instead of

PropertyUtils.getProperty(p, "aDate");
Jesper
  • 202,709
  • 46
  • 318
  • 350
2

May be you need using:

PropertyUtils.getProperty(p, "ADate");

where A in UPPERCASE

Ankar
  • 94
  • 2
  • getters and setters [link](http://stackoverflow.com/questions/2036970/tutorial-on-getters-and-setters) – Ankar Sep 23 '11 at 13:27