「Spring-Aop」源码分析三: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() {
                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进来,可以

「Spring-Aop」源码分析三:JDK动态代理&Cglib_第1张图片

到此分析似乎陷入僵局,但是还记得之前把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来执行创建。这里就省略下中间步骤了,直接跳到创建的地方。

「Spring-Aop」源码分析三:JDK动态代理&Cglib_第2张图片

最终Enhancer会调用父类进行创建。所以直接跳过中间步骤了,直接研究net.sf.cglib.core.AbstractClassGenerator#generate了。看下方代码,可以看到和JDK动态代理差不多,先生成字节码,然后再通过反射创建。我们先去看下,生成字节码
「Spring-Aop」源码分析三:JDK动态代理&Cglib_第3张图片

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部分还是会更新完的。后面可能更新时间就完全看心情+时间了,那群卷王是怎么做到日更,甚至一天好几更的,我反正写一篇好几个小时就没了,实在肝不动了。感兴趣的同学可以联系我,一起讨论技术哈(创作不易,给个赞呗)

你可能感兴趣的:(动态代理源码分析)