乐趣区

关于动态代理:SpringAop源码分析三JDK动态代理Cglib

马上春节啦,没啥事件了,抽空整顿了下 Spring 源码。前两天整顿了 Spring 的 Ioc,前面就开始 Aop 了。看能更到哪就更到哪吧,不定期更新~~~~反正也没人看

本期做一个提前准备,钻研下 JDK 动静代理和 Cglib。看完还是搞不明确,来砍我。

JDK 动静代理

测试代码

public interface Test1 {void sayHello();

}

public interface Test2 {void sayBye();
}

public class TargetInterfaceImpl implements Test1,Test2{
    @Override
    public void sayHello() {System.out.println("sayHello");
    }

    @Override
    public void sayBye() {System.out.println("sayBye");
    }
}


public class JdkInvocation {public static void main(String[] args) {
        // 须要代理的对象
        Test1 t=new TargetInterfaceImpl();
        // 自定义 Invocation 的实现
        InvocationHandler invokeHandler= (proxy, method, args1) -> {method.invoke(t, args1);
            return null;
        };
        Test1 targetInterface=(Test1)Proxy.newProxyInstance(JdkInvocation.class.getClassLoader(),TargetInterfaceImpl.class.getInterfaces(),invokeHandler);
        targetInterface.sayHello();
        byte[] bytes = ProxyGenerator.generateProxyClass("aaaaa", new Class[]{targetInterface.getClass()});
        try(FileOutputStream fos =new FileOutputStream(new File("jdk.class"))
        ){fos.write(bytes);
            fos.flush();}catch (Exception e){e.printStackTrace();
        }
    }
}

源码剖析

JDK 动静代理次要就是实现 InvocationHandler,重写invoke 办法,交给 Proxy.newProxyInstance 生成代理对象。
ProxyGenerator.generateProxyClass为获取字节码文件,并打印,上面的能够疏忽,本次次要剖析的就是Proxy.newProxyInstance

查看源码能够很分明的看到 Class<?> cl = getProxyClass0(loader, intfs); 外围就是这一步,生成了一个 class,前面就是通过反射创立了对象,所以,咱们只有关怀 getProxyClass0 即可

@CallerSensitive
public static Object newProxyInstance(ClassLoader loader,
                                      Class<?>[] interfaces,
                                      InvocationHandler h)
    throws IllegalArgumentException
{Objects.requireNonNull(h);

    final Class<?>[] intfs = interfaces.clone();
    final SecurityManager sm = System.getSecurityManager();
    if (sm != null) {checkProxyAccess(Reflection.getCallerClass(), loader, intfs);
    }

    /*
     * Look up or generate the designated proxy class.
     */
    Class<?> cl = getProxyClass0(loader, intfs);

    /*
     * Invoke its constructor with the designated invocation handler.
     */
    try {if (sm != null) {checkNewProxyPermission(Reflection.getCallerClass(), cl);
        }

        final Constructor<?> cons = cl.getConstructor(constructorParams);
        final InvocationHandler ih = h;
        if (!Modifier.isPublic(cl.getModifiers())) {AccessController.doPrivileged(new PrivilegedAction<Void>() {public Void run() {cons.setAccessible(true);
                    return null;
                }
            });
        }
        return cons.newInstance(new Object[]{h});
    } catch (IllegalAccessException|InstantiationException e) {throw new InternalError(e.toString(), e);
    } catch (InvocationTargetException e) {Throwable t = e.getCause();
        if (t instanceof RuntimeException) {throw (RuntimeException) t;
        } else {throw new InternalError(t.toString(), t);
        }
    } catch (NoSuchMethodException e) {throw new InternalError(e.toString(), e);
    }
}

最终会进入到 java.lang.reflect.Proxy.ProxyClassFactory#apply 生成代理类,defineClass0为 native 办法,无奈跟进。

