# Java 对象初始化、拷贝工具

📆 2021-12-20 16:53

# 实现

@Slf4j
public class ClassUtils {
    private static <T> T _initClass(Class<T> tClass){
        try {
            T obj = tClass.newInstance();
            Field[] field = tClass.getDeclaredFields();
            for (Field f : field) {
                f.setAccessible(true);
                //获取属性名
                String name = f.getName();
                name = name.substring(0, 1).toUpperCase() + name.substring(1);
                //获取属性类型
                String type = f.getGenericType().toString();
                Method method = null;
                try {
                    try {
                        if (type.equals("class java.lang.String")) {
                            method = obj.getClass().getMethod("set" + name, String.class);
                            method.invoke(obj, "");
                        }
                        if (type.equals("class java.lang.Integer")) {
                            method = obj.getClass().getMethod("set" + name, Integer.class);
                            method.invoke(obj, 0);
                        }
                        if (type.equals("class java.lang.Long")) {
                            method = obj.getClass().getMethod("set" + name, Long.class);
                            method.invoke(obj, 0L);
                        }
                        if (type.equals("class java.lang.Double")) {
                            method = obj.getClass().getMethod("set" + name, Double.class);
                            method.invoke(obj, 0.00);
                        }
                        if (type.equals("class java.lang.Boolean")) {
                            method = obj.getClass().getMethod("set" + name, Boolean.class);
                            method.invoke(obj, false);
                        }
                        if (type.equals("class java.util.Date")) {
                            method = obj.getClass().getMethod("set" + name, Date.class);
                            method.invoke(obj, new Date());
                        }
                        if (type.equals("int")) {
                            method = obj.getClass().getMethod("set" + name, int.class);
                            //给对象的这个属性赋值 int类型
                            method.invoke(obj, 0);
                        }
                    } catch (InvocationTargetException e) {
                        log.error("ClassUtils解析出错",e);
                    }
                } catch (NoSuchMethodException | IllegalAccessException e) {
                    log.error("ClassUtils解析出错",e);
                }
            }
            return obj;
        }catch (Exception e){
            log.error("初始化class出错",e);
            return null;
        }
    }

    private static String[] _getNullPropertyNames (Object source) {
        final BeanWrapper src = new BeanWrapperImpl(source);
        java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

        Set<String> emptyNames = new HashSet<String>();
        for(java.beans.PropertyDescriptor pd : pds) {
            Object srcValue = src.getPropertyValue(pd.getName());
            if (srcValue == null) emptyNames.add(pd.getName());
        }
        String[] result = new String[emptyNames.size()];
        return emptyNames.toArray(result);
    }

    public static void copyPropertiesIgnoreNull(Object src, Object target){
        BeanUtils.copyProperties(src, target, _getNullPropertyNames(src));
    }

    public static <T> T copyPropertiesIgnoreNull(Object src,Class<T> clazz) {
        T obj=_initClass(clazz);
        if(src==null) return obj;
        copyPropertiesIgnoreNull(src, obj);
        return obj;
    }
}

# 使用

User a=new User("username","password");
User aCopy=ClassUtils.copyPropertiesIgnoreNull(a,User.class);

User b=new User("mingzi","mima");
User bCopy=new User();
ClassUtils.copyPropertiesIgnoreNull(b,bCopy);
最后更新于: 12/20/2021, 4:57:02 PM