🎨 remove commons-beanutils dependency

This commit is contained in:
NaccOll 2022-07-12 16:32:34 +08:00 committed by GitHub
parent 188d1e186b
commit 27bccb3a7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 7 deletions

View File

@ -35,11 +35,6 @@
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>

View File

@ -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<Object, Object> map = new BeanMap(o);
Map<Object, Object> map = getObjectToMap(o);
Set<Map.Entry<Object, Object>> set = map.entrySet();
Iterator<Map.Entry<Object, Object>> 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<Object, Object> getObjectToMap(Object obj) {
try {
Map<Object, Object> result = new LinkedHashMap<>();
final Class<? extends Object> 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;
}
}
}