BeanUtils - 'describe'方法返回InvocationTargetException

时间:2011-04-18 19:14:22

标签: java apache-commons-beanutils invocationtargetexception

我试图在Map中获取以下实体的属性:

@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());
    }
    ....
}

通过以下内容:

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

显然currentObject PSParameter ..

每当调用describe函数时,我都会得到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)
....

根本原因如下:

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

任何暗示为什么会发生这种情况?我的意思是,我很清楚异常的根本原因,无法将字符串解析为整数,但为什么 BeanUtils 及其 describe 正在这样做?

无论如何要绕过这个或任何其他选择?谢谢!

1 个答案:

答案 0 :(得分:1)

来自Javadoc

  

返回指定bean提供read方法的整个属性集。此映射包含为其提供读取方法的所有属性的String转换属性值

这意味着describe()方法返回给定bean的属性值到属性值的映射,调用getter来获取后者。这意味着在调用describe()时,任何getter都不会抛出异常。