咖啡伴侣

呆在上海
posts - 163, comments - 156, trackbacks - 0, articles - 2

用XMLEncode输出时候,有BigDecimal有时候不好使。

Posted on 2008-03-28 10:07 oathleo 阅读(526) 评论(0)  编辑  收藏 所属分类: Java

用XMLEncode输出时候,如果有BigDecimal有时候不好使。
原因是:如果类的变量在定义时候有初始值,而不是null,就必须要重载DefaultPersistenceDelegate的mutatesTo方法。
关于这个说明,在网上这里可以看到:
http://forum.java.sun.com/thread.jspa?threadID=631299&messageID=3642493

有兴趣的可以看看:
//setup big decimal delegate.
          DefaultPersistenceDelegate bigDecimalDelegate = new DefaultPersistenceDelegate() {
              protected Expression instantiate(Object oldInstance, Encoder out) {
                  BigDecimal decimal = (BigDecimal) oldInstance;
                  return new Expression(oldInstance, oldInstance.getClass(), "new", new Object[] {decimal.toString()});
              }
              //must override this method.
              // see http://forum.java.sun.com/thread.jspa?threadID=631299&messageID=3642493
              protected boolean mutatesTo(Object oldInstance, Object newInstance) {
                  return oldInstance.equals(newInstance);
              }
              //--- Joshua
          };

网上的牛人说:

This works for BigDecimal properties that aren't initialized, i.e. null. But if you initialize the property, then this won't work unless you override mutatesTo in addition to instantiate mentioned above:

protected boolean mutatesTo(Object oldInstance, Object newInstance) {
return oldInstance.equals(newInstance);
}


只有注册用户登录后才能发表评论。


网站导航: