Class ClassDependencyAnalyzer

java.lang.Object
cloud.opencode.base.classloader.dependency.ClassDependencyAnalyzer

public final class ClassDependencyAnalyzer extends Object
Class Dependency Analyzer - Bytecode-level dependency analysis utility 类依赖分析器 - 字节码级别的依赖分析工具

Parses the constant pool of Java class files to extract dependency information without loading the classes. Uses Tarjan's algorithm to detect cyclic dependencies in dependency graphs.

解析 Java 类文件的常量池以提取依赖信息,无需加载类。 使用 Tarjan 算法检测依赖图中的循环依赖。

Features | 主要功能:

  • Analyze class dependencies from bytecode - 从字节码分析类依赖
  • Build dependency graphs for packages - 为包构建依赖图
  • Detect cyclic dependencies using Tarjan's SCC algorithm - 使用 Tarjan SCC 算法检测循环依赖
  • No class loading required — reads raw bytecode only - 无需加载类,仅读取原始字节码

Usage Examples | 使用示例:

// Analyze a single class
Set<String> deps = ClassDependencyAnalyzer.analyze(
    "com.example.MyClass", Thread.currentThread().getContextClassLoader());

// Analyze from bytecode
byte[] bytecode = Files.readAllBytes(Path.of("MyClass.class"));
Set<String> deps = ClassDependencyAnalyzer.analyze(bytecode);

// Detect cycles
DependencyGraph graph = ClassDependencyAnalyzer.analyzePackage(
    "com.example", classLoader);
List<CyclicDependency> cycles = ClassDependencyAnalyzer.detectCycles(graph);

Security | 安全性:

  • Thread-safe: Yes (stateless utility) - 线程安全: 是(无状态工具类)
  • No class loading — only bytecode reading - 不加载类,仅读取字节码
Since:
JDK 25, opencode-base-classloader V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • analyze

      public static Set<String> analyze(String className, ClassLoader classLoader) throws IOException
      Analyze the dependencies of a class by reading its bytecode from the ClassLoader. 通过从类加载器读取字节码来分析类的依赖。
      Parameters:
      className - the fully qualified class name | 完全限定类名
      classLoader - the ClassLoader to read bytecode from | 用于读取字节码的类加载器
      Returns:
      set of dependency class names (excluding java.*, javax.*, jdk.*, arrays, and self) | 依赖类名集合(排除 java.*、javax.*、jdk.*、数组类型和自身)
      Throws:
      NullPointerException - if any parameter is null | 如果参数为 null 则抛出空指针异常
      IOException - if the class bytecode cannot be read | 如果无法读取类字节码
    • analyze

      public static Set<String> analyze(byte[] bytecode) throws IOException
      Analyze the dependencies of a class from raw bytecode. 从原始字节码分析类的依赖。
      Parameters:
      bytecode - the class file bytecode | 类文件字节码
      Returns:
      set of dependency class names (excluding java.*, javax.*, jdk.*, and arrays) | 依赖类名集合(排除 java.*、javax.*、jdk.* 和数组类型)
      Throws:
      NullPointerException - if bytecode is null | 如果字节码为 null 则抛出空指针异常
      IOException - if the bytecode is malformed | 如果字节码格式错误
    • analyzePackage

      public static DependencyGraph analyzePackage(String packageName, ClassLoader classLoader) throws IOException
      Analyze all classes in a package and build a dependency graph. 分析包中的所有类并构建依赖图。

      Note: This method requires the ClassLoader to provide a resource listing for the package directory. Not all ClassLoaders support this.

      注意:此方法要求类加载器为包目录提供资源列表。 并非所有类加载器都支持此功能。

      Parameters:
      packageName - the package name | 包名
      classLoader - the ClassLoader to scan | 要扫描的类加载器
      Returns:
      the dependency graph for all discovered classes | 所有发现的类的依赖图
      Throws:
      NullPointerException - if any parameter is null | 如果参数为 null 则抛出空指针异常
      IOException - if bytecode cannot be read | 如果无法读取字节码
    • detectCycles

      public static List<CyclicDependency> detectCycles(DependencyGraph graph)
      Detect cyclic dependencies in a dependency graph using Tarjan's SCC algorithm. 使用 Tarjan SCC 算法检测依赖图中的循环依赖。
      Parameters:
      graph - the dependency graph to analyze | 要分析的依赖图
      Returns:
      list of cyclic dependencies (strongly connected components of size > 1) | 循环依赖列表(大小大于 1 的强连通分量)
      Throws:
      NullPointerException - if graph is null | 如果图为 null 则抛出空指针异常