From 27bccb3a7e48999d93df8d2d8bbd2664089edd5f Mon Sep 17 00:00:00 2001 From: NaccOll Date: Tue, 12 Jul 2022 16:32:34 +0800 Subject: [PATCH] :art: remove commons-beanutils dependency --- weixin-java-pay/pom.xml | 5 --- .../service/impl/EcommerceServiceImpl.java | 33 +++++++++++++++++-- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/weixin-java-pay/pom.xml b/weixin-java-pay/pom.xml index fcd6a92c4..5109e93c8 100644 --- a/weixin-java-pay/pom.xml +++ b/weixin-java-pay/pom.xml @@ -35,11 +35,6 @@ commons-lang3 - - commons-beanutils - commons-beanutils - 1.9.4 - org.bouncycastle bcpkix-jdk15on diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/EcommerceServiceImpl.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/EcommerceServiceImpl.java index 23f1cbd8c..27a34a9e2 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/EcommerceServiceImpl.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/EcommerceServiceImpl.java @@ -13,16 +13,22 @@ import com.google.common.base.CaseFormat; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import lombok.RequiredArgsConstructor; -import org.apache.commons.beanutils.BeanMap; import org.apache.commons.lang3.StringUtils; +import java.beans.BeanInfo; +import java.beans.IntrospectionException; +import java.beans.Introspector; +import java.beans.PropertyDescriptor; import java.io.IOException; import java.io.InputStream; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.nio.charset.StandardCharsets; import java.security.GeneralSecurityException; import java.text.DateFormat; import java.util.Iterator; import java.util.Map; +import java.util.LinkedHashMap; import java.util.Objects; import java.util.Set; @@ -385,7 +391,7 @@ public class EcommerceServiceImpl implements EcommerceService { * @return 拼接好的string */ private String parseURLPair(Object o) { - Map map = new BeanMap(o); + Map map = getObjectToMap(o); Set> set = map.entrySet(); Iterator> it = set.iterator(); StringBuilder sb = new StringBuilder(); @@ -399,4 +405,27 @@ public class EcommerceServiceImpl implements EcommerceService { return sb.deleteCharAt(sb.length() - 1).toString(); } + public static Map getObjectToMap(Object obj) { + try { + Map result = new LinkedHashMap<>(); + final Class beanClass = obj.getClass(); + final BeanInfo beanInfo = Introspector.getBeanInfo(beanClass); + final PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); + if (propertyDescriptors != null) { + for (final PropertyDescriptor propertyDescriptor : propertyDescriptors) { + if (propertyDescriptor != null) { + final String name = propertyDescriptor.getName(); + final Method readMethod = propertyDescriptor.getReadMethod(); + if (readMethod != null) { + result.put(name, readMethod.invoke(obj)); + } + } + } + } + return result; + } catch (IllegalAccessException | IntrospectionException | InvocationTargetException ignored) { + return null; + } + } + }