mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix ReflectUtil
This commit is contained in:
parent
c62ede5276
commit
7ffdc2c473
@ -11,6 +11,7 @@
|
|||||||
* 【core 】 改进Bean判断和注入逻辑:支持public字段注入(issue#I1689L@Gitee)
|
* 【core 】 改进Bean判断和注入逻辑:支持public字段注入(issue#I1689L@Gitee)
|
||||||
* 【extra】 新增SpringUtil
|
* 【extra】 新增SpringUtil
|
||||||
* 【http 】 Get请求支持body,移除body(JSON)方法(issue#671@Github)
|
* 【http 】 Get请求支持body,移除body(JSON)方法(issue#671@Github)
|
||||||
|
* 【core 】 ReflectUtil修正getFieldValue逻辑,防止歧义
|
||||||
|
|
||||||
|
|
||||||
### Bug修复
|
### Bug修复
|
||||||
|
@ -199,7 +199,7 @@ public class ReflectUtil {
|
|||||||
/**
|
/**
|
||||||
* 获取字段值
|
* 获取字段值
|
||||||
*
|
*
|
||||||
* @param obj 对象
|
* @param obj 对象,如果static字段,此处为类
|
||||||
* @param fieldName 字段名
|
* @param fieldName 字段名
|
||||||
* @return 字段值
|
* @return 字段值
|
||||||
* @throws UtilException 包装IllegalAccessException异常
|
* @throws UtilException 包装IllegalAccessException异常
|
||||||
@ -208,7 +208,19 @@ public class ReflectUtil {
|
|||||||
if (null == obj || StrUtil.isBlank(fieldName)) {
|
if (null == obj || StrUtil.isBlank(fieldName)) {
|
||||||
return null;
|
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) {
|
if (null == field) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
if(obj instanceof Class){
|
||||||
|
// 静态字段获取时对象为null
|
||||||
|
obj = null;
|
||||||
|
}
|
||||||
|
|
||||||
setAccessible(field);
|
setAccessible(field);
|
||||||
Object result;
|
Object result;
|
||||||
try {
|
try {
|
||||||
@ -236,13 +253,13 @@ public class ReflectUtil {
|
|||||||
/**
|
/**
|
||||||
* 获取所有字段的值
|
* 获取所有字段的值
|
||||||
*
|
*
|
||||||
* @param obj bean对象
|
* @param obj bean对象,如果是static字段,此处为类class
|
||||||
* @return 字段值数组
|
* @return 字段值数组
|
||||||
* @since 4.1.17
|
* @since 4.1.17
|
||||||
*/
|
*/
|
||||||
public static Object[] getFieldsValue(Object obj) {
|
public static Object[] getFieldsValue(Object obj) {
|
||||||
if (null != obj) {
|
if (null != obj) {
|
||||||
final Field[] fields = getFields(obj.getClass());
|
final Field[] fields = getFields(obj instanceof Class ? (Class<?>)obj : obj.getClass());
|
||||||
if (null != fields) {
|
if (null != fields) {
|
||||||
final Object[] values = new Object[fields.length];
|
final Object[] values = new Object[fields.length];
|
||||||
for (int i = 0; i < fields.length; i++) {
|
for (int i = 0; i < fields.length; i++) {
|
||||||
|
@ -1,14 +1,12 @@
|
|||||||
package cn.hutool.core.util;
|
package cn.hutool.core.util;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import cn.hutool.core.lang.test.bean.ExamInfoDict;
|
||||||
import java.lang.reflect.Method;
|
import cn.hutool.core.util.ClassUtilTest.TestSubClass;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import cn.hutool.core.lang.Filter;
|
import java.lang.reflect.Field;
|
||||||
import cn.hutool.core.lang.test.bean.ExamInfoDict;
|
import java.lang.reflect.Method;
|
||||||
import cn.hutool.core.util.ClassUtilTest.TestSubClass;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 反射工具类单元测试
|
* 反射工具类单元测试
|
||||||
@ -24,13 +22,7 @@ public class ReflectUtilTest {
|
|||||||
Assert.assertEquals(22, methods.length);
|
Assert.assertEquals(22, methods.length);
|
||||||
|
|
||||||
//过滤器测试
|
//过滤器测试
|
||||||
methods = ReflectUtil.getMethods(ExamInfoDict.class, new Filter<Method>() {
|
methods = ReflectUtil.getMethods(ExamInfoDict.class, t -> Integer.class.equals(t.getReturnType()));
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean accept(Method t) {
|
|
||||||
return Integer.class.equals(t.getReturnType());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
Assert.assertEquals(4, methods.length);
|
Assert.assertEquals(4, methods.length);
|
||||||
final Method method = methods[0];
|
final Method method = methods[0];
|
||||||
@ -76,6 +68,13 @@ public class ReflectUtilTest {
|
|||||||
Field privateField = ReflectUtil.getField(TestSubClass.class, "privateField");
|
Field privateField = ReflectUtil.getField(TestSubClass.class, "privateField");
|
||||||
Assert.assertNotNull(privateField);
|
Assert.assertNotNull(privateField);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getFieldsTest() {
|
||||||
|
// 能够获取到父类字段
|
||||||
|
final Field[] fields = ReflectUtil.getFields(TestSubClass.class);
|
||||||
|
Assert.assertEquals(4, fields.length);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void setFieldTest() {
|
public void setFieldTest() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user