@Override
public Class<?> apply(ClassLoader loader, Class<?>[] interfaces) {


    ... 略
    /*
     * Choose a name for the proxy class to generate.
     */
    long num = nextUniqueNumber.getAndIncrement();
    String proxyName = proxyPkg + proxyClassNamePrefix + num;

    /*
     * Generate the specified proxy class.
     */
    byte[] proxyClassFile = ProxyGenerator.generateProxyClass(proxyName, interfaces, accessFlags);
    try {
        return defineClass0(loader, proxyName,
                            proxyClassFile, 0, proxyClassFile.length);
    } catch (ClassFormatError e) {
        /*
         * A ClassFormatError here means that (barring bugs in the
         * proxy class generation code) there was some other
         * invalid aspect of the arguments supplied to the proxy
         * class creation (such as virtual machine limitations
         * exceeded).
         */
        throw new IllegalArgumentException(e.toString());
    }
}

紧接着到最外层能够看到点猫腻,从代码能够看到他有个构造函数是须要传入咱们的 InvocationHandler 进来,能够

到此剖析仿佛陷入僵局,然而还记得之前把 class 文件打印进去的那段代码么,运行后果如下,查看源码,合乎上述推理,所以 JDK 动静代理就是通过指标接口,由 jdk 底层生成字节码,而后通过本人实现的 InvocationHandler 具体执行。这个就是 JDK 动静代理了。

import com.sun.proxy..Proxy0;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;

public final class aaaaa extends Proxy implements Proxy0 {
    private static Method m1;
    private static Method m8;
    private static Method m2;
    private static Method m7;
    private static Method m12;
    private static Method m14;
    private static Method m0;
    private static Method m11;
    private static Method m3;
    private static Method m13;
    private static Method m5;
    private static Method m10;
    private static Method m6;
    private static Method m4;
    private static Method m9;

    public aaaaa(InvocationHandler var1) throws  {super(var1);
    }

    public final boolean equals(Object var1) throws  {
        try {return (Boolean)super.h.invoke(this, m1, new Object[]{var1});
        } catch (RuntimeException | Error var3) {throw var3;} catch (Throwable var4) {throw new UndeclaredThrowableException(var4);
        }
    }

    public final InvocationHandler getInvocationHandler(Object var1) throws IllegalArgumentException {
        try {return (InvocationHandler)super.h.invoke(this, m8, new Object[]{var1});
        } catch (RuntimeException | Error var3) {throw var3;} catch (Throwable var4) {throw new UndeclaredThrowableException(var4);
        }
    }

    public final String toString() throws  {
        try {return (String)super.h.invoke(this, m2, (Object[])null);
        } catch (RuntimeException | Error var2) {throw var2;} catch (Throwable var3) {throw new UndeclaredThrowableException(var3);
        }
    }

    public final Class getProxyClass(ClassLoader var1, Class[] var2) throws IllegalArgumentException {
        try {return (Class)super.h.invoke(this, m7, new Object[]{var1, var2});
        } catch (RuntimeException | Error var4) {throw var4;} catch (Throwable var5) {throw new UndeclaredThrowableException(var5);
        }
    }

    public final Class getClass() throws  {
        try {return (Class)super.h.invoke(this, m12, (Object[])null);
        } catch (RuntimeException | Error var2) {throw var2;} catch (Throwable var3) {throw new UndeclaredThrowableException(var3);
        }
    }

    public final void notifyAll() throws  {
        try {super.h.invoke(this, m14, (Object[])null);
        } catch (RuntimeException | Error var2) {throw var2;} catch (Throwable var3) {throw new UndeclaredThrowableException(var3);
        }
    }

    public final int hashCode() throws  {
        try {return (Integer)super.h.invoke(this, m0, (Object[])null);
        } catch (RuntimeException | Error var2) {throw var2;} catch (Throwable var3) {throw new UndeclaredThrowableException(var3);
        }
    }

    public final void wait() throws InterruptedException {
        try {super.h.invoke(this, m11, (Object[])null);
        } catch (RuntimeException | InterruptedException | Error var2) {throw var2;} catch (Throwable var3) {throw new UndeclaredThrowableException(var3);
        }
    }

