Share a JavaBean property copy tool class, super easy to use.

One, the code piece

Share a JavaBean attribute copy tool class, not much nonsense, just go to the code:


import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 属性拷贝工具,支持继承,要拷贝的属性必须有get,set的公用方法
 *
 * @author weiyuan
 * @version 1.0
 * @date 2014/11/12 14:05
 */
public class PropertyClone {
    
    
    private final static String SET_METHOD_FIX = "set";

    private final static String GET_METHOD_FIX = "get";

    private PropertyClone() {
    
    

    }

    private static <T> Map<String, Method> findPublicMethod(T t, String fix) {
    
    
        Map<String, Method> methodMap = new HashMap<String, Method>();
        Class<?> clz = t.getClass();
        while (!clz.equals(Object.class)) {
    
    
            Method[] methods = clz.getDeclaredMethods();
            for (Method method : methods) {
    
    
                int status = method.getModifiers();
                if (!Modifier.isPublic(status)) {
    
    
                    continue;
                }
                if (method.getName().startsWith(fix)) {
    
    
                    String field = method.getName().substring(3, 4).toLowerCase() + method.getName().substring(4);
                    if (methodMap.keySet().contains(field)) {
    
    
                        continue;
                    }
                    methodMap.put(field, method);
                }
            }
            clz = clz.getSuperclass();
        }
        return methodMap;
    }

    /**
     * <pre>
     * 属性拷贝工具方法
     * </pre>
     *
     * @param <T>  对象类型
     * @param dest 目标对象
     * @param orig 源对象
     * @param copyNullFlag 当属性值为null时,是否赋值. true:赋值,false:不赋值
     */
    public static <T, K> void copyProperties(T dest, K orig, Boolean copyNullFlag) {
    
    
        copyProperties(dest, orig, copyNullFlag, null);
    }

    /**
     * @param <T>
     * @param <K>
     * @param dest
     * @param orig
     * @param copyNullFlag
     * @param uncope       非克隆属性名
     */
    public static <T, K> void copyProperties(T dest, K orig, Boolean copyNullFlag, Set<String> uncope) {
    
    
        Map<String, Method> destSetMethodMap = findPublicMethod(dest, SET_METHOD_FIX);
        // 源对象中的所有get方法集合
        Map<String, Method> origGetMethodMap = findPublicMethod(orig, GET_METHOD_FIX);
        Set<String> fields = origGetMethodMap.keySet();
        boolean flag = (uncope == null || uncope.size() <= 0) ? false : true;
        for (String field : fields) {
    
    
            if (flag && uncope.contains(field)) {
    
    
                continue;
            }
            Method getMethod = origGetMethodMap.get(field);
            try {
    
    
                Object value = getMethod.invoke(orig, new Object[]{
    
    });
                if (!copyNullFlag) {
    
    
                    if (value == null) {
    
    
                        continue;
                    }
                }
                Method setMethod = destSetMethodMap.get(field);
                if (setMethod != null)
                    setMethod.invoke(dest, value);
            } catch (Exception e) {
    
    
                throw new RuntimeException("实体属性克隆发生异常");
            }
        }
    }
}


Second, use examples

//将origEngity的属性拷贝到destEntity中,false-不拷贝空属性
PropertyClone.copyProperties(destEntity, origEngity, false);

Three, finally

If you don’t want to take it away, give it a reward. If you don’t give a reward, at least pay attention to the blogger, haha, haha, hehe.

Guess you like

Origin blog.csdn.net/datuanyuan/article/details/109100013