Beanutils.copyProperties的异常问题

Beanutils.copyProperties的异常问题

Beanutils.copyProperties有两种异常

Beanutils.copyProperties 异常一: No origin bean specified
Beanutils.copyProperties 异常二: No destination bean specified

源码跟踪

通过阅读Beanutils.copyProperties发现它内部调用BeanUtilsBean.getInstance().copyProperties(dest, orig)方法

  • Beanutils.copyProperties() 源码
/**
 - 对于属性名称相同的所有情况,将属性值从原始bean复制到目标bean。 
 - 有关更多细节,请参见BeanUtilsBean。 
 - 参数: dest —被修改属性的目标bean 
 -       orig —被检索属性的起源bean
 */
public static void copyProperties(final Object dest, final Object orig)
        throws IllegalAccessException, InvocationTargetException {

        BeanUtilsBean.getInstance().copyProperties(dest, orig);
    }
  • BeanUtilsBean.copyProperties() 源码
public void copyProperties(final Object dest, final Object orig)
       throws IllegalAccessException, InvocationTargetException {

       // Validate existence of the specified beans
       if (dest == null) {
           throw new IllegalArgumentException
                   ("No destination bean specified");
       }
       if (orig == null) {
           throw new IllegalArgumentException("No origin bean specified");
       }
       if (log.isDebugEnabled()) {
           log.debug("BeanUtils.copyProperties(" + dest + ", " +
                     orig + ")");
       }

       // Copy the properties, converting as necessary
       if (orig instanceof DynaBean) {
           final DynaProperty[] origDescriptors =
               ((DynaBean) orig).getDynaClass().getDynaProperties();
           for (DynaProperty origDescriptor : origDescriptors) {
               final String name = origDescriptor.getName();
               // Need to check isReadable() for WrapDynaBean
               // (see Jira issue# BEANUTILS-61)
               if (getPropertyUtils().isReadable(orig, name) &&
                   getPropertyUtils().isWriteable(dest, name)) {
                   final Object value = ((DynaBean) orig).get(name);
                   copyProperty(dest, name, value);
               }
           }
       } else if (orig instanceof Map) {
           @SuppressWarnings("unchecked")
           final
           // Map properties are always of type <String, Object>
           Map<String, Object> propMap = (Map<String, Object>) orig;
           for (final Map.Entry<String, Object> entry : propMap.entrySet()) {
               final String name = entry.getKey();
               if (getPropertyUtils().isWriteable(dest, name)) {
                   copyProperty(dest, name, entry.getValue());
               }
           }
       } else /* if (orig is a standard JavaBean) */ {
           final PropertyDescriptor[] origDescriptors =
               getPropertyUtils().getPropertyDescriptors(orig);
           for (PropertyDescriptor origDescriptor : origDescriptors) {
               final String name = origDescriptor.getName();
               if ("class".equals(name)) {
                   continue; // No point in trying to set an object's class
               }
               if (getPropertyUtils().isReadable(orig, name) &&
                   getPropertyUtils().isWriteable(dest, name)) {
                   try {
                       final Object value =
                           getPropertyUtils().getSimpleProperty(orig, name);
                       copyProperty(dest, name, value);
                   } catch (final NoSuchMethodException e) {
                       // Should not happen
                   }
               }
           }
       }

   }

源码分析

通过查看源码,我们可以清晰的看到public void copyProperties(Object dest, Object orig)方法,如果dest或orig为null,会引发以上两个异常。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值