    public final void sayHello() throws  {
        try {super.h.invoke(this, m3, (Object[])null);
        } catch (RuntimeException | Error var2) {throw var2;} catch (Throwable var3) {throw new UndeclaredThrowableException(var3);
        }
    }

    public final void notify() throws  {
        try {super.h.invoke(this, m13, (Object[])null);
        } catch (RuntimeException | Error var2) {throw var2;} catch (Throwable var3) {throw new UndeclaredThrowableException(var3);
        }
    }

    public final Object newProxyInstance(ClassLoader var1, Class[] var2, InvocationHandler var3) throws IllegalArgumentException {
        try {return (Object)super.h.invoke(this, m5, new Object[]{var1, var2, var3});
        } catch (RuntimeException | Error var5) {throw var5;} catch (Throwable var6) {throw new UndeclaredThrowableException(var6);
        }
    }

    public final void wait(long var1) throws InterruptedException {
        try {super.h.invoke(this, m10, new Object[]{var1});
        } catch (RuntimeException | InterruptedException | Error var4) {throw var4;} catch (Throwable var5) {throw new UndeclaredThrowableException(var5);
        }
    }

    public final boolean isProxyClass(Class var1) throws  {
        try {return (Boolean)super.h.invoke(this, m6, new Object[]{var1});
        } catch (RuntimeException | Error var3) {throw var3;} catch (Throwable var4) {throw new UndeclaredThrowableException(var4);
        }
    }

    public final void sayBye() throws  {
        try {super.h.invoke(this, m4, (Object[])null);
        } catch (RuntimeException | Error var2) {throw var2;} catch (Throwable var3) {throw new UndeclaredThrowableException(var3);
        }
    }

    public final void wait(long var1, int var3) throws InterruptedException {
        try {super.h.invoke(this, m9, new Object[]{var1, var3});
        } catch (RuntimeException | InterruptedException | Error var5) {throw var5;} catch (Throwable var6) {throw new UndeclaredThrowableException(var6);
        }
    }

    static {
        try {m1 = Class.forName("java.lang.Object").getMethod("equals", Class.forName("java.lang.Object"));
            m8 = Class.forName("com.sun.proxy.$Proxy0").getMethod("getInvocationHandler", Class.forName("java.lang.Object"));
            m2 = Class.forName("java.lang.Object").getMethod("toString");
            m7 = Class.forName("com.sun.proxy.$Proxy0").getMethod("getProxyClass", Class.forName("java.lang.ClassLoader"), Class.forName("[Ljava.lang.Class;"));
            m12 = Class.forName("com.sun.proxy.$Proxy0").getMethod("getClass");
            m14 = Class.forName("com.sun.proxy.$Proxy0").getMethod("notifyAll");
            m0 = Class.forName("java.lang.Object").getMethod("hashCode");
            m11 = Class.forName("com.sun.proxy.$Proxy0").getMethod("wait");
            m3 = Class.forName("com.sun.proxy.$Proxy0").getMethod("sayHello");
            m13 = Class.forName("com.sun.proxy.$Proxy0").getMethod("notify");
            m5 = Class.forName("com.sun.proxy.$Proxy0").getMethod("newProxyInstance", Class.forName("java.lang.ClassLoader"), Class.forName("[Ljava.lang.Class;"), Class.forName("java.lang.reflect.InvocationHandler"));
            m10 = Class.forName("com.sun.proxy.$Proxy0").getMethod("wait", Long.TYPE);
            m6 = Class.forName("com.sun.proxy.$Proxy0").getMethod("isProxyClass", Class.forName("java.lang.Class"));
            m4 = Class.forName("com.sun.proxy.$Proxy0").getMethod("sayBye");
            m9 = Class.forName("com.sun.proxy.$Proxy0").getMethod("wait", Long.TYPE, Integer.TYPE);
        } catch (NoSuchMethodException var2) {throw new NoSuchMethodError(var2.getMessage());
        } catch (ClassNotFoundException var3) {throw new NoClassDefFoundError(var3.getMessage());
        }
    }
}

CGLIB

测试代码

