设计模式 动态代理 cglib(笔记)

发布于:2022-10-13 ⋅ 阅读:(307) ⋅ 点赞:(0)
public class Station {
    public void sellTicket(){
        System.out.println("已经卖出");
    }
}
public class ProxyFactory implements MethodInterceptor {
    private Station station = new Station();
    public Station getStation(){
        //创建enhancer对象,类似于Proxy
        Enhancer enhancer = new Enhancer();
        //传入要被动态代理的类
        enhancer.setSuperclass(Station.class);
        //设置回调函数,传入对象要实现 MethodInterceptor
        enhancer.setCallback(this);
        //创建匿名内部类继承setSuperClass()传入的类
        Station station1 =(Station) enhancer.create();
        return station1;
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("已经收取手续费");
        Object result = method.invoke(station, objects);
        System.out.println("打印结果");
        return result;
    }
}
public class Client {
    public static void main(String[] args) {
        //创建代理工厂
        ProxyFactory proxyFactory = new ProxyFactory();
        //获取继承Station的匿名内部类
        Station station = proxyFactory.getStation();
        //调用sellTicket()
        station.sellTicket();
        //获得匿名匿名内部类的class文件名
        System.out.println(station.getClass());
        while (true){

        }
    }
}

public class Station$$EnhancerByCGLIB$$6977634a
extends Station
implements Factory {
    private boolean CGLIB$BOUND;
    private static final ThreadLocal CGLIB$THREAD_CALLBACKS;
    private static final Callback[] CGLIB$STATIC_CALLBACKS;
    private MethodInterceptor CGLIB$CALLBACK_0;
    private static final Method CGLIB$sellTicket$0$Method;
    private static final MethodProxy CGLIB$sellTicket$0$Proxy;
    private static final Object[] CGLIB$emptyArgs;
    private static final Method CGLIB$finalize$1$Method;
    private static final MethodProxy CGLIB$finalize$1$Proxy;
    private static final Method CGLIB$equals$2$Method;
    private static final MethodProxy CGLIB$equals$2$Proxy;
    private static final Method CGLIB$toString$3$Method;
    private static final MethodProxy CGLIB$toString$3$Proxy;
    private static final Method CGLIB$hashCode$4$Method;
    private static final MethodProxy CGLIB$hashCode$4$Proxy;
    private static final Method CGLIB$clone$5$Method;
    private static final MethodProxy CGLIB$clone$5$Proxy;

    public Station$$EnhancerByCGLIB$$6977634a() {
        Station$$EnhancerByCGLIB$$6977634a station$$EnhancerByCGLIB$$6977634a = this;
        Station$$EnhancerByCGLIB$$6977634a.CGLIB$BIND_CALLBACKS(station$$EnhancerByCGLIB$$6977634a);
    }

    static {
        Station$$EnhancerByCGLIB$$6977634a.CGLIB$STATICHOOK1();
    }

    protected final void finalize() throws Throwable {
        MethodInterceptor methodInterceptor = this.CGLIB$CALLBACK_0;
        if (methodInterceptor == null) {
            Station$$EnhancerByCGLIB$$6977634a.CGLIB$BIND_CALLBACKS(this);
            methodInterceptor = this.CGLIB$CALLBACK_0;
        }
        if (methodInterceptor != null) {
            Object object = methodInterceptor.intercept(this, CGLIB$finalize$1$Method, CGLIB$emptyArgs, CGLIB$finalize$1$Proxy);
            return;
        }
        super.finalize();
    }

    public final boolean equals(Object object) {
        MethodInterceptor methodInterceptor = this.CGLIB$CALLBACK_0;
        if (methodInterceptor == null) {
            Station$$EnhancerByCGLIB$$6977634a.CGLIB$BIND_CALLBACKS(this);
            methodInterceptor = this.CGLIB$CALLBACK_0;
        }
        if (methodInterceptor != null) {
            Object object2 = methodInterceptor.intercept(this, CGLIB$equals$2$Method, new Object[]{object}, CGLIB$equals$2$Proxy);
            return object2 == null ? false : (Boolean)object2;
        }
        return super.equals(object);
    }

    public final String toString() {
        MethodInterceptor methodInterceptor = this.CGLIB$CALLBACK_0;
        if (methodInterceptor == null) {
            Station$$EnhancerByCGLIB$$6977634a.CGLIB$BIND_CALLBACKS(this);
            methodInterceptor = this.CGLIB$CALLBACK_0;
        }
        if (methodInterceptor != null) {
            return (String)methodInterceptor.intercept(this, CGLIB$toString$3$Method, CGLIB$emptyArgs, CGLIB$toString$3$Proxy);
        }
        return super.toString();
    }

    public final int hashCode() {
        MethodInterceptor methodInterceptor = this.CGLIB$CALLBACK_0;
        if (methodInterceptor == null) {
            Station$$EnhancerByCGLIB$$6977634a.CGLIB$BIND_CALLBACKS(this);
            methodInterceptor = this.CGLIB$CALLBACK_0;
        }
        if (methodInterceptor != null) {
            Object object = methodInterceptor.intercept(this, CGLIB$hashCode$4$Method, CGLIB$emptyArgs, CGLIB$hashCode$4$Proxy);
            return object == null ? 0 : ((Number)object).intValue();
        }
        return super.hashCode();
    }

    protected final Object clone() throws CloneNotSupportedException {
        MethodInterceptor methodInterceptor = this.CGLIB$CALLBACK_0;
        if (methodInterceptor == null) {
            Station$$EnhancerByCGLIB$$6977634a.CGLIB$BIND_CALLBACKS(this);
            methodInterceptor = this.CGLIB$CALLBACK_0;
        }
        if (methodInterceptor != null) {
            return methodInterceptor.intercept(this, CGLIB$clone$5$Method, CGLIB$emptyArgs, CGLIB$clone$5$Proxy);
        }
        return super.clone();
    }

    public Object newInstance(Callback callback) {
        Station$$EnhancerByCGLIB$$6977634a.CGLIB$SET_THREAD_CALLBACKS(new Callback[]{callback});
        Station$$EnhancerByCGLIB$$6977634a station$$EnhancerByCGLIB$$6977634a = new Station$$EnhancerByCGLIB$$6977634a();
        Station$$EnhancerByCGLIB$$6977634a.CGLIB$SET_THREAD_CALLBACKS(null);
        return station$$EnhancerByCGLIB$$6977634a;
    }

    public Object newInstance(Callback[] callbackArray) {
        Station$$EnhancerByCGLIB$$6977634a.CGLIB$SET_THREAD_CALLBACKS(callbackArray);
        Station$$EnhancerByCGLIB$$6977634a station$$EnhancerByCGLIB$$6977634a = new Station$$EnhancerByCGLIB$$6977634a();
        Station$$EnhancerByCGLIB$$6977634a.CGLIB$SET_THREAD_CALLBACKS(null);
        return station$$EnhancerByCGLIB$$6977634a;
    }

    public Object newInstance(Class[] classArray, Object[] objectArray, Callback[] callbackArray) {
        Station$$EnhancerByCGLIB$$6977634a station$$EnhancerByCGLIB$$6977634a;
        Station$$EnhancerByCGLIB$$6977634a.CGLIB$SET_THREAD_CALLBACKS(callbackArray);
        Class[] classArray2 = classArray;
        switch (classArray.length) {
            case 0: {
                station$$EnhancerByCGLIB$$6977634a = new Station$$EnhancerByCGLIB$$6977634a();
                break;
            }
            default: {
                throw new IllegalArgumentException("Constructor not found");
            }
        }
        Station$$EnhancerByCGLIB$$6977634a.CGLIB$SET_THREAD_CALLBACKS(null);
        return station$$EnhancerByCGLIB$$6977634a;
    }

    public static MethodProxy CGLIB$findMethodProxy(Signature signature) {
        String string = ((Object)signature).toString();
        switch (string.hashCode()) {
            case -2137181769: {
                if (!string.equals("sellTicket()V")) break;
                return CGLIB$sellTicket$0$Proxy;
            }
            case -1574182249: {
                if (!string.equals("finalize()V")) break;
                return CGLIB$finalize$1$Proxy;
            }
            case -508378822: {
                if (!string.equals("clone()Ljava/lang/Object;")) break;
                return CGLIB$clone$5$Proxy;
            }
            case 1826985398: {
                if (!string.equals("equals(Ljava/lang/Object;)Z")) break;
                return CGLIB$equals$2$Proxy;
            }
            case 1913648695: {
                if (!string.equals("toString()Ljava/lang/String;")) break;
                return CGLIB$toString$3$Proxy;
            }
            case 1984935277: {
                if (!string.equals("hashCode()I")) break;
                return CGLIB$hashCode$4$Proxy;
            }
        }
        return null;
    }

    public void setCallback(int n, Callback callback) {
        switch (n) {
            case 0: {
                this.CGLIB$CALLBACK_0 = (MethodInterceptor)callback;
                break;
            }
        }
    }

    public void setCallbacks(Callback[] callbackArray) {
        Callback[] callbackArray2 = callbackArray;
        Station$$EnhancerByCGLIB$$6977634a station$$EnhancerByCGLIB$$6977634a = this;
        this.CGLIB$CALLBACK_0 = (MethodInterceptor)callbackArray[0];
    }

    public final void sellTicket() {
        MethodInterceptor methodInterceptor = this.CGLIB$CALLBACK_0;
        if (methodInterceptor == null) {
            Station$$EnhancerByCGLIB$$6977634a.CGLIB$BIND_CALLBACKS(this);
            methodInterceptor = this.CGLIB$CALLBACK_0;
        }
        if (methodInterceptor != null) {
            Object object = methodInterceptor.intercept(this, CGLIB$sellTicket$0$Method, CGLIB$emptyArgs, CGLIB$sellTicket$0$Proxy);
            return;
        }
        super.sellTicket();
    }

    public Callback getCallback(int n) {
        MethodInterceptor methodInterceptor;
        Station$$EnhancerByCGLIB$$6977634a.CGLIB$BIND_CALLBACKS(this);
        switch (n) {
            case 0: {
                methodInterceptor = this.CGLIB$CALLBACK_0;
                break;
            }
            default: {
                methodInterceptor = null;
            }
        }
        return methodInterceptor;
    }

    public Callback[] getCallbacks() {
        Station$$EnhancerByCGLIB$$6977634a.CGLIB$BIND_CALLBACKS(this);
        Station$$EnhancerByCGLIB$$6977634a station$$EnhancerByCGLIB$$6977634a = this;
        return new Callback[]{this.CGLIB$CALLBACK_0};
    }

    public static void CGLIB$SET_THREAD_CALLBACKS(Callback[] callbackArray) {
        CGLIB$THREAD_CALLBACKS.set(callbackArray);
    }

    public static void CGLIB$SET_STATIC_CALLBACKS(Callback[] callbackArray) {
        CGLIB$STATIC_CALLBACKS = callbackArray;
    }

    final boolean CGLIB$equals$2(Object object) {
        return super.equals(object);
    }

    final int CGLIB$hashCode$4() {
        return super.hashCode();
    }

    final Object CGLIB$clone$5() throws CloneNotSupportedException {
        return super.clone();
    }

    final String CGLIB$toString$3() {
        return super.toString();
    }

    final void CGLIB$finalize$1() throws Throwable {
        super.finalize();
    }

    static void CGLIB$STATICHOOK1() {
        CGLIB$THREAD_CALLBACKS = new ThreadLocal();
        CGLIB$emptyArgs = new Object[0];
        Class<?> clazz = Class.forName("review.design_model.proxy.cglib.Station$$EnhancerByCGLIB$$6977634a");
        Class<?> clazz2 = Class.forName("review.design_model.proxy.cglib.Station");
        CGLIB$sellTicket$0$Method = ReflectUtils.findMethods(new String[]{"sellTicket", "()V"}, clazz2.getDeclaredMethods())[0];
        CGLIB$sellTicket$0$Proxy = MethodProxy.create(clazz2, clazz, "()V", "sellTicket", "CGLIB$sellTicket$0");
        clazz2 = Class.forName("java.lang.Object");
        Method[] methodArray = ReflectUtils.findMethods(new String[]{"finalize", "()V", "equals", "(Ljava/lang/Object;)Z", "toString", "()Ljava/lang/String;", "hashCode", "()I", "clone", "()Ljava/lang/Object;"}, clazz2.getDeclaredMethods());
        CGLIB$finalize$1$Method = methodArray[0];
        CGLIB$finalize$1$Proxy = MethodProxy.create(clazz2, clazz, "()V", "finalize", "CGLIB$finalize$1");
        CGLIB$equals$2$Method = methodArray[1];
        CGLIB$equals$2$Proxy = MethodProxy.create(clazz2, clazz, "(Ljava/lang/Object;)Z", "equals", "CGLIB$equals$2");
        CGLIB$toString$3$Method = methodArray[2];
        CGLIB$toString$3$Proxy = MethodProxy.create(clazz2, clazz, "()Ljava/lang/String;", "toString", "CGLIB$toString$3");
        CGLIB$hashCode$4$Method = methodArray[3];
        CGLIB$hashCode$4$Proxy = MethodProxy.create(clazz2, clazz, "()I", "hashCode", "CGLIB$hashCode$4");
        CGLIB$clone$5$Method = methodArray[4];
        CGLIB$clone$5$Proxy = MethodProxy.create(clazz2, clazz, "()Ljava/lang/Object;", "clone", "CGLIB$clone$5");
    }

    final void CGLIB$sellTicket$0() {
        super.sellTicket();
    }

    private static final void CGLIB$BIND_CALLBACKS(Object object) {
        block2: {
            Object object2;
            block3: {
                Station$$EnhancerByCGLIB$$6977634a station$$EnhancerByCGLIB$$6977634a = (Station$$EnhancerByCGLIB$$6977634a)object;
                if (station$$EnhancerByCGLIB$$6977634a.CGLIB$BOUND) break block2;
                station$$EnhancerByCGLIB$$6977634a.CGLIB$BOUND = true;
                object2 = CGLIB$THREAD_CALLBACKS.get();
                if (object2 != null) break block3;
                object2 = CGLIB$STATIC_CALLBACKS;
                if (CGLIB$STATIC_CALLBACKS == null) break block2;
            }
            station$$EnhancerByCGLIB$$6977634a.CGLIB$CALLBACK_0 = (MethodInterceptor)((Callback[])object2)[0];
        }
    }
}

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到