• 【对象属性复制】BeanUtils.copyProperties(obj1, obj2);


    实现对象的属性值复制,只会复制命名相同的文件。
    
    import org.springframework.beans.BeanUtils;
    BeanUtils.copyProperties(obj1, obj2);
    	/**
    	 * Copy the property values of the given source bean into the given target bean.
    	 * <p>Note: The source and target classes do not have to match or even be derived
    	 * from each other, as long as the properties match. Any bean properties that the
    	 * source bean exposes but the target bean does not will silently be ignored.
    	 * @param source the source bean
    	 * @param target the target bean
    	 * @param editable the class (or interface) to restrict property setting to
    	 * @param ignoreProperties array of property names to ignore
    	 * @throws BeansException if the copying failed
    	 * @see BeanWrapper
    	 */
    	private static void copyProperties(Object source, Object target, Class<?> editable, String... ignoreProperties)
    			throws BeansException {
    
    		Assert.notNull(source, "Source must not be null");
    		Assert.notNull(target, "Target must not be null");
    
    		Class<?> actualEditable = target.getClass();
    		if (editable != null) {
    			if (!editable.isInstance(target)) {
    				throw new IllegalArgumentException("Target class [" + target.getClass().getName() +
    						"] not assignable to Editable class [" + editable.getName() + "]");
    			}
    			actualEditable = editable;
    		}
    		PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
    		List<String> ignoreList = (ignoreProperties != null ? Arrays.asList(ignoreProperties) : null);
    
    		for (PropertyDescriptor targetPd : targetPds) {
    			Method writeMethod = targetPd.getWriteMethod();
    			if (writeMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) {
    				PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());
    				if (sourcePd != null) {
    					Method readMethod = sourcePd.getReadMethod();
    					if (readMethod != null &&
    							ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) {
    						try {
    							if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
    								readMethod.setAccessible(true);
    							}
    							Object value = readMethod.invoke(source);
    							if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
    								writeMethod.setAccessible(true);
    							}
    							writeMethod.invoke(target, value);
    						}
    						catch (Throwable ex) {
    							throw new FatalBeanException(
    									"Could not copy property '" + targetPd.getName() + "' from source to target", ex);
    						}
    					}
    				}
    			}
    		}
    	}
    
  • 相关阅读:
    Java算法之 二分搜寻法 ( 搜寻原则的代表)
    Java算法之 二分搜寻法 ( 搜寻原则的代表)
    基本选择器示例 改变颜色
    jquery 2.3.1 基本选择器
    ModelMapper:从对象到对象的映射库
    老司机带你在MySQL领域“大吉大利,晚上吃鸡”
    jquery getElementById
    jquery getElementsByName
    根据id获取元素
    win10能点开开始菜单但是不能点里面的功能
  • 原文地址:https://www.cnblogs.com/the-fool/p/11054041.html
Copyright © 2020-2023  润新知