Class OpenProxy

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

public final class OpenProxy extends Object
Proxy Facade Entry Class 代理门面入口类

Provides common dynamic proxy operations API.

提供常用动态代理操作API。

Features | 主要功能:

  • JDK dynamic proxy creation - JDK动态代理创建
  • Proxy inspection - 代理检查
  • Invocation handler utilities - 调用处理器工具

Usage Examples | 使用示例:

// Create proxy with interceptor
MyService proxy = OpenProxy.create(MyService.class, (p, method, args, invoker) -> {
    System.out.println("Before: " + method.getName());
    return invoker.invoke(args);
});

// Check if object is proxy
boolean isProxy = OpenProxy.isProxy(obj);

Security | 安全性:

  • Thread-safe: Yes (stateless utility class) - 线程安全: 是(无状态工具类)
  • Null-safe: No (caller must ensure non-null arguments) - 空值安全: 否(调用方须确保非空参数)
Since:
JDK 25, opencode-base-reflect V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final record 
    Method call record 方法调用记录
    static final record 
    Recording proxy wrapper 记录代理包装器
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> T
    create(Class<T> interfaceClass, MethodInterceptor interceptor)
    Creates a proxy for an interface with interceptor 为接口创建带拦截器的代理
    static <T> T
    create(Class<T> interfaceClass, InvocationHandler handler)
    Creates a proxy for an interface with handler 为接口创建带处理器的代理
    static Object
    create(InvocationHandler handler, Class<?>... interfaces)
    Creates a proxy for multiple interfaces 为多个接口创建代理
    static <T> T
    createNoOp(Class<T> interfaceClass)
    Creates a no-op proxy that returns default values 创建返回默认值的空操作代理
    createRecording(Class<T> interfaceClass)
    Creates a mock proxy that records method calls 创建记录方法调用的模拟代理
    static <T> ProxyFactory<T>
    factory(Class<T> interfaceClass)
    Creates a factory for building proxies 创建构建代理的工厂
    Gets the invocation handler of a proxy 获取代理的调用处理器
    static Class<?>[]
    Gets the interfaces implemented by a proxy 获取代理实现的接口
    static boolean
    implementsInterface(Object proxy, Class<?> interfaceClass)
    Checks if proxy implements an interface 检查代理是否实现接口
    static boolean
    isProxy(Object object)
    Checks if an object is a proxy 检查对象是否为代理
    static <T> T
    unwrap(Object proxy)
    Unwraps a proxy to get the underlying target 解包代理获取底层目标
    static <T> T
    wrap(Class<T> interfaceClass, T target)
    Creates a proxy wrapping a target object 创建包装目标对象的代理
    static <T> T
    wrap(Class<T> interfaceClass, T target, MethodInterceptor interceptor)
    Creates a proxy wrapping a target with interceptor 创建带拦截器的包装目标对象的代理

    Methods inherited from class Object

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

    • create

      public static <T> T create(Class<T> interfaceClass, MethodInterceptor interceptor)
      Creates a proxy for an interface with interceptor 为接口创建带拦截器的代理
      Type Parameters:
      T - the interface type | 接口类型
      Parameters:
      interfaceClass - the interface class | 接口类
      interceptor - the method interceptor | 方法拦截器
      Returns:
      the proxy | 代理
    • create

      public static <T> T create(Class<T> interfaceClass, InvocationHandler handler)
      Creates a proxy for an interface with handler 为接口创建带处理器的代理
      Type Parameters:
      T - the interface type | 接口类型
      Parameters:
      interfaceClass - the interface class | 接口类
      handler - the invocation handler | 调用处理器
      Returns:
      the proxy | 代理
    • wrap

      public static <T> T wrap(Class<T> interfaceClass, T target)
      Creates a proxy wrapping a target object 创建包装目标对象的代理
      Type Parameters:
      T - the interface type | 接口类型
      Parameters:
      interfaceClass - the interface class | 接口类
      target - the target object | 目标对象
      Returns:
      the proxy | 代理
    • wrap

      public static <T> T wrap(Class<T> interfaceClass, T target, MethodInterceptor interceptor)
      Creates a proxy wrapping a target with interceptor 创建带拦截器的包装目标对象的代理
      Type Parameters:
      T - the interface type | 接口类型
      Parameters:
      interfaceClass - the interface class | 接口类
      target - the target object | 目标对象
      interceptor - the method interceptor | 方法拦截器
      Returns:
      the proxy | 代理
    • create

      public static Object create(InvocationHandler handler, Class<?>... interfaces)
      Creates a proxy for multiple interfaces 为多个接口创建代理
      Parameters:
      handler - the invocation handler | 调用处理器
      interfaces - the interfaces | 接口
      Returns:
      the proxy | 代理
    • factory

      public static <T> ProxyFactory<T> factory(Class<T> interfaceClass)
      Creates a factory for building proxies 创建构建代理的工厂
      Type Parameters:
      T - the interface type | 接口类型
      Parameters:
      interfaceClass - the interface class | 接口类
      Returns:
      the factory | 工厂
    • isProxy

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

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

      public static Class<?>[] getInterfaces(Object proxy)
      Gets the interfaces implemented by a proxy 获取代理实现的接口
      Parameters:
      proxy - the proxy | 代理
      Returns:
      the 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
    • unwrap

      public static <T> T unwrap(Object proxy)
      Unwraps a proxy to get the underlying target 解包代理获取底层目标
      Type Parameters:
      T - the target type | 目标类型
      Parameters:
      proxy - the proxy | 代理
      Returns:
      the target or null | 目标或null
    • createNoOp

      public static <T> T createNoOp(Class<T> interfaceClass)
      Creates a no-op proxy that returns default values 创建返回默认值的空操作代理
      Type Parameters:
      T - the interface type | 接口类型
      Parameters:
      interfaceClass - the interface class | 接口类
      Returns:
      the proxy | 代理
    • createRecording

      public static <T> OpenProxy.RecordingProxy<T> createRecording(Class<T> interfaceClass)
      Creates a mock proxy that records method calls 创建记录方法调用的模拟代理
      Type Parameters:
      T - the interface type | 接口类型
      Parameters:
      interfaceClass - the interface class | 接口类
      Returns:
      the recording proxy | 记录代理