All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.tukeof.common.util.bean.BeanModUtil Maven / Gradle / Ivy

The newest version!
package com.tukeof.common.util.bean;

import com.tukeof.common.util.ObjectUtil;
import com.tukeof.common.util.ReflectUtil;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Create by tuke on 2019-01-23
 */
public class BeanModUtil {

    // ==== ==== ==== ====    ==== ==== ==== ====    ==== ==== ==== ====
    // bean copy
    public static  T copy(Object source, Class clazz) {
        ObjectUtil.checkNotNull(source, "source");
        T target = null;
        try {
            target = clazz.newInstance();
            copyProperties(source, target);
        } catch (Throwable ignored) {
        }

        return target;
    }

    public static void copyProperties(Object source, Object target) throws IllegalAccessException {
        Map sourceFieldMap = new HashMap<>();
        ReflectUtil.retrieveFields(source.getClass(), sourceFieldMap);

        Map targetFieldMap = new HashMap<>();
        ReflectUtil.retrieveFields(target.getClass(), targetFieldMap);

        Set targetFields = targetFieldMap.keySet();
        for (String sourceFieldName : sourceFieldMap.keySet()) {
            if (targetFields.contains(sourceFieldName)) {

                Field targetField = targetFieldMap.get(sourceFieldName);
                Field sourceField = sourceFieldMap.get(sourceFieldName);

                targetField.setAccessible(true);
                sourceField.setAccessible(true);
                targetField.set(target, sourceField.get(source));
            }
        }

    }

    // ==== ==== ==== ====    ==== ==== ==== ====    ==== ==== ==== ====
    // bean op
    public static  T nullify(T bean) {
        Class clazz = bean.getClass();
        List fields = new ArrayList<>();
        ReflectUtil.retrieveFields(clazz, fields);
        if (fields.isEmpty()) return bean;
        for (Field field : fields) {
            nullifyField(bean, field);
        }
        return bean;
    }

    public static void nullifyField(Object bean, Field field) {
        if (ReflectUtil.isFinalOrStaticField(field)) {
            return;
        }

        field.setAccessible(true);
        Class type = field.getType();
        try {
            if (!type.isPrimitive()) {
                field.set(bean, null);
            } else {
                if (type.equals(boolean.class)) {
                    field.setBoolean(bean, false);
                } else if (type.equals(byte.class)) {
                    field.setByte(bean, (byte) 0);
                } else if (type.equals(short.class)) {
                    field.setShort(bean, (short) 0);
                } else if (type.equals(char.class)) {
                    field.setChar(bean, (char) 0);
                } else if (type.equals(int.class)) {
                    field.setInt(bean, 0);
                } else if (type.equals(long.class)) {
                    field.setLong(bean, 0L);
                } else if (type.equals(float.class)) {
                    field.setFloat(bean, 0.F);
                } else if (type.equals(double.class)) {
                    field.setDouble(bean, 0.);
                }
            }
        } catch (IllegalAccessException ignored) {
        }
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy