java beanUtilsX操作属性类

//
// 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);
            }
        }
    }
}
时间: 2024-10-09 09:11:05

java beanUtilsX操作属性类的相关文章

java之操作excel类

package com.lilysilk.util; import java.io.FileInputStream; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; /** * @author dsp *@Comments : 导入导出Excel工具类 *

Java属性类:Properties的常用方法

Properties类本身是Hashtable类的子类,也是按照key-value的形式存放数据的. 设置和取得属性: public class PropertiesDemo01{ public static void main(String args[]){ Properties pro = new Properties() ; // 创建Properties对象 pro.setProperty("BJ","BeiJing") ; // 设置属性 pro.setP

java笔记--使用SwingWoker类完成耗时操作

使用SwingWoker类完成耗时操作: 对于Swing中的耗时操作,通常要在一个新的线程中运行,以免程序"假死". 在java6.0中,可以用SwingWoker类来完成 SwingWoker<T,V>是在专用线程中执行长时间GUI交互任务的抽象类.用Swing 编写多线程应用程序时,要记住两个约束条件:     1.不应该在事件指派线程运行耗时任务,否则应用程序将无反应.     2.只能在事件指派线程上访问Swing控件     注: 通常需要将耗时的任务放到Swin

java的接口、类、属性、方法各有哪些修饰符

参考博客:http://blog.csdn.net/cao_tao199612/article/details/7458245 1. 接口的修饰符只有:public 2. 类的修饰符分为:可访问控制符和非访问控制符两种. 可访问控制符是:公共类修饰符 public 非访问控制符有:抽象类修饰符 abstract :最终类修饰符 final 1.公共类修饰符 public : Java 语言中类 的可访问控制符只有一个: public 即公共的.每个 Java 程序的主类都必须是 public 类

C++的继承操作---基类指针访问派生类问题---基类成员恢复访问属性问题

#include "stdafx.h" #include <iostream> #include <algorithm> using namespace std; class Base { public: int num; virtual void func() { cout<<"Do something in Base"<<endl; } }; class Derived:private Base { public:

Android(java)学习笔记167:Java中操作文件的类介绍

1.File类:对硬盘上的文件和目录进行操作的类.    File类是文件和目录路径名抽象表现形式  构造函数:        1) File(String pathname)       Creates a new File instance by converting the given pathname string into an abstract pathname. 2)File(File parent, String child)       Creates a new File i

Java学习笔记——File类之文件管理和读写操作、下载图片

Java学习笔记——File类之文件管理和读写操作.下载图片 File类的总结: 1.文件和文件夹的创建 2.文件的读取 3.文件的写入 4.文件的复制(字符流.字节流.处理流) 5.以图片地址下载图片 文件和文件夹 相关函数 (boolean) mkdir() 创建此抽象路径名指定的目录  (boolean) mkdirs() 创建此抽象路径名指定的目录,包括所有必需但不存在的父目录. (boolean) delete() 删除此抽象路径名表示的文件或目录 (boolean) createNe

Java数据库操作类演示

只在mysql上测试过,不知道算不算好使?1. [代码][Java]代码     package org.load.demo; import java.io.IOException;import java.util.List;import java.util.Map; import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRe

转:java中包、类、方法、属性、常量的命名规则

必须用英文,不要用汉语拼音 1:包(package):用于将完成不同功能的类分门别类,放在不同的目录(包)下,包的命名规则:将公司域名反转作为包名.比如www.sohu.com 对于包名:每个字母都需要小写.比如:com.sohu.test;该包下的Test类的全名是:com.sohu.Test.java . 如果定义类的时候没有使用package,那么java就认为我们所定义的类位于默认包里面(default package). 2:类:首字母大写,如果一个类由多个单词构成,那么每个单词的首字