使用beanUtils复制属性


public class BeanTest {


    /**
     * 测试项目的基本信息
    * @throws Exception
     */
    @Test
    public void test01() throws Exception {


        Student student = new Student();
        student.setName("cbw");
        student.setAge(18);
        student.setMajor("machaniacal");
        student.setParent("1212");



        Person person = new Person();
        person.setAge(100);
        person.setName("太上老君");
        person.setMajor("练仙丹");
        person.setWife("炉子");

        System.out.println("-------------变化前-------------");

        System.out.println(student);
//        System.out.println(person);

        BeanUtils.copyProperties(person, student);

        System.out.println("-------------变化后-------------");

        System.out.println(student);
//        System.out.println(person);


//        -------------变化前-------------
//        Student [name=cbw, major=machaniacal, age=18, parent=1212]
//        -------------变化后-------------
//        Student [name=太上老君, major=练仙丹, age=100, parent=1212]


    }

}

beanUtils 复制属性


    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);
                            }
//读取source的值                            Object value = readMethod.invoke(source);
                            if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
                                writeMethod.setAccessible(true);
                            }
//其实是setter方法                            writeMethod.invoke(target, value);
                        }
                        catch (Throwable ex) {
                            throw new FatalBeanException(
                                    "Could not copy property '" + targetPd.getName() + "' from source to target", ex);
                        }
                    }
                }
            }
        }
    }

results matching ""

    No results matching ""