public class CglibOrigin {public void sayHello(){System.out.println("hello world");
    }
}
public class CglibCallBack implements MethodInterceptor {

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {System.out.println("调用前");
        Object result = methodProxy.invokeSuper(o, objects);
        System.out.println("调用后"+result);
        return result;
    }
}


public class Demo {public static void main(String[] args) {Enhancer enhancer =new Enhancer();
        enhancer.setSuperclass(CglibOrigin.class);
        enhancer.setCallback(new CglibCallBack());
        CglibOrigin cglibTestOrigin=(CglibOrigin)enhancer.create();
        cglibTestOrigin.sayHello();

        byte[] bytes = ProxyGenerator.generateProxyClass("aaaa", new Class[]{cglibTestOrigin.getClass()});
        try{FileOutputStream fos =new FileOutputStream(new File("aaaa.class"));
            fos.write(bytes);
            fos.flush();}catch (Exception e){e.printStackTrace();
        }

    }
}

源码剖析

从测试的代码能够看进去,cglib 能够不依赖接口,间接能够生成类的代理类,所有的过程都封装在 create 办法中。
所以咱们着重钻研下 create 办法就行了
create 办法最终会调用一个 Future 来执行创立。这里就省略下两头步骤了,间接跳到创立的中央。

最终 Enhancer 会调用父类进行创立。所以间接跳过两头步骤了,间接钻研 net.sf.cglib.core.AbstractClassGenerator#generate 了。看下方代码,能够看到和 JDK 动静代理差不多,学生成字节码,而后再通过反射创立。咱们先去看下,生成字节码

