javaapache-commons-beanutilsinvocationtargetexception

BeanUtils - 'describe' method returning InvocationTargetException


I am trying to obtain in a Map the properties of the following Entity:

@Entity
@Table(name = "ps_parameter")
@NamedQueries({Named Queries Here..})
public class PSParameter
    implements Serializable
{
    //~ Static variables/initializers ----------------------------------------------------
    ....

    //~ Instance variables ---------------------------------------------------------------

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private Integer id;

    @Column(name = "name", nullable = false)
    private String name;

    @Column(name = "paramValue", nullable = false)
    private String paramValue;

    //~ Constructors ---------------------------------------------------------------------
    ....

    //~ Methods --------------------------------------------------------------------------
    public Double getDoubleValue()
    {
        return Double.parseDouble(getParamValue());
    }

    public Integer getIntegerValue()
    {
        return Integer.parseInt(getParamValue());
    }
    ....
}

Through the following:

 ....
 try
   {
   Map propertiesCurrentObject = BeanUtils.describe(currentObject);
   ....
   }
 ....

Clearly currentObject is a PSParameter..

Whenever the describe function is invoked, I get an InvocationTargetException:

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:2170)
    at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1332)
    at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:770)
    at org.apache.commons.beanutils.BeanUtilsBean.getNestedProperty(BeanUtilsBean.java:715)
    at org.apache.commons.beanutils.BeanUtilsBean.getProperty(BeanUtilsBean.java:741)
    at org.apache.commons.beanutils.BeanUtilsBean.describe(BeanUtilsBean.java:514)
    at org.apache.commons.beanutils.BeanUtils.describe(BeanUtils.java:185)
    at xxx.yyy.ejb.core.facade.AuditEntryFacade.getChangesBetweenTwoObjects(AuditEntryFacade.java:1198)
....

Whose root cause is the following:

Caused by: java.lang.NumberFormatException: For input string: "true"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:481)
    at java.lang.Integer.parseInt(Integer.java:514)
    at xxx.yyy.ejb.core.entity.PSParameter.getIntegerValue(PSParameter.java:169)
    ... 87 more

Any hint why is this happening? I mean, it is clear for me the root cause of the exception, not being able to Parse a String to an Integer, but why BeanUtils and its describe is doing so?

Anyway to circumvent this, or any alternative? Thanks!


Solution

  • From Javadoc:

    Return the entire set of properties for which the specified bean provides a read method. This map contains the to String converted property values for all properties for which a read method is provided

    This means that describe() method returns a map from property to property value of a given bean, calling getter to obtain the latter. This means none of your getters should throw an exception when describe() is called.