Class ClassIndexReader

java.lang.Object
cloud.opencode.base.classloader.index.ClassIndexReader

public final class ClassIndexReader extends Object
Class Index Reader - Loads a pre-built class index from the classpath 类索引读取器 - 从 classpath 加载预构建的类索引

Reads the JSON index file produced by ClassIndexWriter and validates it against the current classpath hash for staleness detection.

读取由 ClassIndexWriter 生成的 JSON 索引文件, 并通过当前 classpath 哈希值进行陈旧检测验证。

Usage Examples | 使用示例:

Optional<ClassIndex> index = ClassIndexReader.load();
if (index.isPresent() && ClassIndexReader.isValid(index.get())) {
    // use index
}

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
Since:
JDK 25, opencode-base-classloader V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    Deprecated, for removal: This API element is subject to removal in a future version.
    Use invalidateCache() instead
    static void
    Invalidate the singleton cache, forcing the next load() call to re-read the index from classpath.
    static boolean
    Check whether the index is still valid by comparing its classpath hash with the current one.
    Load the class index from the default classpath location using the context class loader 使用上下文类加载器从默认 classpath 位置加载类索引
    load(ClassLoader classLoader)
    Load the class index from the default classpath location using the specified class loader 使用指定的类加载器从默认 classpath 位置加载类索引

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • load

      public static Optional<ClassIndex> load()
      Load the class index from the default classpath location using the context class loader 使用上下文类加载器从默认 classpath 位置加载类索引
      Returns:
      the loaded index, or empty if not found or version incompatible | 加载的索引,未找到或版本不兼容则返回 empty
    • load

      public static Optional<ClassIndex> load(ClassLoader classLoader)
      Load the class index from the default classpath location using the specified class loader 使用指定的类加载器从默认 classpath 位置加载类索引
      Parameters:
      classLoader - class loader to use for resource lookup | 用于资源查找的类加载器
      Returns:
      the loaded index, or empty if not found or version incompatible | 加载的索引,未找到或版本不兼容则返回 empty
    • isValid

      public static boolean isValid(ClassIndex index)
      Check whether the index is still valid by comparing its classpath hash with the current one. If invalid, the cached index is automatically cleared so that subsequent load() calls will re-read from classpath. 通过比较 classpath 哈希值与当前值来检查索引是否仍然有效。 若无效,自动清除缓存索引,以便后续 load() 调用重新从 classpath 读取。
      Parameters:
      index - the class index to validate | 要验证的类索引
      Returns:
      true if the classpath hash matches | 如果 classpath 哈希值匹配则返回 true
    • invalidateCache

      public static void invalidateCache()
      Invalidate the singleton cache, forcing the next load() call to re-read the index from classpath. Useful when the classpath changes at runtime (e.g. hot deployment). 使单例缓存失效,强制下一次 load() 调用从 classpath 重新读取索引。 在运行时 classpath 变化(如热部署)时有用。
    • clearCache

      @Deprecated(since="1.0.3", forRemoval=true) public static void clearCache()
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use invalidateCache() instead
      Clear the singleton cache (useful for testing) 清除单例缓存(便于测试)