protected Class generate(ClassLoaderData data) {
    Class gen;
    Object save = CURRENT.get();
    CURRENT.set(this);
    try {ClassLoader classLoader = data.getClassLoader();
        if (classLoader == null) {
            throw new IllegalStateException("ClassLoader is null while trying to define class" +
                    getClassName() + ". It seems that the loader has been expired from a weak reference somehow." +
                    "Please file an issue at cglib's issue tracker.");
        }
        synchronized (classLoader) {String name = generateClassName(data.getUniqueNamePredicate());              
          data.reserveName(name);
          this.setClassName(name);
        }
        if (attemptLoad) {
            try {gen = classLoader.loadClass(getClassName());
                return gen;
            } catch (ClassNotFoundException e) {// ignore}
        }
        byte[] b = strategy.generate(this);
        // 上面这几行是我本人加的不便察看
        String className = ClassNameReader.getClassName(new ClassReader(b));
        try{FileOutputStream fos =new FileOutputStream(new File(integer.incrementAndGet()+".class"));

            fos.write(b);
            fos.flush();}catch (Exception e){e.printStackTrace();
        }
        ProtectionDomain protectionDomain = getProtectionDomain();
        synchronized (classLoader) { // just in case
            if (protectionDomain == null) {gen = ReflectUtils.defineClass(className, b, classLoader);
            } else {gen = ReflectUtils.defineClass(className, b, classLoader, protectionDomain);
            }
        }
        return gen;
    } catch (RuntimeException e) {throw e;} catch (Error e) {throw e;} catch (Exception e) {throw new CodeGenerationException(e);
    } finally {CURRENT.set(save);
    }
}

它的实现是通过 net.sf.cglib.proxy.Enhancer#generateClass 办法,这里就涉及到我的常识盲区了,CGLIB 竟然手写字节码,感兴趣的能够本人钻研下哈(我还要头发)

public void generateClass(ClassVisitor v) throws Exception {Class sc = (superclass == null) ? Object.class : superclass;

    if (TypeUtils.isFinal(sc.getModifiers()))
        throw new IllegalArgumentException("Cannot subclass final class" + sc.getName());
    List constructors = new ArrayList(Arrays.asList(sc.getDeclaredConstructors()));
    filterConstructors(sc, constructors);

    // Order is very important: must add superclass, then
    // its superclass chain, then each interface and
    // its superinterfaces.
    List actualMethods = new ArrayList();
    List interfaceMethods = new ArrayList();
    final Set forcePublic = new HashSet();
    getMethods(sc, interfaces, actualMethods, interfaceMethods, forcePublic);

    List methods = CollectionUtils.transform(actualMethods, new Transformer() {public Object transform(Object value) {Method method = (Method)value;
            int modifiers = Constants.ACC_FINAL
                | (method.getModifiers()
                   & ~Constants.ACC_ABSTRACT
                   & ~Constants.ACC_NATIVE
                   & ~Constants.ACC_SYNCHRONIZED);
            if (forcePublic.contains(MethodWrapper.create(method))) {modifiers = (modifiers & ~Constants.ACC_PROTECTED) | Constants.ACC_PUBLIC;
            }
            return ReflectUtils.getMethodInfo(method, modifiers);
        }
    });

    ClassEmitter e = new ClassEmitter(v);
    if (currentData == null) {
    e.begin_class(Constants.V1_8,
                  Constants.ACC_PUBLIC,
                  getClassName(),
                  Type.getType(sc),
                  (useFactory ?
                   TypeUtils.add(TypeUtils.getTypes(interfaces), FACTORY) :
                   TypeUtils.getTypes(interfaces)),
                  Constants.SOURCE_FILE);
    } else {
        e.begin_class(Constants.V1_8,
                Constants.ACC_PUBLIC,
                getClassName(),
                null,
                new Type[]{FACTORY},
                Constants.SOURCE_FILE);
    }
    List constructorInfo = CollectionUtils.transform(constructors, MethodInfoTransformer.getInstance());

    e.declare_field(Constants.ACC_PRIVATE, BOUND_FIELD, Type.BOOLEAN_TYPE, null);
    e.declare_field(Constants.ACC_PUBLIC | Constants.ACC_STATIC, FACTORY_DATA_FIELD, OBJECT_TYPE, null);
    if (!interceptDuringConstruction) {e.declare_field(Constants.ACC_PRIVATE, CONSTRUCTED_FIELD, Type.BOOLEAN_TYPE, null);
    }
    e.declare_field(Constants.PRIVATE_FINAL_STATIC, THREAD_CALLBACKS_FIELD, THREAD_LOCAL, null);
    e.declare_field(Constants.PRIVATE_FINAL_STATIC, STATIC_CALLBACKS_FIELD, CALLBACK_ARRAY, null);
    if (serialVersionUID != null) {e.declare_field(Constants.PRIVATE_FINAL_STATIC, Constants.SUID_FIELD_NAME, Type.LONG_TYPE, serialVersionUID);
    }

    for (int i = 0; i < callbackTypes.length; i++) {e.declare_field(Constants.ACC_PRIVATE, getCallbackField(i), callbackTypes[i], null);
    }
    // This is declared private to avoid "public field" pollution
    e.declare_field(Constants.ACC_PRIVATE | Constants.ACC_STATIC, CALLBACK_FILTER_FIELD, OBJECT_TYPE, null);

    if (currentData == null) {emitMethods(e, methods, actualMethods);
        emitConstructors(e, constructorInfo);
    } else {emitDefaultConstructor(e);
    }
    emitSetThreadCallbacks(e);
    emitSetStaticCallbacks(e);
    emitBindCallbacks(e);

    if (useFactory || currentData != null) {int[] keys = getCallbackKeys();
        emitNewInstanceCallbacks(e);
        emitNewInstanceCallback(e);
        emitNewInstanceMultiarg(e, constructorInfo);
        emitGetCallback(e, keys);
        emitSetCallback(e, keys);
        emitGetCallbacks(e);
        emitSetCallbacks(e);
    }

    e.end_class();}

// 转换成字节数组
public byte[] toByteArray() {return (byte[]) java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {public Object run() {byte[] b = ((ClassWriter) DebuggingClassWriter.super.cv).toByteArray();
            if (debugLocation != null) {String dirs = className.replace('.', File.separatorChar);
                try {new File(debugLocation + File.separatorChar + dirs).getParentFile().mkdirs();
                    
                    File file = new File(new File(debugLocation), dirs + ".class");
                    OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
                    try {out.write(b);
                    } finally {out.close();
                    }
                    
                    if (traceCtor != null) {file = new File(new File(debugLocation), dirs + ".asm");
                        out = new BufferedOutputStream(new FileOutputStream(file));
                        try {ClassReader cr = new ClassReader(b);
                            PrintWriter pw = new PrintWriter(new OutputStreamWriter(out));
                            ClassVisitor tcv = (ClassVisitor)traceCtor.newInstance(new Object[]{null, pw});
                            cr.accept(tcv, 0);
                            pw.flush();} finally {out.close();
                        }
                    }
                } catch (Exception e) {throw new CodeGenerationException(e);
                }
            }
            return b;
         }  
        });
        
    }

