BeanUtils工具类

package net.iclassmate.modules.util;

import java.beans.PropertyDescriptor;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.BeanWrapperImpl;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService;

import net.iclassmate.modules.core.metadata.NotInCopy;
import net.iclassmate.modules.core.spring.converter.EnumToEnumConverter;
import net.iclassmate.modules.core.spring.converter.SerializableToSerializableConverter;

public class BeanUtils {

    static DefaultConversionService conversionService = new DefaultConversionService();

    static {
        conversionService.addConverter(new EnumToEnumConverter());
        conversionService.addConverter(new SerializableToSerializableConverter());
    }

    public static boolean hasProperty(Class<?> clazz, String name) {
        if (org.springframework.beans.BeanUtils.getPropertyDescriptor(clazz, name) != null) return true;
        return false;
    }

    public static void copyPropertiesIfNotNull(Object source, Object target, String... properties) {
        if (properties.length == 0) return;
        BeanWrapperImpl bws = new BeanWrapperImpl(source);
        bws.setConversionService(conversionService);
        BeanWrapperImpl bwt = new BeanWrapperImpl(target);
        bwt.setConversionService(conversionService);
        for (String propertyName : properties) {
            Object value = bws.getPropertyValue(propertyName);
            if (value != null) bwt.setPropertyValue(propertyName, value);
        }
    }

    public static void copyProperties(Map<String, Object> source, Object target, String... ignoreProperties) {
        BeanWrapperImpl bw = new BeanWrapperImpl(target);
        bw.setConversionService(conversionService);
        for (Map.Entry<String, Object> entry : source.entrySet()) {
            if (bw.isWritableProperty(entry.getKey())) bw.setPropertyValue(entry.getKey(), entry.getValue());
        }
    }

    public static void copyProperties(Object source, Object target, boolean ignoreNullValue, boolean ignoreNotInCopy,
            String... ignoreProperties) {
        Set<String> ignores = new HashSet<>();
        if (ignoreNotInCopy) ignores.addAll(AnnotationUtils.getAnnotatedPropertyNames(source.getClass(), NotInCopy.class));
        ignores.addAll(Arrays.asList(ignoreProperties));
        normalizeCollectionFields(source);
        BeanWrapperImpl bws = new BeanWrapperImpl(source);
        bws.setConversionService(conversionService);
        PropertyDescriptor[] sourcePds = bws.getPropertyDescriptors();
        BeanWrapperImpl bwt = new BeanWrapperImpl(target);
        bwt.setConversionService(conversionService);
        PropertyDescriptor[] targetPds = bwt.getPropertyDescriptors();
        for (PropertyDescriptor sourcePd : sourcePds) {
            if (sourcePd.getReadMethod() == null) continue;
            String name = sourcePd.getName();
            if (ignores.contains(name)) continue;
            PropertyDescriptor targetPd = null;
            for (PropertyDescriptor pd : targetPds) {
                if (pd.getName().equals(name)) {
                    targetPd = pd;
                    break;
                }
            }
            if (targetPd == null || targetPd.getWriteMethod() == null) continue;
            Object value = bws.getPropertyValue(name);
            if (!ignoreNullValue || value != null) bwt.setPropertyValue(name, value);
        }
    }

    public static void copyProperties(Object source, Object target, boolean ignoreNotInCopy, String... ignoreProperties) {
        copyProperties(source, target, false, ignoreNotInCopy, ignoreProperties);
    }

    public static void copyProperties(Object source, Object target, String... ignoreProperties) {
        copyProperties(source, target, false, true, ignoreProperties);
    }

    public static Object convert(Class<?> beanClass, String propertyName, String value) {
        try {
            return convert(beanClass.newInstance(), propertyName, value);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static Object convert(Object bean, String propertyName, String value) {
        BeanWrapperImpl bw = new BeanWrapperImpl(bean);
        bw.setConversionService(conversionService);
        ConversionService cs = ApplicationContextUtils.getBean(ConversionService.class);
        if (cs != null) bw.setConversionService(cs);
        bw.setPropertyValue(propertyName, value);
        return bw.getPropertyValue(propertyName);
    }

    public static PropertyDescriptor getPropertyDescriptor(Class<?> beanClass, String propertyName) {
        if (propertyName.indexOf('.') == -1) return org.springframework.beans.BeanUtils.getPropertyDescriptor(beanClass, propertyName);
        String[] arr = propertyName.split("\\.");
        PropertyDescriptor pd = null;
        int i = 0;
        Class<?> clazz = beanClass;
        while (i < arr.length) {
            pd = BeanUtils.getPropertyDescriptor(clazz, arr[i]);
            if (pd == null) return null;
            clazz = pd.getPropertyType();
            i++;
        }
        return pd;
    }

    public static void setPropertyValue(Object bean, String propertyName, Object propertyValue) {
        BeanWrapperImpl bw = new BeanWrapperImpl(bean);
        bw.setConversionService(conversionService);
        if (propertyName.indexOf('.') == -1) {
            bw.setPropertyValue(propertyName, propertyValue);
            return;
        }
        String[] arr = propertyName.split("\\.");
        int i = 0;
        String name = null;
        while (i < arr.length - 1) {
            if (name == null) name = arr[i];
            else name += "." + arr[i];
            Object value = bw.getPropertyValue(name);
            if (value == null) {
                try {
                    value = getPropertyDescriptor(bean.getClass(), name).getPropertyType().newInstance();
                    bw.setPropertyValue(name, value);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
            i++;
        }
        bw.setPropertyValue(propertyName, propertyValue);
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static void normalizeCollectionFields(Object bean) {
        if (!(bean instanceof Serializable)) return;
        try {
            Class<?> clazz = bean.getClass();
            while (clazz != Object.class) {
                Field[] fields = clazz.getDeclaredFields();
                for (Field f : fields) {
                    f.setAccessible(true);
                    Object value = f.get(bean);
                    if (value == null) continue;
                    if (value.getClass().getName().startsWith("java.")) continue;
                    Class<?> declaredType = f.getType();
                    if (declaredType == List.class) {
                        List newValue = new ArrayList();
                        newValue.addAll((List) value);
                        f.set(bean, newValue);
                    } else if (declaredType == Set.class) {
                        Set newValue = new LinkedHashSet();
                        newValue.addAll((Set) value);
                        f.set(bean, newValue);
                    } else if (declaredType == Map.class) {
                        Map newValue = new LinkedHashMap();
                        newValue.putAll((Map) value);
                        f.set(bean, newValue);
                    }
                }
                clazz = clazz.getSuperclass();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值