Class ReflectCache
java.lang.Object
cloud.opencode.base.reflect.ReflectCache
Reflection Cache - Centralized Cache for Reflection Results
反射缓存 - 反射结果的集中缓存
Provides high-performance caching for reflection operations to avoid repeated reflection lookups. Uses weak-key caching for Class-keyed entries to prevent ClassLoader memory leaks in dynamic classloading environments.
为反射操作提供高性能缓存,避免重复的反射查找。 对 Class 键使用弱引用缓存,防止动态类加载环境中的 ClassLoader 内存泄漏。
Features | 主要功能:
- Field caching - 字段缓存
- Method caching - 方法缓存
- Constructor caching - 构造器缓存
- TypeToken caching - 类型令牌缓存
- Thread-safe operations - 线程安全操作
- Cache statistics - 缓存统计
- GC-aware: Class entries are released when ClassLoader is collected - GC感知: 当ClassLoader回收时自动释放
Usage Examples | 使用示例:
// Get cached fields
Field[] fields = ReflectCache.getFields(MyClass.class)
.orElseGet(() -> {
Field[] f = MyClass.class.getDeclaredFields();
ReflectCache.cacheFields(MyClass.class, f);
return f;
});
// Get cache statistics
CacheStats stats = ReflectCache.getCacheStats();
System.out.println("Hit rate: " + stats.hitRate());
Security | 安全性:
- Thread-safe: Yes (uses synchronized WeakHashMap and ConcurrentHashMap) - 线程安全: 是
- Null-safe: Yes (null inputs are ignored) - 空值安全: 是(null输入被忽略)
- GC-safe: Class-keyed caches use weak keys to prevent ClassLoader leaks - GC安全: Class键使用弱引用
- Since:
- JDK 25, opencode-base-reflect V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordCache Statistics Record 缓存统计记录 -
Method Summary
Modifier and TypeMethodDescriptionstatic voidcacheConstructors(Class<?> clazz, Constructor<?>[] constructors) Caches constructors for a class 缓存类的构造器static voidcacheFields(Class<?> clazz, Field[] fields) Caches fields for a class 缓存类的字段static voidcacheMethods(Class<?> clazz, Method[] methods) Caches methods for a class 缓存类的方法static <T> voidcacheTypeToken(Type type, TypeToken<T> token) Caches TypeToken for a type 缓存类型的类型令牌static voidClears all caches 清除所有缓存static voidclearCache(Class<?> clazz) Clears cache for a specific class 清除特定类的缓存static ReflectCache.CacheStatsGets cache statistics 获取缓存统计static Optional<Constructor<?>[]> getConstructors(Class<?> clazz) Gets cached constructors for a class 获取类的缓存构造器Gets cached fields for a class 获取类的缓存字段getMethods(Class<?> clazz) Gets cached methods for a class 获取类的缓存方法getTypeToken(Type type) Gets cached TypeToken for a type 获取类型的缓存类型令牌
-
Method Details
-
getFields
-
cacheFields
-
getMethods
-
cacheMethods
-
getConstructors
Gets cached constructors for a class 获取类的缓存构造器- Parameters:
clazz- the class | 类- Returns:
- cached constructors or empty | 缓存的构造器或空
-
cacheConstructors
Caches constructors for a class 缓存类的构造器- Parameters:
clazz- the class | 类constructors- the constructors to cache | 要缓存的构造器
-
getTypeToken
-
cacheTypeToken
-
clearCache
public static void clearCache()Clears all caches 清除所有缓存 -
clearCache
Clears cache for a specific class 清除特定类的缓存- Parameters:
clazz- the class to clear cache for | 要清除缓存的类
-
getCacheStats
Gets cache statistics 获取缓存统计- Returns:
- cache statistics | 缓存统计
-