BeanUtils - “描述”返回 InvokingTargetException 的方法

发布于 2024-11-02 06:53:42 字数 3019 浏览 1 评论 0原文

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

@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调用函数时,我收到一个 InitationTargetException

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 会这样做呢?

无论如何要规避这个,或者有其他选择吗?谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是偏爱你 2024-11-09 06:53:42

来自 Javadoc

返回指定 bean 为其提供读取方法的整个属性集。该映射包含提供了读取方法的所有属性的字符串转换属性值

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文