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

com.github.hdy.common.util.Beans Maven / Gradle / Ivy

There is a newer version: 2.0.5
Show newest version
package com.github.hdy.common.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.hdy.common.exceptions.CustomException;
import org.springframework.beans.BeanUtils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public abstract class Beans {

    private Beans() {
    }

    public static  T newBean(Object obj, Class clazz) {
        return newBean(obj, clazz, null);
    }

    public static  T newBean(Object obj, Class clazz, ObjectMapper mapper) {
        return ObjectMappers.convert(obj, clazz, mapper);
    }

    public static  T newBean(Map map, Class clazz) {
        return newBean(map, clazz, null);
    }

    public static  T newBean(Map map, Class clazz, ObjectMapper mapper) {
        return ObjectMappers.convert(map, clazz, mapper);
    }


    /**
     * 复制对象
     *
     * @param source 被复制对象
     * @param target 复制到目标对象
     */
    public static void copy(Object source, Object target) {
        try {
            BeanUtils.copyProperties(source, target);
        } catch (Exception e) {
            throw new CustomException(e);
        }
    }

    /**
     * 复制对象(排除为空的对象)
     *
     * @param source 被复制对象
     * @param target 复制到目标对象
     */
    public static void copyNotNull(Object source, Object target) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Field[] fields = source.getClass().getDeclaredFields();
        List emptyList = new ArrayList<>();
        for (Field field : fields) {
            if (Strings.isNull(source.getClass().getMethod("get" + Strings.capitalize(field.getName())).invoke(source))) {
                emptyList.add(field.getName());
            }
        }
        String emptyStrs[] = new String[emptyList.size()];
        for (int i = 0; i < emptyList.size(); i++) {
            emptyStrs[i] = emptyList.get(i);
        }
        try {
            BeanUtils.copyProperties(source, target, emptyStrs);
        } catch (Exception e) {
            throw new CustomException(e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy