【fix sonar】ReflectHelper文件

This commit is contained in:
梁琦涛
2023-03-06 13:32:54 +08:00
parent f220843f12
commit f953de3cd6
@@ -1,6 +1,7 @@
package com.jero.common.util; package com.jero.common.util;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@@ -25,11 +26,11 @@ public class ReflectHelper {
/** /**
* 存放get方法 * 存放get方法
*/ */
private Hashtable<String, Method> getMethods = null; private HashMap<String, Method> getMethods = null;
/** /**
* 存放set方法 * 存放set方法
*/ */
private Hashtable<String, Method> setMethods = null; private HashMap<String, Method> setMethods = null;
/** /**
* 定义构造方法 -- 一般来说是个pojo * 定义构造方法 -- 一般来说是个pojo
@@ -45,8 +46,8 @@ public class ReflectHelper {
* @desc 初始化 * @desc 初始化
*/ */
public void initMethods() { public void initMethods() {
getMethods = new Hashtable<String, Method>(); getMethods = new HashMap<String, Method>();
setMethods = new Hashtable<String, Method>(); setMethods = new HashMap<String, Method>();
cls = obj.getClass(); cls = obj.getClass();
Method[] methods = cls.getMethods(); Method[] methods = cls.getMethods();
// 定义正则表达式,从方法中过滤出getter / setter 函数. // 定义正则表达式,从方法中过滤出getter / setter 函数.
@@ -66,8 +67,6 @@ public class ReflectHelper {
} else if (Pattern.matches(ss, methodName)) { } else if (Pattern.matches(ss, methodName)) {
param = setM.matcher(methodName).replaceAll(rapl).toLowerCase(); param = setM.matcher(methodName).replaceAll(rapl).toLowerCase();
setMethods.put(param, m); setMethods.put(param, m);
} else {
// logger.info(methodName + " 不是getter,setter方法!");
} }
} }
} }
@@ -198,10 +197,10 @@ public class ReflectHelper {
public static Object getFieldVal(String fieldName, Object o) { public static Object getFieldVal(String fieldName, Object o) {
try { try {
// 暴力反射获取属性 // 暴力反射获取属性
Field filed = o.getClass().getDeclaredField(fieldName); Field field = o.getClass().getDeclaredField(fieldName);
// 设置反射时取消Java的访问检查,暴力访问 // 设置反射时取消Java的访问检查,暴力访问
filed.setAccessible(true); ReflectionUtils.makeAccessible(field);
Object val = filed.get(o); Object val = field.get(o);
return val; return val;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@@ -216,7 +215,6 @@ public class ReflectHelper {
Field[] fields = o.getClass().getDeclaredFields(); Field[] fields = o.getClass().getDeclaredFields();
String[] fieldNames = new String[fields.length]; String[] fieldNames = new String[fields.length];
for (int i = 0; i < fields.length; i++) { for (int i = 0; i < fields.length; i++) {
//log.info(fields[i].getType());
fieldNames[i] = fields[i].getName(); fieldNames[i] = fields[i].getName();
} }
return fieldNames; return fieldNames;
@@ -227,8 +225,7 @@ public class ReflectHelper {
*/ */
public static List<Map> getFiledsInfo(Object o) { public static List<Map> getFiledsInfo(Object o) {
Field[] fields = o.getClass().getDeclaredFields(); Field[] fields = o.getClass().getDeclaredFields();
String[] fieldNames = new String[fields.length]; List<Map> list = new ArrayList<>();
List<Map> list = new ArrayList<Map>();
Map<String, Object> infoMap = null; Map<String, Object> infoMap = null;
for (int i = 0; i < fields.length; i++) { for (int i = 0; i < fields.length; i++) {
infoMap = new HashMap<String, Object>(); infoMap = new HashMap<String, Object>();