¶Ô±ÈÐÂÎļþ |
| | |
| | | /* |
| | | * Copyright (c) 2018-2028, DreamLu All rights reserved. |
| | | * |
| | | * Redistribution and use in source and binary forms, with or without |
| | | * modification, are permitted provided that the following conditions are met: |
| | | * |
| | | * Redistributions of source code must retain the above copyright notice, |
| | | * this list of conditions and the following disclaimer. |
| | | * Redistributions in binary form must reproduce the above copyright |
| | | * notice, this list of conditions and the following disclaimer in the |
| | | * documentation and/or other materials provided with the distribution. |
| | | * Neither the name of the dreamlu.net developer nor the names of its |
| | | * contributors may be used to endorse or promote products derived from |
| | | * this software without specific prior written permission. |
| | | * Author: DreamLu 墿¥æ¢¦ (596392912@qq.com) |
| | | */ |
| | | package org.springblade.core.tool.utils; |
| | | |
| | | |
| | | import org.springblade.core.tool.beans.BeanProperty; |
| | | import org.springblade.core.tool.beans.BladeBeanCopier; |
| | | import org.springblade.core.tool.beans.BladeBeanMap; |
| | | import org.springblade.core.tool.convert.BladeConverter; |
| | | import org.springframework.beans.BeanWrapper; |
| | | import org.springframework.beans.BeansException; |
| | | import org.springframework.beans.PropertyAccessorFactory; |
| | | import org.springframework.cglib.beans.BeanGenerator; |
| | | import org.springframework.lang.Nullable; |
| | | |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * å®ä½å·¥å
·ç±» |
| | | * |
| | | * @author L.cm |
| | | */ |
| | | public class BeanUtil extends org.springframework.beans.BeanUtils { |
| | | |
| | | /** |
| | | * å®ä¾å对象 |
| | | * |
| | | * @param clazz ç±» |
| | | * @param <T> æ³åæ è®° |
| | | * @return 对象 |
| | | */ |
| | | @SuppressWarnings("unchecked") |
| | | public static <T> T newInstance(Class<?> clazz) { |
| | | return (T) instantiateClass(clazz); |
| | | } |
| | | |
| | | /** |
| | | * å®ä¾å对象 |
| | | * |
| | | * @param clazzStr ç±»å |
| | | * @param <T> æ³åæ è®° |
| | | * @return 对象 |
| | | */ |
| | | public static <T> T newInstance(String clazzStr) { |
| | | try { |
| | | Class<?> clazz = ClassUtil.forName(clazzStr, null); |
| | | return newInstance(clazz); |
| | | } catch (ClassNotFoundException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * è·åBeanç屿§, æ¯æ propertyName å¤çº§ ï¼test.user.name |
| | | * |
| | | * @param bean bean |
| | | * @param propertyName 屿§å |
| | | * @return 屿§å¼ |
| | | */ |
| | | @Nullable |
| | | public static Object getProperty(@Nullable Object bean, String propertyName) { |
| | | if (bean == null) { |
| | | return null; |
| | | } |
| | | BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(bean); |
| | | return beanWrapper.getPropertyValue(propertyName); |
| | | } |
| | | |
| | | /** |
| | | * 设置Bean屿§, æ¯æ propertyName å¤çº§ ï¼test.user.name |
| | | * |
| | | * @param bean bean |
| | | * @param propertyName 屿§å |
| | | * @param value 屿§å¼ |
| | | */ |
| | | public static void setProperty(Object bean, String propertyName, Object value) { |
| | | Objects.requireNonNull(bean, "bean Could not null"); |
| | | BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(bean); |
| | | beanWrapper.setPropertyValue(propertyName, value); |
| | | } |
| | | |
| | | /** |
| | | * æ·±å¤å¶ |
| | | * |
| | | * <p> |
| | | * æ¯æ map bean |
| | | * </p> |
| | | * |
| | | * @param source æºå¯¹è±¡ |
| | | * @param <T> æ³åæ è®° |
| | | * @return T |
| | | */ |
| | | @SuppressWarnings("unchecked") |
| | | @Nullable |
| | | public static <T> T clone(@Nullable T source) { |
| | | if (source == null) { |
| | | return null; |
| | | } |
| | | return (T) BeanUtil.copy(source, source.getClass()); |
| | | } |
| | | |
| | | /** |
| | | * copy å¯¹è±¡å±æ§ï¼é»è®¤ä¸ä½¿ç¨Convert |
| | | * |
| | | * <p> |
| | | * æ¯æ map bean copy |
| | | * </p> |
| | | * |
| | | * @param source æºå¯¹è±¡ |
| | | * @param clazz ç±»å |
| | | * @param <T> æ³åæ è®° |
| | | * @return T |
| | | */ |
| | | @Nullable |
| | | public static <T> T copy(@Nullable Object source, Class<T> clazz) { |
| | | if (source == null) { |
| | | return null; |
| | | } |
| | | return BeanUtil.copy(source, source.getClass(), clazz); |
| | | } |
| | | |
| | | /** |
| | | * copy å¯¹è±¡å±æ§ï¼é»è®¤ä¸ä½¿ç¨Convert |
| | | * |
| | | * <p> |
| | | * æ¯æ map bean copy |
| | | * </p> |
| | | * |
| | | * @param source æºå¯¹è±¡ |
| | | * @param sourceClazz æºç±»å |
| | | * @param targetClazz è½¬æ¢æçç±»å |
| | | * @param <T> æ³åæ è®° |
| | | * @return T |
| | | */ |
| | | @Nullable |
| | | public static <T> T copy(@Nullable Object source, Class sourceClazz, Class<T> targetClazz) { |
| | | if (source == null) { |
| | | return null; |
| | | } |
| | | BladeBeanCopier copier = BladeBeanCopier.create(sourceClazz, targetClazz, false); |
| | | T to = newInstance(targetClazz); |
| | | copier.copy(source, to, null); |
| | | return to; |
| | | } |
| | | |
| | | /** |
| | | * copy å表对象ï¼é»è®¤ä¸ä½¿ç¨Convert |
| | | * |
| | | * <p> |
| | | * æ¯æ map bean copy |
| | | * </p> |
| | | * |
| | | * @param sourceList æºå表 |
| | | * @param targetClazz è½¬æ¢æçç±»å |
| | | * @param <T> æ³åæ è®° |
| | | * @return T |
| | | */ |
| | | public static <T> List<T> copy(@Nullable Collection<?> sourceList, Class<T> targetClazz) { |
| | | if (sourceList == null || sourceList.isEmpty()) { |
| | | return Collections.emptyList(); |
| | | } |
| | | List<T> outList = new ArrayList<>(sourceList.size()); |
| | | Class<?> sourceClazz = null; |
| | | for (Object source : sourceList) { |
| | | if (source == null) { |
| | | continue; |
| | | } |
| | | if (sourceClazz == null) { |
| | | sourceClazz = source.getClass(); |
| | | } |
| | | T bean = BeanUtil.copy(source, sourceClazz, targetClazz); |
| | | outList.add(bean); |
| | | } |
| | | return outList; |
| | | } |
| | | |
| | | /** |
| | | * æ·è´å¯¹è±¡ |
| | | * |
| | | * <p> |
| | | * æ¯æ map bean copy |
| | | * </p> |
| | | * |
| | | * @param source æºå¯¹è±¡ |
| | | * @param targetBean éè¦èµå¼ç对象 |
| | | */ |
| | | public static void copy(@Nullable Object source, @Nullable Object targetBean) { |
| | | if (source == null || targetBean == null) { |
| | | return; |
| | | } |
| | | BladeBeanCopier copier = BladeBeanCopier |
| | | .create(source.getClass(), targetBean.getClass(), false); |
| | | |
| | | copier.copy(source, targetBean, null); |
| | | } |
| | | |
| | | /** |
| | | * æ·è´å¯¹è±¡ï¼source 屿§å null 夿ï¼Map 䏿¯æï¼map ä¼å instanceof 夿ï¼ä¸ä¼ |
| | | * |
| | | * <p> |
| | | * æ¯æ bean copy |
| | | * </p> |
| | | * |
| | | * @param source æºå¯¹è±¡ |
| | | * @param targetBean éè¦èµå¼ç对象 |
| | | */ |
| | | public static void copyNonNull(@Nullable Object source, @Nullable Object targetBean) { |
| | | if (source == null || targetBean == null) { |
| | | return; |
| | | } |
| | | BladeBeanCopier copier = BladeBeanCopier |
| | | .create(source.getClass(), targetBean.getClass(), false, true); |
| | | |
| | | copier.copy(source, targetBean, null); |
| | | } |
| | | |
| | | /** |
| | | * æ·è´å¯¹è±¡å¹¶å¯¹ä¸åç±»å屿§è¿è¡è½¬æ¢ |
| | | * |
| | | * <p> |
| | | * æ¯æ map bean copy |
| | | * </p> |
| | | * |
| | | * @param source æºå¯¹è±¡ |
| | | * @param targetClazz è½¬æ¢æçç±» |
| | | * @param <T> æ³åæ è®° |
| | | * @return T |
| | | */ |
| | | @Nullable |
| | | public static <T> T copyWithConvert(@Nullable Object source, Class<T> targetClazz) { |
| | | if (source == null) { |
| | | return null; |
| | | } |
| | | return BeanUtil.copyWithConvert(source, source.getClass(), targetClazz); |
| | | } |
| | | |
| | | /** |
| | | * æ·è´å¯¹è±¡å¹¶å¯¹ä¸åç±»å屿§è¿è¡è½¬æ¢ |
| | | * |
| | | * <p> |
| | | * æ¯æ map bean copy |
| | | * </p> |
| | | * |
| | | * @param source æºå¯¹è±¡ |
| | | * @param sourceClazz æºç±» |
| | | * @param targetClazz è½¬æ¢æçç±» |
| | | * @param <T> æ³åæ è®° |
| | | * @return T |
| | | */ |
| | | @Nullable |
| | | public static <T> T copyWithConvert(@Nullable Object source, Class<?> sourceClazz, Class<T> targetClazz) { |
| | | if (source == null) { |
| | | return null; |
| | | } |
| | | BladeBeanCopier copier = BladeBeanCopier.create(sourceClazz, targetClazz, true); |
| | | T to = newInstance(targetClazz); |
| | | copier.copy(source, to, new BladeConverter(sourceClazz, targetClazz)); |
| | | return to; |
| | | } |
| | | |
| | | /** |
| | | * æ·è´å表并对ä¸åç±»å屿§è¿è¡è½¬æ¢ |
| | | * |
| | | * <p> |
| | | * æ¯æ map bean copy |
| | | * </p> |
| | | * |
| | | * @param sourceList æºå¯¹è±¡å表 |
| | | * @param targetClazz è½¬æ¢æçç±» |
| | | * @param <T> æ³åæ è®° |
| | | * @return List |
| | | */ |
| | | public static <T> List<T> copyWithConvert(@Nullable Collection<?> sourceList, Class<T> targetClazz) { |
| | | if (sourceList == null || sourceList.isEmpty()) { |
| | | return Collections.emptyList(); |
| | | } |
| | | List<T> outList = new ArrayList<>(sourceList.size()); |
| | | Class<?> sourceClazz = null; |
| | | for (Object source : sourceList) { |
| | | if (source == null) { |
| | | continue; |
| | | } |
| | | if (sourceClazz == null) { |
| | | sourceClazz = source.getClass(); |
| | | } |
| | | T bean = BeanUtil.copyWithConvert(source, sourceClazz, targetClazz); |
| | | outList.add(bean); |
| | | } |
| | | return outList; |
| | | } |
| | | |
| | | /** |
| | | * Copy the property values of the given source bean into the target class. |
| | | * <p>Note: The source and target classes do not have to match or even be derived |
| | | * from each other, as long as the properties match. Any bean properties that the |
| | | * source bean exposes but the target bean does not will silently be ignored. |
| | | * <p>This is just a convenience method. For more complex transfer needs, |
| | | * |
| | | * @param source the source bean |
| | | * @param targetClazz the target bean class |
| | | * @param <T> æ³åæ è®° |
| | | * @return T |
| | | * @throws BeansException if the copying failed |
| | | */ |
| | | @Nullable |
| | | public static <T> T copyProperties(@Nullable Object source, Class<T> targetClazz) throws BeansException { |
| | | if (source == null) { |
| | | return null; |
| | | } |
| | | T to = newInstance(targetClazz); |
| | | BeanUtil.copyProperties(source, to); |
| | | return to; |
| | | } |
| | | |
| | | /** |
| | | * Copy the property values of the given source bean into the target class. |
| | | * <p>Note: The source and target classes do not have to match or even be derived |
| | | * from each other, as long as the properties match. Any bean properties that the |
| | | * source bean exposes but the target bean does not will silently be ignored. |
| | | * <p>This is just a convenience method. For more complex transfer needs, |
| | | * |
| | | * @param sourceList the source list bean |
| | | * @param targetClazz the target bean class |
| | | * @param <T> æ³åæ è®° |
| | | * @return List |
| | | * @throws BeansException if the copying failed |
| | | */ |
| | | public static <T> List<T> copyProperties(@Nullable Collection<?> sourceList, Class<T> targetClazz) throws BeansException { |
| | | if (sourceList == null || sourceList.isEmpty()) { |
| | | return Collections.emptyList(); |
| | | } |
| | | List<T> outList = new ArrayList<>(sourceList.size()); |
| | | for (Object source : sourceList) { |
| | | if (source == null) { |
| | | continue; |
| | | } |
| | | T bean = BeanUtil.copyProperties(source, targetClazz); |
| | | outList.add(bean); |
| | | } |
| | | return outList; |
| | | } |
| | | |
| | | /** |
| | | * å°å¯¹è±¡è£
æmapå½¢å¼ |
| | | * |
| | | * @param bean æºå¯¹è±¡ |
| | | * @return {Map} |
| | | */ |
| | | @SuppressWarnings("unchecked") |
| | | public static Map<String, Object> toMap(@Nullable Object bean) { |
| | | if (bean == null) { |
| | | return new HashMap<>(0); |
| | | } |
| | | return BladeBeanMap.create(bean); |
| | | } |
| | | |
| | | /** |
| | | * å°map 转为 bean |
| | | * |
| | | * @param beanMap map |
| | | * @param valueType 对象类å |
| | | * @param <T> æ³åæ è®° |
| | | * @return {T} |
| | | */ |
| | | public static <T> T toBean(Map<String, Object> beanMap, Class<T> valueType) { |
| | | Objects.requireNonNull(beanMap, "beanMap Could not null"); |
| | | T to = newInstance(valueType); |
| | | if (beanMap.isEmpty()) { |
| | | return to; |
| | | } |
| | | BeanUtil.copy(beanMap, to); |
| | | return to; |
| | | } |
| | | |
| | | /** |
| | | * ç»ä¸ä¸ªBeanæ·»å åæ®µ |
| | | * |
| | | * @param superBean ç¶çº§Bean |
| | | * @param props æ°å¢å±æ§ |
| | | * @return {Object} |
| | | */ |
| | | @Nullable |
| | | public static Object generator(@Nullable Object superBean, BeanProperty... props) { |
| | | if (superBean == null) { |
| | | return null; |
| | | } |
| | | Class<?> superclass = superBean.getClass(); |
| | | Object genBean = generator(superclass, props); |
| | | BeanUtil.copy(superBean, genBean); |
| | | return genBean; |
| | | } |
| | | |
| | | /** |
| | | * ç»ä¸ä¸ªclassæ·»å åæ®µ |
| | | * |
| | | * @param superclass ç¶çº§ |
| | | * @param props æ°å¢å±æ§ |
| | | * @return {Object} |
| | | */ |
| | | public static Object generator(Class<?> superclass, BeanProperty... props) { |
| | | BeanGenerator generator = new BeanGenerator(); |
| | | generator.setSuperclass(superclass); |
| | | generator.setUseCache(true); |
| | | for (BeanProperty prop : props) { |
| | | generator.addProperty(prop.getName(), prop.getType()); |
| | | } |
| | | return generator.create(); |
| | | } |
| | | |
| | | } |