Java中公用Bean工具类拿走、直接用

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class BeanUtils {

    /**
     * 复制源对象的属性到目标对象中。
     * @param source 源对象
     * @param target 目标对象
     * @throws IllegalAccessException 如果目标对象的属性无法访问
     * @throws InvocationTargetException 如果调用 getter 或 setter 方法时出错
     * @throws IntrospectionException 如果 introspect 操作失败
     */
    public static void copyProperties(Object source, Object target) throws IllegalAccessException, InvocationTargetException, IntrospectionException {
        if (source == null || target == null) {
            throw new IllegalArgumentException("Source and target must not be null");
        }

        BeanInfo sourceBeanInfo = Introspector.getBeanInfo(source.getClass());
        BeanInfo targetBeanInfo = Introspector.getBeanInfo(target.getClass());

        PropertyDescriptor[] sourcePds = sourceBeanInfo.getPropertyDescriptors();
        PropertyDescriptor[] targetPds = targetBeanInfo.getPropertyDescriptors();

        Map<String, PropertyDescriptor> targetPdMap = new HashMap<>();
        for (PropertyDescriptor targetPd : targetPds) {
            targetPdMap.put(targetPd.getName(), targetPd);
        }

        for (PropertyDescriptor sourcePd : sourcePds) {
            String propertyName = sourcePd.getName();
            if (targetPdMap.containsKey(propertyName)) {
                PropertyDescriptor targetPd = targetPdMap.get(propertyName);
                Method readMethod = sourcePd.getReadMethod();
                Method writeMethod = targetPd.getWriteMethod();

                if (readMethod != null && writeMethod != null) {
                    Object value = readMethod.invoke(source);
                    writeMethod.invoke(target, value);
                }
            }
        }
    }

    /**
     * 将一个对象的属性转换为 Map。
     * @param bean 目标对象
     * @return 包含对象属性的 Map
     * @throws IntrospectionException 如果 introspect 操作失败
     * @throws IllegalAccessException 如果属性访问失败
     * @throws InvocationTargetException 如果调用 getter 方法失败
     */
    public static Map<String, Object> beanToMap(Object bean) throws IntrospectionException, IllegalAccessException, InvocationTargetException {
        if (bean == null) {
            throw new IllegalArgumentException("Bean must not be null");
        }

        Map<String, Object> map = new HashMap<>();
        BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());

        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor pd : propertyDescriptors) {
            String propertyName = pd.getName();
            if (!propertyName.equals("class")) {
                Method getter = pd.getReadMethod();
                Object value = getter.invoke(bean);
                map.put(propertyName, value);
            }
        }

        return map;
    }

    /**
     * 从 Map 创建一个对象。
     * @param map 包含属性的 Map
     * @param beanClass 对象的类
     * @param <T> 对象类型
     * @return 创建的对象
     * @throws InstantiationException 如果对象无法创建
     * @throws IllegalAccessException 如果属性访问失败
     * @throws IntrospectionException 如果 introspect 操作失败
     * @throws InvocationTargetException 如果调用 setter 方法失败
     */
    public static <T> T mapToBean(Map<String, Object> map, Class<T> beanClass) throws InstantiationException, IllegalAccessException, IntrospectionException, InvocationTargetException {
        if (map == null || beanClass == null) {
            throw new IllegalArgumentException("Map and bean class must not be null");
        }

        T bean = beanClass.newInstance();
        BeanInfo beanInfo = Introspector.getBeanInfo(beanClass);

        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor pd : propertyDescriptors) {
            String propertyName = pd.getName();
            if (map.containsKey(propertyName)) {
                Method setter = pd.getWriteMethod();
                if (setter != null) {
                    Object value = map.get(propertyName);
                    setter.invoke(bean, value);
                }
            }
        }

        return bean;
    }

    /**
     * 获取对象的属性值。
     * @param bean 目标对象
     * @param propertyName 属性名
     * @return 属性值
     * @throws IntrospectionException 如果 introspect 操作失败
     * @throws IllegalAccessException 如果属性访问失败
     * @throws InvocationTargetException 如果调用 getter 方法失败
     */
    public static Object getPropertyValue(Object bean, String propertyName) throws IntrospectionException, IllegalAccessException, InvocationTargetException {
        if (bean == null || propertyName == null) {
            throw new IllegalArgumentException("Bean and property name must not be null");
        }

        BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

        for (PropertyDescriptor pd : propertyDescriptors) {
            if (pd.getName().equals(propertyName)) {
                Method getter = pd.getReadMethod();
                if (getter != null) {
                    return getter.invoke(bean);
                }
            }
        }

        throw new IllegalArgumentException("No such property: " + propertyName);
    }

    /**
     * 设置对象的属性值。
     * @param bean 目标对象
     * @param propertyName 属性名
     * @param value 属性值
     * @throws IntrospectionException 如果 introspect 操作失败
     * @throws IllegalAccessException 如果属性访问失败
     * @throws InvocationTargetException 如果调用 setter 方法失败
     */
    public static void setPropertyValue(Object bean, String propertyName, Object value) throws IntrospectionException, IllegalAccessException, InvocationTargetException {
        if (bean == null || propertyName == null) {
            throw new IllegalArgumentException("Bean and property name must not be null");
        }

        BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

        for (PropertyDescriptor pd : propertyDescriptors) {
            if (pd.getName().equals(propertyName)) {
                Method setter = pd.getWriteMethod();
                if (setter != null) {
                    setter.invoke(bean, value);
                    return;
                }
            }
        }

        throw new IllegalArgumentException("No such property: " + propertyName);
    }
}

说明:

  1. copyProperties

    • 将源对象的属性复制到目标对象中,要求源对象和目标对象具有相同的属性名称和类型。
  2. beanToMap

    • 将一个对象的属性转换为 Map。属性名作为 Map 的键,属性值作为 Map 的值。
  3. mapToBean

    • 从一个 Map 创建一个对象,并设置对象的属性值。Map 的键为属性名,值为属性值。
  4. getPropertyValue

    • 获取对象的指定属性的值。
  5. setPropertyValue

    • 设置对象的指定属性的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值