Jaa*_*nus 6 java reflection apache-commons-beanutils
我将变量值设置为null,但有问题:
public class BestObject {
private Timestamp deliveryDate;
public void setDeliveryDate(Timestamp deliveryDate) {
this.deliveryDate = deliveryDate;
}
}
BeanUtils.setProperty(new BestObject(), "deliveryDate", null); // usually the values are not hardcoded, they come from configuration etc
Run Code Online (Sandbox Code Playgroud)
这是错误:
org.apache.commons.beanutils.ConversionException: No value specified
at org.apache.commons.beanutils.converters.SqlTimestampConverter.convert(SqlTimestampConverter.java:148)
at org.apache.commons.beanutils.ConvertUtils.convert(ConvertUtils.java:379)
at org.apache.commons.beanutils.BeanUtils.setProperty(BeanUtils.java:999)
Run Code Online (Sandbox Code Playgroud)
基本上它试图将java.sql.Timestamp值设置为null,但由于某种原因它不起作用.
另一方面,我使用反射包装器BeanUtils(http://commons.apache.org/proper/commons-beanutils/),也许这可以用简单的反射?
我设法用标准反射做到了.
java.lang.reflect.Field prop = object.getClass().getDeclaredField("deliveryDate");
prop.setAccessible(true);
prop.set(object, null);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10469 次 |
| 最近记录: |