fix ReflectUtil

This commit is contained in:
Looly 2019-12-18 10:00:57 +08:00
parent c62ede5276
commit 7ffdc2c473
3 changed files with 34 additions and 17 deletions

View File

@ -11,6 +11,7 @@
* 【core 】 改进Bean判断和注入逻辑支持public字段注入issue#I1689L@Gitee
* 【extra】 新增SpringUtil
* 【http 】 Get请求支持body移除bodyJSON方法issue#671@Github
* 【core 】 ReflectUtil修正getFieldValue逻辑防止歧义
### Bug修复

View File

@ -199,7 +199,7 @@ public class ReflectUtil {
/**
* 获取字段值
*
* @param obj 对象
* @param obj 对象如果static字段此处为类
* @param fieldName 字段名
* @return 字段值
* @throws UtilException 包装IllegalAccessException异常
@ -208,7 +208,19 @@ public class ReflectUtil {
if (null == obj || StrUtil.isBlank(fieldName)) {
return null;
}
return getFieldValue(obj, getField(obj.getClass(), fieldName));
return getFieldValue(obj, getField(obj instanceof Class ? (Class<?>)obj : obj.getClass(), fieldName));
}
/**
* 获取静态字段值
*
* @param field 字段
* @return 字段值
* @throws UtilException 包装IllegalAccessException异常
* @since 5.1.0
*/
public static Object getStaticFieldValue(Field field) throws UtilException {
return getFieldValue(null, field);
}
/**
@ -223,6 +235,11 @@ public class ReflectUtil {
if (null == field) {
return null;
}
if(obj instanceof Class){
// 静态字段获取时对象为null
obj = null;
}
setAccessible(field);
Object result;
try {
@ -236,13 +253,13 @@ public class ReflectUtil {
/**
* 获取所有字段的值
*
* @param obj bean对象
* @param obj bean对象如果是static字段此处为类class
* @return 字段值数组
* @since 4.1.17
*/
public static Object[] getFieldsValue(Object obj) {
if (null != obj) {
final Field[] fields = getFields(obj.getClass());
final Field[] fields = getFields(obj instanceof Class ? (Class<?>)obj : obj.getClass());
if (null != fields) {
final Object[] values = new Object[fields.length];
for (int i = 0; i < fields.length; i++) {

View File

@ -1,14 +1,12 @@
package cn.hutool.core.util;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import cn.hutool.core.lang.test.bean.ExamInfoDict;
import cn.hutool.core.util.ClassUtilTest.TestSubClass;
import org.junit.Assert;
import org.junit.Test;
import cn.hutool.core.lang.Filter;
import cn.hutool.core.lang.test.bean.ExamInfoDict;
import cn.hutool.core.util.ClassUtilTest.TestSubClass;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* 反射工具类单元测试
@ -24,13 +22,7 @@ public class ReflectUtilTest {
Assert.assertEquals(22, methods.length);
//过滤器测试
methods = ReflectUtil.getMethods(ExamInfoDict.class, new Filter<Method>() {
@Override
public boolean accept(Method t) {
return Integer.class.equals(t.getReturnType());
}
});
methods = ReflectUtil.getMethods(ExamInfoDict.class, t -> Integer.class.equals(t.getReturnType()));
Assert.assertEquals(4, methods.length);
final Method method = methods[0];
@ -76,6 +68,13 @@ public class ReflectUtilTest {
Field privateField = ReflectUtil.getField(TestSubClass.class, "privateField");
Assert.assertNotNull(privateField);
}
@Test
public void getFieldsTest() {
// 能够获取到父类字段
final Field[] fields = ReflectUtil.getFields(TestSubClass.class);
Assert.assertEquals(4, fields.length);
}
@Test
public void setFieldTest() {