package com.qianwen.core.tool.utils; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; import com.qianwen.core.tool.beans.BeanProperty; import com.qianwen.core.tool.beans.BladeBeanCopier; import com.qianwen.core.tool.beans.BladeBeanMap; import com.qianwen.core.tool.convert.BladeConverter; import org.springframework.beans.BeanUtils; 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; /* loaded from: blade-core-tool-9.3.0.0-SNAPSHOT.jar:org/springblade/core/tool/utils/BeanUtil.class */ public class BeanUtil extends BeanUtils { public static T newInstance(Class clazz) { return (T) instantiateClass(clazz); } public static T newInstance(String clazzStr) { try { Class clazz = ClassUtil.forName(clazzStr, null); return (T) newInstance(clazz); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } } @Nullable public static Object getProperty(@Nullable Object bean, String propertyName) { if (bean == null) { return null; } BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(bean); return beanWrapper.getPropertyValue(propertyName); } 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); } @Nullable public static T clone(@Nullable T source) { if (source == null) { return null; } return (T) copy((Object) source, (Class) source.getClass()); } @Nullable public static T copy(@Nullable Object source, Class clazz) { if (source == null) { return null; } return (T) copy(source, source.getClass(), clazz); } @Nullable public static T copy(@Nullable Object source, Class sourceClazz, Class targetClazz) { if (source == null) { return null; } BladeBeanCopier copier = BladeBeanCopier.create(sourceClazz, targetClazz, false); T to = (T) newInstance((Class) targetClazz); copier.copy(source, to, null); return to; } public static List copy(@Nullable Collection sourceList, Class targetClazz) { if (sourceList == null || sourceList.isEmpty()) { return Collections.emptyList(); } ArrayList arrayList = new ArrayList(sourceList.size()); Class sourceClazz = null; for (Object source : sourceList) { if (source != null) { if (sourceClazz == null) { sourceClazz = source.getClass(); } arrayList.add(copy(source, sourceClazz, targetClazz)); } } return arrayList; } 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); } 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); } @Nullable public static T copyWithConvert(@Nullable Object source, Class targetClazz) { if (source == null) { return null; } return (T) copyWithConvert(source, source.getClass(), targetClazz); } @Nullable public static T copyWithConvert(@Nullable Object source, Class sourceClazz, Class targetClazz) { if (source == null) { return null; } BladeBeanCopier copier = BladeBeanCopier.create(sourceClazz, targetClazz, true); T to = (T) newInstance((Class) targetClazz); copier.copy(source, to, new BladeConverter(sourceClazz, targetClazz)); return to; } public static List copyWithConvert(@Nullable Collection sourceList, Class targetClazz) { if (sourceList == null || sourceList.isEmpty()) { return Collections.emptyList(); } ArrayList arrayList = new ArrayList(sourceList.size()); Class sourceClazz = null; for (Object source : sourceList) { if (source != null) { if (sourceClazz == null) { sourceClazz = source.getClass(); } arrayList.add(copyWithConvert(source, sourceClazz, targetClazz)); } } return arrayList; } @Nullable public static T copyProperties(@Nullable Object source, Class targetClazz) throws BeansException { if (source == null) { return null; } T to = (T) newInstance((Class) targetClazz); copyProperties(source, to); return to; } public static List copyProperties(@Nullable Collection sourceList, Class targetClazz) throws BeansException { if (sourceList == null || sourceList.isEmpty()) { return Collections.emptyList(); } ArrayList arrayList = new ArrayList(sourceList.size()); for (Object source : sourceList) { if (source != null) { arrayList.add(copyProperties(source, targetClazz)); } } return arrayList; } public static Map toMap(@Nullable Object bean) { if (bean == null) { return new HashMap(0); } return BladeBeanMap.create(bean); } public static T toBean(Map beanMap, Class valueType) { Objects.requireNonNull(beanMap, "beanMap Could not null"); T to = (T) newInstance((Class) valueType); if (beanMap.isEmpty()) { return to; } copy(beanMap, to); return to; } @Nullable public static Object generator(@Nullable Object superBean, BeanProperty... props) { if (superBean == null) { return null; } Class superclass = superBean.getClass(); Object genBean = generator(superclass, props); copy(superBean, genBean); return genBean; } 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(); } }