All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.rx.bean.DynamicProxyBean Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
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);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy