Class ReflectCache

java.lang.Object
cloud.opencode.base.reflect.ReflectCache

public final class ReflectCache extends Object
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:
  • Method Details

    • getFields

      public static Optional<Field[]> getFields(Class<?> clazz)
      Gets cached fields for a class 获取类的缓存字段
      Parameters:
      clazz - the class | 类
      Returns:
      cached fields or empty | 缓存的字段或空
    • cacheFields

      public static void cacheFields(Class<?> clazz, Field[] fields)
      Caches fields for a class 缓存类的字段
      Parameters:
      clazz - the class | 类
      fields - the fields to cache | 要缓存的字段
    • getMethods

      public static Optional<Method[]> getMethods(Class<?> clazz)
      Gets cached methods for a class 获取类的缓存方法
      Parameters:
      clazz - the class | 类
      Returns:
      cached methods or empty | 缓存的方法或空
    • cacheMethods

      public static void cacheMethods(Class<?> clazz, Method[] methods)
      Caches methods for a class 缓存类的方法
      Parameters:
      clazz - the class | 类
      methods - the methods to cache | 要缓存的方法
    • getConstructors

      public static Optional<Constructor<?>[]> getConstructors(Class<?> clazz)
      Gets cached constructors for a class 获取类的缓存构造器
      Parameters:
      clazz - the class | 类
      Returns:
      cached constructors or empty | 缓存的构造器或空
    • cacheConstructors

      public static void cacheConstructors(Class<?> clazz, Constructor<?>[] constructors)
      Caches constructors for a class 缓存类的构造器
      Parameters:
      clazz - the class | 类
      constructors - the constructors to cache | 要缓存的构造器
    • getTypeToken

      public static <T> Optional<TypeToken<T>> getTypeToken(Type type)
      Gets cached TypeToken for a type 获取类型的缓存类型令牌
      Type Parameters:
      T - the type parameter | 类型参数
      Parameters:
      type - the type | 类型
      Returns:
      cached TypeToken or empty | 缓存的类型令牌或空
    • cacheTypeToken

      public static <T> void cacheTypeToken(Type type, TypeToken<T> token)
      Caches TypeToken for a type 缓存类型的类型令牌
      Type Parameters:
      T - the type parameter | 类型参数
      Parameters:
      type - the type | 类型
      token - the TypeToken to cache | 要缓存的类型令牌
    • clearCache

      public static void clearCache()
      Clears all caches 清除所有缓存
    • clearCache

      public static void clearCache(Class<?> clazz)
      Clears cache for a specific class 清除特定类的缓存
      Parameters:
      clazz - the class to clear cache for | 要清除缓存的类
    • getCacheStats

      public static ReflectCache.CacheStats getCacheStats()
      Gets cache statistics 获取缓存统计
      Returns:
      cache statistics | 缓存统计