mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
add method
This commit is contained in:
parent
4209cffa06
commit
140e1d546f
@ -150,6 +150,20 @@ public class ReflectUtil {
|
||||
return ArrayUtil.firstMatch((field) -> name.equals(getFieldName(field)), fields);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取本类定义的指定名称的字段,包括私有字段,但是不包括父类字段
|
||||
* @param beanClass Bean的Class
|
||||
* @param name 字段名称
|
||||
* @return 字段对象,如果未找到返回{@code null}
|
||||
*/
|
||||
public static Field getDeClearField(final Class<?> beanClass, final String name){
|
||||
try {
|
||||
return beanClass.getDeclaredField(name);
|
||||
} catch (NoSuchFieldException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定类中字段名和字段对应的有序Map,包括其父类中的字段<br>
|
||||
* 如果子类与父类中存在同名字段,则这两个字段同时存在,子类字段在前,父类字段在后。
|
||||
|
@ -308,7 +308,7 @@ public class ReflectUtilTest {
|
||||
Assert.assertNull(noField);
|
||||
|
||||
// 获取不到父类字段
|
||||
final Field field = ReflectUtil.getField(TestSubClass.class, "field");
|
||||
final Field field = ReflectUtil.getDeClearField(TestSubClass.class, "field");
|
||||
Assert.assertNull(field);
|
||||
|
||||
final Field subField = ReflectUtil.getField(TestSubClass.class, "subField");
|
||||
|
Loading…
x
Reference in New Issue
Block a user