码迷,mamicode.com
首页 > 编程语言 > 详细

java beanUtilsX操作属性类

时间:2017-12-23 23:06:28      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:final   .com   org   des   apach   pop   bool   eth   getc   

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package cn.com.tisco.upad.common.util.convert;

import cn.com.tisco.upad.common.util.ArrayUtilsX;
import cn.com.tisco.upad.common.util.validation.Preconditions;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.beanutils.PropertyUtilsBean;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.BeansException;

public final class BeanUtilsX {
    public BeanUtilsX() {
    }

    public static void copyProperty(Object bean, String name, Object value) {
        try {
            BeanUtils.copyProperty(bean, name, value);
        } catch (Exception var4) {
            throw new RuntimeException("拷贝属性失败!", var4);
        }
    }

    public static void copyProperties(Object source, Object target) {
        try {
            BeanUtils.copyProperties(target, source);
        } catch (Exception var3) {
            throw new RuntimeException("bean属性复制失败!", var3);
        }
    }

    public static void copyProperties(Object source, Object target, String... excludedProperties) {
        if(ArrayUtilsX.isEmpty(excludedProperties)) {
            try {
                BeanUtils.copyProperties(target, source);
            } catch (Exception var8) {
                throw new RuntimeException("Bean属性复制失败!", var8);
            }
        } else {
            Map sourcePropValues = describe(source);
            String[] arr$ = excludedProperties;
            int len$ = excludedProperties.length;

            for(int i$ = 0; i$ < len$; ++i$) {
                String excludedProp = arr$[i$];
                if(sourcePropValues.containsKey(excludedProp)) {
                    sourcePropValues.remove(excludedProp);
                }
            }

            populate(target, sourcePropValues);
        }
    }

    public static void copyProperties(Object source, Object target, boolean copyNull, boolean copyEmpty, String... ignoreProperties) throws BeansException {
        if(copyNull && copyEmpty) {
            copyProperties(source, target, ignoreProperties);
        } else {
            String[] nullValueProperty = getNullValueProperties(source, !copyNull, !copyEmpty);
            if(ArrayUtilsX.isEmpty(ignoreProperties) && ArrayUtilsX.isEmpty(nullValueProperty)) {
                copyProperties(source, target);
            } else {
                ignoreProperties = (String[])ArrayUtils.addAll(nullValueProperty, ignoreProperties);
                copyProperties(source, target, ignoreProperties);
            }
        }

    }

    private static String[] getNullValueProperties(Object source, boolean includeNull, boolean includeEmpty) {
        BeanWrapperImpl src = new BeanWrapperImpl(source);
        PropertyDescriptor[] pds = src.getPropertyDescriptors();
        HashSet emptyNames = new HashSet();
        PropertyDescriptor[] result = pds;
        int len$ = pds.length;

        for(int i$ = 0; i$ < len$; ++i$) {
            PropertyDescriptor pd = result[i$];
            Object srcValue = src.getPropertyValue(pd.getName());
            if(includeNull && srcValue == null) {
                emptyNames.add(pd.getName());
            }

            if(includeEmpty && srcValue instanceof String && StringUtils.isEmpty((String)srcValue)) {
                emptyNames.add(pd.getName());
            }
        }

        String[] var11 = new String[emptyNames.size()];
        return (String[])emptyNames.toArray(var11);
    }

    public static void copySpecificProperties(Object source, Object target, String[] properties) {
        if(ArrayUtils.isEmpty(properties)) {
            copyProperties(target, source);
        } else {
            Map sourceObjectMap = describe(source);
            HashMap propValueToCopy = new HashMap();
            String[] arr$ = properties;
            int len$ = properties.length;

            for(int i$ = 0; i$ < len$; ++i$) {
                String property = arr$[i$];
                if(sourceObjectMap.containsKey(property)) {
                    propValueToCopy.put(property, sourceObjectMap.get(property));
                }
            }

            populate(target, propValueToCopy);
        }
    }

    public static <T> T cloneBean(T bean) {
        try {
            return BeanUtils.cloneBean(bean);
        } catch (Exception var2) {
            throw new RuntimeException("bean克隆失败!", var2);
        }
    }

    public static <T> List<T> cloneBeanList(List<T> beans) {
        ArrayList results = new ArrayList();
        Iterator i$ = beans.iterator();

        while(i$.hasNext()) {
            Object bean = i$.next();
            results.add(cloneBean(bean));
        }

        return results;
    }