这里就间接看下生成的字节码了,也和 jdk 动静代理差不多,都是通过 InvocationHandler 实现的。前面的感觉也没必要纠结了,毕竟都手写字节码了,想怎么写怎么写,具体实现过程也不重要了。就看到这吧~~~

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;
import net.sf.cglib.core.Signature;
import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.MethodProxy;
import net.sf.cglib.self.CglibOrigin..EnhancerByCGLIB..e61bfbb8;

public final class aaaa extends Proxy implements e61bfbb8 {
    private static Method m1;
    private static Method m11;
    private static Method m2;
    private static Method m5;
    private static Method m17;
    private static Method m19;
    private static Method m0;
    private static Method m16;
    private static Method m8;
    private static Method m9;
    private static Method m4;
    private static Method m3;
    private static Method m18;
    private static Method m10;
    private static Method m12;
    private static Method m15;
    private static Method m14;
    private static Method m6;
    private static Method m7;
    private static Method m13;

    public aaaa(InvocationHandler var1) throws  {super(var1);
    }

    public final boolean equals(Object var1) throws  {
        try {return (Boolean)super.h.invoke(this, m1, new Object[]{var1});
        } catch (RuntimeException | Error var3) {throw var3;} catch (Throwable var4) {throw new UndeclaredThrowableException(var4);
        }
    }

    public final Object newInstance(Callback[] var1) throws  {
        try {return (Object)super.h.invoke(this, m11, new Object[]{var1});
        } catch (RuntimeException | Error var3) {throw var3;} catch (Throwable var4) {throw new UndeclaredThrowableException(var4);
        }
    }

    public final String toString() throws  {
        try {return (String)super.h.invoke(this, m2, (Object[])null);
        } catch (RuntimeException | Error var2) {throw var2;} catch (Throwable var3) {throw new UndeclaredThrowableException(var3);
        }
    }

    public final Callback getCallback(int var1) throws  {
        try {return (Callback)super.h.invoke(this, m5, new Object[]{var1});
        } catch (RuntimeException | Error var3) {throw var3;} catch (Throwable var4) {throw new UndeclaredThrowableException(var4);
        }
    }

    public final Class getClass() throws  {
        try {return (Class)super.h.invoke(this, m17, (Object[])null);
        } catch (RuntimeException | Error var2) {throw var2;} catch (Throwable var3) {throw new UndeclaredThrowableException(var3);
        }
    }

    public final void notifyAll() throws  {
        try {super.h.invoke(this, m19, (Object[])null);
        } catch (RuntimeException | Error var2) {throw var2;} catch (Throwable var3) {throw new UndeclaredThrowableException(var3);
        }
    }

    public final int hashCode() throws  {
        try {return (Integer)super.h.invoke(this, m0, (Object[])null);
        } catch (RuntimeException | Error var2) {throw var2;} catch (Throwable var3) {throw new UndeclaredThrowableException(var3);
        }
    }

    public final void wait() throws InterruptedException {
        try {super.h.invoke(this, m16, (Object[])null);
        } catch (RuntimeException | InterruptedException | Error var2) {throw var2;} catch (Throwable var3) {throw new UndeclaredThrowableException(var3);
        }
    }

    public final void sayHello() throws  {
        try {super.h.invoke(this, m8, (Object[])null);
        } catch (RuntimeException | Error var2) {throw var2;} catch (Throwable var3) {throw new UndeclaredThrowableException(var3);
        }
    }

