Class ProxyUtil

java.lang.Object
cloud.opencode.base.reflect.proxy.ProxyUtil

public final class ProxyUtil extends Object
Proxy Utility Class 代理工具类

Provides low-level proxy operation utilities with caching.

提供带缓存的底层代理操作工具。

Features | 主要功能:

  • Proxy class creation with caching - 带缓存的代理类创建
  • Proxy detection and handler extraction - 代理检测和处理器提取

Usage Examples | 使用示例:

boolean isProxy = ProxyUtil.isProxy(obj);
InvocationHandler handler = ProxyUtil.getInvocationHandler(proxy);

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 proxy class lookups; proxy detection and handler extraction O(1) - 时间复杂度: 缓存命中时代理类查找为 O(1);代理检测和处理器提取 O(1)
  • Space complexity: O(1) per cached proxy class entry - 空间复杂度: O(1),每个缓存代理类条目
Since:
JDK 25, opencode-base-reflect V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • getProxyClass

      public static Class<?> getProxyClass(ClassLoader loader, Class<?>... interfaces)
      Gets or creates proxy class (cached) 获取或创建代理类(缓存)
      Parameters:
      loader - the class loader | 类加载器
      interfaces - the interfaces | 接口
      Returns:
      the proxy class | 代理类
    • newProxyInstance

      public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler handler)
      Creates a proxy instance 创建代理实例
      Parameters:
      loader - the class loader | 类加载器
      interfaces - the interfaces | 接口
      handler - the invocation handler | 调用处理器
      Returns:
      the proxy instance | 代理实例
    • newProxyInstance

      public static <T> T newProxyInstance(Class<T> interfaceClass, InvocationHandler handler)
      Creates a proxy instance for a single interface 为单个接口创建代理实例
      Type Parameters:
      T - the interface type | 接口类型
      Parameters:
      interfaceClass - the interface class | 接口类
      handler - the invocation handler | 调用处理器
      Returns:
      the proxy instance | 代理实例
    • isProxyClass

      public static boolean isProxyClass(Class<?> clazz)
      Checks if a class is a proxy class 检查类是否为代理类
      Parameters:
      clazz - the class | 类
      Returns:
      true if proxy class | 如果是代理类返回true
    • isProxy

      public static boolean isProxy(Object obj)
      Checks if an object is a proxy instance 检查对象是否为代理实例
      Parameters:
      obj - the object | 对象
      Returns:
      true if proxy | 如果是代理返回true
    • getInvocationHandler

      public static InvocationHandler getInvocationHandler(Object proxy)
      Gets the invocation handler of a proxy 获取代理的调用处理器
      Parameters:
      proxy - the proxy | 代理
      Returns:
      the invocation handler | 调用处理器
    • getInvocationHandlerSafe

      public static Optional<InvocationHandler> getInvocationHandlerSafe(Object proxy)
      Gets the invocation handler safely 安全获取调用处理器
      Parameters:
      proxy - the proxy | 代理
      Returns:
      Optional of handler | 处理器的Optional
    • getProxyInterfaces

      public static Class<?>[] getProxyInterfaces(Object proxy)
      Gets interfaces implemented by a proxy 获取代理实现的接口
      Parameters:
      proxy - the proxy | 代理
      Returns:
      array of interfaces | 接口数组
    • getProxyInterfaceList

      public static List<Class<?>> getProxyInterfaceList(Object proxy)
      Gets interfaces implemented by a proxy as list 获取代理实现的接口列表
      Parameters:
      proxy - the proxy | 代理
      Returns:
      list of interfaces | 接口列表
    • implementsInterface

      public static boolean implementsInterface(Object proxy, Class<?> interfaceClass)
      Checks if proxy implements an interface 检查代理是否实现接口
      Parameters:
      proxy - the proxy | 代理
      interfaceClass - the interface class | 接口类
      Returns:
      true if implements | 如果实现返回true
    • isProxyable

      public static boolean isProxyable(Class<?> clazz)
      Validates that a class is an interface suitable for proxying 验证类是否为适合代理的接口
      Parameters:
      clazz - the class | 类
      Returns:
      true if valid | 如果有效返回true
    • areProxyable

      public static boolean areProxyable(Class<?>... interfaces)
      Validates multiple interfaces for proxying 验证多个接口是否适合代理
      Parameters:
      interfaces - the interfaces | 接口
      Returns:
      true if all valid | 如果全部有效返回true
    • isObjectMethod

      public static boolean isObjectMethod(Method method)
      Checks if a method is from Object class 检查方法是否来自Object类
      Parameters:
      method - the method | 方法
      Returns:
      true if Object method | 如果是Object方法返回true
    • isEqualsMethod

      public static boolean isEqualsMethod(Method method)
      Checks if a method is equals 检查方法是否为equals
      Parameters:
      method - the method | 方法
      Returns:
      true if equals | 如果是equals返回true
    • isHashCodeMethod

      public static boolean isHashCodeMethod(Method method)
      Checks if a method is hashCode 检查方法是否为hashCode
      Parameters:
      method - the method | 方法
      Returns:
      true if hashCode | 如果是hashCode返回true
    • isToStringMethod

      public static boolean isToStringMethod(Method method)
      Checks if a method is toString 检查方法是否为toString
      Parameters:
      method - the method | 方法
      Returns:
      true if toString | 如果是toString返回true
    • getDefaultReturnValue

      public static Object getDefaultReturnValue(Class<?> returnType)
      Gets the default return value for a type 获取类型的默认返回值
      Parameters:
      returnType - the return type | 返回类型
      Returns:
      the default value | 默认值
    • clearCache

      public static void clearCache()
      Clears proxy class cache 清除代理类缓存
    • getCacheSize

      public static int getCacheSize()
      Gets cache size 获取缓存大小
      Returns:
      the cache size | 缓存大小