Class FieldUtil
java.lang.Object
cloud.opencode.base.reflect.FieldUtil
Field Utility Class
字段工具类
Provides low-level field operation utilities with caching.
提供带缓存的底层字段操作工具。
Features | 主要功能:
- Field discovery with caching - 带缓存的字段发现
- Inherited field resolution - 继承字段解析
- Field filtering by type, modifier, annotation - 按类型、修饰符、注解过滤字段
Usage Examples | 使用示例:
List<Field> fields = FieldUtil.getAllFields(User.class);
Field field = FieldUtil.getField(User.class, "name");
Security | 安全性:
- Thread-safe: Yes (uses ConcurrentHashMap for caching) - 线程安全: 是(使用ConcurrentHashMap缓存)
- Null-safe: No (caller must ensure non-null arguments) - 空值安全: 否(调用方须确保非空参数)
Performance | 性能特性:
- Time complexity: O(1) for cached lookups; O(f) for first access where f is the number of fields (including inherited) - 时间复杂度: 缓存命中时 O(1);首次访问为 O(f),f为字段数量(含继承)
- Space complexity: O(f) for the cached fields per class - 空间复杂度: O(f),每类缓存字段
- Since:
- JDK 25, opencode-base-reflect V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidClears field cache 清除字段缓存static voidclearCache(Class<?> clazz) Clears cache for specific class 清除特定类的缓存getAllFields(Class<?> clazz) Gets all fields including inherited (cached) 获取所有字段包含继承(缓存)static Field[]getDeclaredFields(Class<?> clazz) Gets all declared fields (no inheritance) 获取所有声明的字段(不含继承)static FieldGets field by name (cached) 按名称获取字段(缓存)static FieldgetFieldOrThrow(Class<?> clazz, String fieldName) Gets field or throws exception 获取字段或抛出异常Gets fields matching predicate 获取匹配条件的字段getFieldsOfType(Class<?> clazz, Class<?> fieldType) Gets fields of specific type 获取特定类型的字段getFieldsWithAnnotation(Class<?> clazz, Class<? extends Annotation> annotationClass) Gets fields with annotation 获取带注解的字段static ObjectgetFieldValue(Object object, String fieldName) Gets field value by name, with unified Record/Class support.static TypegetGenericType(Field field) Gets field generic type 获取字段泛型类型getInstanceFields(Class<?> clazz) Gets non-static fields 获取非静态字段getStaticFields(Class<?> clazz) Gets static fields 获取静态字段static ObjectgetStaticValue(Field field) Gets static field value 获取静态字段值static Class<?> Gets field type 获取字段类型static ObjectGets field value 获取字段值static <T> TGets field value with type cast 获取字段值并转型static booleanChecks if field is final 检查字段是否为finalstatic booleanChecks if field is static 检查字段是否为静态static booleanisTransient(Field field) Checks if field is transient 检查字段是否为transientstatic booleanisVolatile(Field field) Checks if field is volatile 检查字段是否为volatilestatic voidsetStaticValue(Field field, Object value) Sets static field value 设置静态字段值static voidSets field value 设置字段值
-
Method Details
-
getDeclaredFields
-
getAllFields
-
getField
-
getFieldOrThrow
Gets field or throws exception 获取字段或抛出异常- Parameters:
clazz- the class | 类fieldName- the field name | 字段名- Returns:
- the field | 字段
- Throws:
OpenReflectException- if not found | 如果未找到
-
getFields
-
getFieldsWithAnnotation
public static List<Field> getFieldsWithAnnotation(Class<?> clazz, Class<? extends Annotation> annotationClass) Gets fields with annotation 获取带注解的字段- Parameters:
clazz- the class | 类annotationClass- the annotation class | 注解类- Returns:
- list of fields | 字段列表
-
getFieldsOfType
-
getInstanceFields
-
getStaticFields
-
getFieldValue
Gets field value by name, with unified Record/Class support. 按名称获取字段值,统一支持 Record 和普通类。For records, invokes the accessor method. For classes, uses field reflection.
对于 Record,调用访问器方法。对于普通类,使用字段反射。
- Parameters:
object- the target object | 目标对象fieldName- the field name | 字段名- Returns:
- the field value | 字段值
- Throws:
OpenReflectException- if field not found or access failed | 如果字段未找到或访问失败
-
getValue
-
getValue
-
setValue
-
getStaticValue
-
setStaticValue
-
getType
-
getGenericType
-
isStatic
Checks if field is static 检查字段是否为静态- Parameters:
field- the field | 字段- Returns:
- true if static | 如果是静态返回true
-
isFinal
Checks if field is final 检查字段是否为final- Parameters:
field- the field | 字段- Returns:
- true if final | 如果是final返回true
-
isTransient
Checks if field is transient 检查字段是否为transient- Parameters:
field- the field | 字段- Returns:
- true if transient | 如果是transient返回true
-
isVolatile
Checks if field is volatile 检查字段是否为volatile- Parameters:
field- the field | 字段- Returns:
- true if volatile | 如果是volatile返回true
-
clearCache
public static void clearCache()Clears field cache 清除字段缓存 -
clearCache
Clears cache for specific class 清除特定类的缓存- Parameters:
clazz- the class | 类
-