    public final void CGLIB$SET_STATIC_CALLBACKS(Callback[] var1) throws  {
        try {super.h.invoke(this, m9, new Object[]{var1});
        } catch (RuntimeException | Error var3) {throw var3;} catch (Throwable var4) {throw new UndeclaredThrowableException(var4);
        }
    }

    public final void setCallbacks(Callback[] var1) throws  {
        try {super.h.invoke(this, m4, new Object[]{var1});
        } catch (RuntimeException | Error var3) {throw var3;} catch (Throwable var4) {throw new UndeclaredThrowableException(var4);
        }
    }

    public final void setCallback(int var1, Callback var2) throws  {
        try {super.h.invoke(this, m3, new Object[]{var1, var2});
        } catch (RuntimeException | Error var4) {throw var4;} catch (Throwable var5) {throw new UndeclaredThrowableException(var5);
        }
    }

    public final void notify() throws  {
        try {super.h.invoke(this, m18, (Object[])null);
        } catch (RuntimeException | Error var2) {throw var2;} catch (Throwable var3) {throw new UndeclaredThrowableException(var3);
        }
    }

    public final void CGLIB$SET_THREAD_CALLBACKS(Callback[] var1) throws  {
        try {super.h.invoke(this, m10, new Object[]{var1});
        } catch (RuntimeException | Error var3) {throw var3;} catch (Throwable var4) {throw new UndeclaredThrowableException(var4);
        }
    }

    public final Object newInstance(Class[] var1, Object[] var2, Callback[] var3) throws  {
        try {return (Object)super.h.invoke(this, m12, new Object[]{var1, var2, var3});
        } catch (RuntimeException | Error var5) {throw var5;} catch (Throwable var6) {throw new UndeclaredThrowableException(var6);
        }
    }

    public final void wait(long var1) throws InterruptedException {
        try {super.h.invoke(this, m15, new Object[]{var1});
        } catch (RuntimeException | InterruptedException | Error var4) {throw var4;} catch (Throwable var5) {throw new UndeclaredThrowableException(var5);
        }
    }

    public final void wait(long var1, int var3) throws InterruptedException {
        try {super.h.invoke(this, m14, new Object[]{var1, var3});
        } catch (RuntimeException | InterruptedException | Error var5) {throw var5;} catch (Throwable var6) {throw new UndeclaredThrowableException(var6);
        }
    }

    public final Callback[] getCallbacks() throws  {
        try {return (Callback[])super.h.invoke(this, m6, (Object[])null);
        } catch (RuntimeException | Error var2) {throw var2;} catch (Throwable var3) {throw new UndeclaredThrowableException(var3);
        }
    }

    public final MethodProxy CGLIB$findMethodProxy(Signature var1) throws  {
        try {return (MethodProxy)super.h.invoke(this, m7, new Object[]{var1});
        } catch (RuntimeException | Error var3) {throw var3;} catch (Throwable var4) {throw new UndeclaredThrowableException(var4);
        }
    }

    public final Object newInstance(Callback var1) throws  {
        try {return (Object)super.h.invoke(this, m13, new Object[]{var1});
        } catch (RuntimeException | Error var3) {throw var3;} catch (Throwable var4) {throw new UndeclaredThrowableException(var4);
        }
    }