    public static Map<String, Object> describe(Object bean) {
        Map props = null;
        PropertyUtilsBean pub = new PropertyUtilsBean();

        try {
            props = pub.describe(bean);
        } catch (Exception var4) {
            throw new RuntimeException(var4);
        }

        props.remove("class");
        return props;
    }

    public static String[] getArrayProperty(Object bean, String name) {
        try {
            return BeanUtils.getArrayProperty(bean, name);
        } catch (Exception var3) {
            throw new RuntimeException("获取对象属性值失败!", var3);
        }
    }

    public static String getProperty(Object bean, String name) {
        try {
            return BeanUtils.getProperty(bean, name);
        } catch (Exception var3) {
            throw new RuntimeException("获取对象属性值失败!", var3);
        }
    }

    public static void populate(Object bean, Map<String, Object> properties) {
        Preconditions.notNull(bean, "The bean to populate should not be null.", new Object[0]);
        if(properties != null && properties.size() != 0) {
            try {
                Iterator e = properties.keySet().iterator();

                while(e.hasNext()) {
                    String propName = (String)e.next();
                    if(propName != null && !propName.isEmpty()) {
                        PropertyDescriptor pd = PropertyUtils.getPropertyDescriptor(bean, propName);
                        if(pd != null) {
                            Method writeMethod = pd.getWriteMethod();
                            Object propValue = properties.get(pd.getName());
                            if(writeMethod != null && Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
                                writeMethod.invoke(bean, new Object[]{ConvertUtils.convert(propValue, pd.getPropertyType())});
                            }
                        }
                    }
                }

            } catch (Exception var7) {
                throw new IllegalStateException("Failed to populate bean with given map.", var7);
            }
        }
    }

    public static void setProperty(Object bean, String name, Object value) {
        try {
            BeanUtils.setProperty(bean, name, value);
        } catch (Exception var4) {
            throw new RuntimeException("设置属性值失败![类:" + bean.getClass().getName() + "][属性名:" + name + "][值:" + value + "]", var4);
        }
    }

    public static Class<?> reflectGetEntity(String className) {
        try {
            return Class.forName(className);
        } catch (Exception var2) {
            throw new RuntimeException("没有找到类" + className, var2);
        }
    }

    public static Class<?> getPropertyType(Object bean, String property) {
        try {
            return (new PropertyUtilsBean()).getPropertyType(bean, property);
        } catch (Exception var3) {
            throw new RuntimeException("获取属性类型失败!", var3);
        }
    }

    public static Map<String, Class<?>> getPropTypes(Class<?> clazz) {
        if(clazz == null) {
            throw new IllegalArgumentException("clazz must not null!");
        } else {
            HashMap results = new HashMap();

            try {
                BeanInfo e = Introspector.getBeanInfo(clazz);
                PropertyDescriptor[] arr$ = e.getPropertyDescriptors();
                int len$ = arr$.length;

                for(int i$ = 0; i$ < len$; ++i$) {
                    PropertyDescriptor propertyDescriptor = arr$[i$];
                    String propName = propertyDescriptor.getName();
                    if(propertyDescriptor.getReadMethod() != null) {
                        results.put(propName, propertyDescriptor.getPropertyType());
                    }
                }

                results.remove("class");
                return results;
            } catch (IntrospectionException var8) {
                throw new RuntimeException(var8);
            }
        }
    }

    public static Set<String> getPropNames(Class<?> clazz) {
        return getPropTypes(clazz).keySet();
    }

    public static Map<String, Object> getPropValues(Object bean) {
        if(bean == null) {
            throw new IllegalArgumentException("Target object must not null!");
        } else {
            HashMap results = new HashMap();

            try {
                BeanInfo e = Introspector.getBeanInfo(bean.getClass());
                PropertyDescriptor[] arr$ = e.getPropertyDescriptors();
                int len$ = arr$.length;

                for(int i$ = 0; i$ < len$; ++i$) {
                    PropertyDescriptor propertyDescriptor = arr$[i$];
                    String propName = propertyDescriptor.getName();
                    Method readMethod = propertyDescriptor.getReadMethod();
                    if(readMethod != null) {
                        Object value = readMethod.invoke(bean, new Object[0]);
                        results.put(propName, value);
                    }
                }

                results.remove("class");
                return results;
            } catch (Exception var10) {
                throw new RuntimeException(var10);
            }
        }
    }
}

 

java beanUtilsX操作属性类

标签:final   .com   org   des   apach   pop   bool   eth   getc   

原文地址:http://www.cnblogs.com/wxx516/p/8094518.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!