Skip to content

Commit 27bccb3

Browse files
authored
🎨 remove commons-beanutils dependency
1 parent 188d1e1 commit 27bccb3

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

weixin-java-pay/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@
3535
<artifactId>commons-lang3</artifactId>
3636
</dependency>
3737

38-
<dependency>
39-
<groupId>commons-beanutils</groupId>
40-
<artifactId>commons-beanutils</artifactId>
41-
<version>1.9.4</version>
42-
</dependency>
4338
<dependency>
4439
<groupId>org.bouncycastle</groupId>
4540
<artifactId>bcpkix-jdk15on</artifactId>

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/EcommerceServiceImpl.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,22 @@
1313
import com.google.gson.Gson;
1414
import com.google.gson.GsonBuilder;
1515
import lombok.RequiredArgsConstructor;
16-
import org.apache.commons.beanutils.BeanMap;
1716
import org.apache.commons.lang3.StringUtils;
1817

18+
import java.beans.BeanInfo;
19+
import java.beans.IntrospectionException;
20+
import java.beans.Introspector;
21+
import java.beans.PropertyDescriptor;
1922
import java.io.IOException;
2023
import java.io.InputStream;
24+
import java.lang.reflect.InvocationTargetException;
25+
import java.lang.reflect.Method;
2126
import java.nio.charset.StandardCharsets;
2227
import java.security.GeneralSecurityException;
2328
import java.text.DateFormat;
2429
import java.util.Iterator;
2530
import java.util.Map;
31+
import java.util.LinkedHashMap;
2632
import java.util.Objects;
2733
import java.util.Set;
2834

@@ -385,7 +391,7 @@ private boolean verifyNotifySign(SignatureHeader header, String data) {
385391
* @return 拼接好的string
386392
*/
387393
private String parseURLPair(Object o) {
388-
Map<Object, Object> map = new BeanMap(o);
394+
Map<Object, Object> map = getObjectToMap(o);
389395
Set<Map.Entry<Object, Object>> set = map.entrySet();
390396
Iterator<Map.Entry<Object, Object>> it = set.iterator();
391397
StringBuilder sb = new StringBuilder();
@@ -399,4 +405,27 @@ private String parseURLPair(Object o) {
399405
return sb.deleteCharAt(sb.length() - 1).toString();
400406
}
401407

408+
public static Map<Object, Object> getObjectToMap(Object obj) {
409+
try {
410+
Map<Object, Object> result = new LinkedHashMap<>();
411+
final Class<? extends Object> beanClass = obj.getClass();
412+
final BeanInfo beanInfo = Introspector.getBeanInfo(beanClass);
413+
final PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
414+
if (propertyDescriptors != null) {
415+
for (final PropertyDescriptor propertyDescriptor : propertyDescriptors) {
416+
if (propertyDescriptor != null) {
417+
final String name = propertyDescriptor.getName();
418+
final Method readMethod = propertyDescriptor.getReadMethod();
419+
if (readMethod != null) {
420+
result.put(name, readMethod.invoke(obj));
421+
}
422+
}
423+
}
424+
}
425+
return result;
426+
} catch (IllegalAccessException | IntrospectionException | InvocationTargetException ignored) {
427+
return null;
428+
}
429+
}
430+
402431
}

0 commit comments

Comments
 (0)