shz.spring.CglibProxy Maven / Gradle / Ivy
package shz.spring;
import org.springframework.cglib.core.DebuggingClassWriter;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import shz.core.*;
import java.lang.reflect.Method;
import java.util.function.Function;
@SuppressWarnings("unchecked")
public final class CglibProxy implements MethodInterceptor {
public static final class Param {
public final Object proxy;
public final Object bean;
public final Method method;
public final Object[] args;
public final MethodProxy methodProxy;
Param(Object proxy, Object bean, Method method, Object[] args, MethodProxy methodProxy) {
this.proxy = proxy;
this.bean = bean;
this.method = method;
this.args = args;
this.methodProxy = methodProxy;
}
}
private final T bean;
private final Function executor;
private CglibProxy(T bean, Function executor) {
this.bean = bean;
this.executor = executor;
}
@Override
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
Class> dCls = method.getDeclaringClass();
if (dCls == Object.class) {
switch (method.getName()) {
case "hashCode":
return System.identityHashCode(proxy);
case "equals":
return proxy == args[0];
default:
return method.invoke(bean, args);
}
}
Object invoke = executor.apply(new Param(proxy, bean, method, args, methodProxy));
if (invoke == null) return null;
Class> rt = method.getReturnType();
if (rt == void.class || rt == Void.class) return null;
if (!rt.isInstance(invoke)) invoke = FieldSetter.create(invoke, rt, method.getGenericReturnType());
return invoke;
}
public static T getProxy(T bean, Function executor, String path) {
if (NullHelp.nonBlank(path)) System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, path);
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(bean.getClass());
enhancer.setUseCache(false);
enhancer.setCallback(new CglibProxy<>(bean, executor));
return (T) enhancer.create();
}
public static T getProxy(T bean, Function executor) {
return getProxy(bean, executor, null);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy