private static void copyPropertiesByNotNull(Object source, Object target, List<String> copyProperties) throws InvocationTargetException, IllegalAccessException { PropertyDescriptor[] targetPds = BeanUtils.getPropertyDescriptors(target.getClass()); for (PropertyDescriptor targetPd : targetPds) { //只複製須要的字段,字段名稱包含枚舉值時就須要複製值 if (copyProperties.stream().anyMatch(vo -> targetPd.getName().toLowerCase().contains(vo.toLowerCase()))) { PropertyDescriptor sourcePd = BeanUtils.getPropertyDescriptor(source.getClass(), targetPd.getName()); Object sourceValue = sourcePd.getReadMethod().invoke(source); Object targetValue = targetPd.getReadMethod().invoke(target); //只有目標對象屬性沒值而且源對象屬性有值才進行復制操做 if (targetValue == null && sourceValue != null) { targetPd.getWriteMethod().invoke(target, sourcePd.getReadMethod().invoke(source)); } } } }