From 80b6cbfc9c861469146318d0b3dd5f8b8b525b8a Mon Sep 17 00:00:00 2001 From: xiejun <xiejun@vci-tech.com> Date: 星期五, 01 十一月 2024 15:11:19 +0800 Subject: [PATCH] Revert "集成获取mdm分发通用数据格式接口集成" --- Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/utils/BeanUtil.java | 424 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 424 insertions(+), 0 deletions(-) diff --git a/Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/utils/BeanUtil.java b/Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/utils/BeanUtil.java new file mode 100644 index 0000000..26abd4c --- /dev/null +++ b/Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/utils/BeanUtil.java @@ -0,0 +1,424 @@ +/* + * 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 澶氱骇 锛歵est.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 澶氱骇 锛歵est.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 瀵硅薄灞炴�э紝榛樿涓嶄娇鐢–onvert + * + * <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 瀵硅薄灞炴�э紝榛樿涓嶄娇鐢–onvert + * + * <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); + } + + /** + * 鎷疯礉瀵硅薄锛宻ource 灞炴�у仛 null 鍒ゆ柇锛孧ap 涓嶆敮鎸侊紝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; + } + + /** + * 灏嗗璞¤鎴恗ap褰㈠紡 + * + * @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); + } + + /** + * 灏唌ap 杞负 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; + } + + /** + * 缁欎竴涓狟ean娣诲姞瀛楁 + * + * @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; + } + + /** + * 缁欎竴涓猚lass娣诲姞瀛楁 + * + * @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(); + } + +} -- Gitblit v1.9.3