Class ProxyUtil
java.lang.Object
cloud.opencode.base.reflect.proxy.ProxyUtil
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 Summary
Modifier and TypeMethodDescriptionstatic booleanareProxyable(Class<?>... interfaces) Validates multiple interfaces for proxying 验证多个接口是否适合代理static voidClears proxy class cache 清除代理类缓存static intGets cache size 获取缓存大小static ObjectgetDefaultReturnValue(Class<?> returnType) Gets the default return value for a type 获取类型的默认返回值static InvocationHandlergetInvocationHandler(Object proxy) Gets the invocation handler of a proxy 获取代理的调用处理器static Optional<InvocationHandler> getInvocationHandlerSafe(Object proxy) Gets the invocation handler safely 安全获取调用处理器static Class<?> getProxyClass(ClassLoader loader, Class<?>... interfaces) Gets or creates proxy class (cached) 获取或创建代理类(缓存)getProxyInterfaceList(Object proxy) Gets interfaces implemented by a proxy as list 获取代理实现的接口列表static Class<?>[]getProxyInterfaces(Object proxy) Gets interfaces implemented by a proxy 获取代理实现的接口static booleanimplementsInterface(Object proxy, Class<?> interfaceClass) Checks if proxy implements an interface 检查代理是否实现接口static booleanisEqualsMethod(Method method) Checks if a method is equals 检查方法是否为equalsstatic booleanisHashCodeMethod(Method method) Checks if a method is hashCode 检查方法是否为hashCodestatic booleanisObjectMethod(Method method) Checks if a method is from Object class 检查方法是否来自Object类static booleanChecks if an object is a proxy instance 检查对象是否为代理实例static booleanisProxyable(Class<?> clazz) Validates that a class is an interface suitable for proxying 验证类是否为适合代理的接口static booleanisProxyClass(Class<?> clazz) Checks if a class is a proxy class 检查类是否为代理类static booleanisToStringMethod(Method method) Checks if a method is toString 检查方法是否为toStringstatic <T> TnewProxyInstance(Class<T> interfaceClass, InvocationHandler handler) Creates a proxy instance for a single interface 为单个接口创建代理实例static ObjectnewProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler handler) Creates a proxy instance 创建代理实例
-
Method Details
-
getProxyClass
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
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
Checks if a class is a proxy class 检查类是否为代理类- Parameters:
clazz- the class | 类- Returns:
- true if proxy class | 如果是代理类返回true
-
isProxy
Checks if an object is a proxy instance 检查对象是否为代理实例- Parameters:
obj- the object | 对象- Returns:
- true if proxy | 如果是代理返回true
-
getInvocationHandler
Gets the invocation handler of a proxy 获取代理的调用处理器- Parameters:
proxy- the proxy | 代理- Returns:
- the invocation handler | 调用处理器
-
getInvocationHandlerSafe
Gets the invocation handler safely 安全获取调用处理器- Parameters:
proxy- the proxy | 代理- Returns:
- Optional of handler | 处理器的Optional
-
getProxyInterfaces
-
getProxyInterfaceList
-
implementsInterface
-
isProxyable
Validates that a class is an interface suitable for proxying 验证类是否为适合代理的接口- Parameters:
clazz- the class | 类- Returns:
- true if valid | 如果有效返回true
-
areProxyable
Validates multiple interfaces for proxying 验证多个接口是否适合代理- Parameters:
interfaces- the interfaces | 接口- Returns:
- true if all valid | 如果全部有效返回true
-
isObjectMethod
Checks if a method is from Object class 检查方法是否来自Object类- Parameters:
method- the method | 方法- Returns:
- true if Object method | 如果是Object方法返回true
-
isEqualsMethod
Checks if a method is equals 检查方法是否为equals- Parameters:
method- the method | 方法- Returns:
- true if equals | 如果是equals返回true
-
isHashCodeMethod
Checks if a method is hashCode 检查方法是否为hashCode- Parameters:
method- the method | 方法- Returns:
- true if hashCode | 如果是hashCode返回true
-
isToStringMethod
Checks if a method is toString 检查方法是否为toString- Parameters:
method- the method | 方法- Returns:
- true if toString | 如果是toString返回true
-
getDefaultReturnValue
-
clearCache
public static void clearCache()Clears proxy class cache 清除代理类缓存 -
getCacheSize
public static int getCacheSize()Gets cache size 获取缓存大小- Returns:
- the cache size | 缓存大小
-