mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
fix ReflectUtil
This commit is contained in:
parent
c62ede5276
commit
7ffdc2c473
@ -11,6 +11,7 @@
|
||||
* 【core 】 改进Bean判断和注入逻辑:支持public字段注入(issue#I1689L@Gitee)
|
||||
* 【extra】 新增SpringUtil
|
||||
* 【http 】 Get请求支持body,移除body(JSON)方法(issue#671@Github)
|
||||
* 【core 】 ReflectUtil修正getFieldValue逻辑,防止歧义
|
||||
|
||||
|
||||
### Bug修复
|
||||
|
@ -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++) {
|
||||
|
@ -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() {
|
||||
|
Loading…
Reference in New Issue
Block a user