    static {
        try {m1 = Class.forName("java.lang.Object").getMethod("equals", Class.forName("java.lang.Object"));
            m11 = Class.forName("net.sf.cglib.self.CglibOrigin$$EnhancerByCGLIB$$e61bfbb8").getMethod("newInstance", Class.forName("[Lnet.sf.cglib.proxy.Callback;"));
            m2 = Class.forName("java.lang.Object").getMethod("toString");
            m5 = Class.forName("net.sf.cglib.self.CglibOrigin$$EnhancerByCGLIB$$e61bfbb8").getMethod("getCallback", Integer.TYPE);
            m17 = Class.forName("net.sf.cglib.self.CglibOrigin$$EnhancerByCGLIB$$e61bfbb8").getMethod("getClass");
            m19 = Class.forName("net.sf.cglib.self.CglibOrigin$$EnhancerByCGLIB$$e61bfbb8").getMethod("notifyAll");
            m0 = Class.forName("java.lang.Object").getMethod("hashCode");
            m16 = Class.forName("net.sf.cglib.self.CglibOrigin$$EnhancerByCGLIB$$e61bfbb8").getMethod("wait");
            m8 = Class.forName("net.sf.cglib.self.CglibOrigin$$EnhancerByCGLIB$$e61bfbb8").getMethod("sayHello");
            m9 = Class.forName("net.sf.cglib.self.CglibOrigin$$EnhancerByCGLIB$$e61bfbb8").getMethod("CGLIB$SET_STATIC_CALLBACKS", Class.forName("[Lnet.sf.cglib.proxy.Callback;"));
            m4 = Class.forName("net.sf.cglib.self.CglibOrigin$$EnhancerByCGLIB$$e61bfbb8").getMethod("setCallbacks", Class.forName("[Lnet.sf.cglib.proxy.Callback;"));
            m3 = Class.forName("net.sf.cglib.self.CglibOrigin$$EnhancerByCGLIB$$e61bfbb8").getMethod("setCallback", Integer.TYPE, Class.forName("net.sf.cglib.proxy.Callback"));
            m18 = Class.forName("net.sf.cglib.self.CglibOrigin$$EnhancerByCGLIB$$e61bfbb8").getMethod("notify");
            m10 = Class.forName("net.sf.cglib.self.CglibOrigin$$EnhancerByCGLIB$$e61bfbb8").getMethod("CGLIB$SET_THREAD_CALLBACKS", Class.forName("[Lnet.sf.cglib.proxy.Callback;"));
            m12 = Class.forName("net.sf.cglib.self.CglibOrigin$$EnhancerByCGLIB$$e61bfbb8").getMethod("newInstance", Class.forName("[Ljava.lang.Class;"), Class.forName("[Ljava.lang.Object;"), Class.forName("[Lnet.sf.cglib.proxy.Callback;"));
            m15 = Class.forName("net.sf.cglib.self.CglibOrigin$$EnhancerByCGLIB$$e61bfbb8").getMethod("wait", Long.TYPE);
            m14 = Class.forName("net.sf.cglib.self.CglibOrigin$$EnhancerByCGLIB$$e61bfbb8").getMethod("wait", Long.TYPE, Integer.TYPE);
            m6 = Class.forName("net.sf.cglib.self.CglibOrigin$$EnhancerByCGLIB$$e61bfbb8").getMethod("getCallbacks");
            m7 = Class.forName("net.sf.cglib.self.CglibOrigin$$EnhancerByCGLIB$$e61bfbb8").getMethod("CGLIB$findMethodProxy", Class.forName("net.sf.cglib.core.Signature"));
            m13 = Class.forName("net.sf.cglib.self.CglibOrigin$$EnhancerByCGLIB$$e61bfbb8").getMethod("newInstance", Class.forName("net.sf.cglib.proxy.Callback"));
        } catch (NoSuchMethodException var2) {throw new NoSuchMethodError(var2.getMessage());
        } catch (ClassNotFoundException var3) {throw new NoClassDefFoundError(var3.getMessage());
        }
    }
}

总结

  • JDK 动静代理: 须要接口创立代理对象,实质是由底层,创立字节码,间接生成对象,理论调用的就是本人实现的InvocationHandler
  • CGLIB: 无需接口,依据标准本人生成字节码。

    年前最初几天,比拟有空,故此整顿下 Spring 源码,马上要回去过年了,前面更新频率就不会这么快了,aop 局部还是会更新完的。前面可能更新工夫就齐全看情绪 + 工夫了,那群卷王是怎么做到日更,甚至一天好几更的,我反正写一篇好几个小时就没了,切实肝不动了。感兴趣的同学能够分割我,一起探讨技术哈(创作不易,给个赞呗)

退出移动版