org.rx.bean.DynamicProxyBean Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxlib Show documentation
Show all versions of rxlib Show documentation
A set of utilities for Java
package org.rx.bean;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.rx.util.function.TripleFunc;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
@RequiredArgsConstructor
public class DynamicProxyBean implements MethodInterceptor, InvocationHandler {
final TripleFunc fn;
@Getter
Object proxyObject;
MethodProxy method;
Method jdkProxy;
@Getter
public Object[] arguments;
public T argument(int i) {
return (T) arguments[i];
}
public T fastInvoke(Object instance) throws Throwable {
if (jdkProxy != null) {
return (T) jdkProxy.invoke(instance, arguments);
}
return (T) method.invoke(instance, arguments);
}
public T fastInvokeSuper() throws Throwable {
if (jdkProxy != null) {
return (T) jdkProxy.invoke(proxyObject, arguments);
}
return (T) method.invokeSuper(proxyObject, arguments);
}
@Override
public Object intercept(Object proxyObject, Method method, Object[] arguments, MethodProxy methodProxy) throws Throwable {
this.proxyObject = proxyObject;
this.method = methodProxy;
this.arguments = arguments;
return fn.invoke(method, this);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
proxyObject = proxy;
arguments = args;
jdkProxy = method;
return fn.invoke(method, this);
}
}