jdk动态代理类的源码
import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.lang.reflect.UndeclaredThrowableException; public final class $Proxy0 extends Proxy implements Manager { private static Method m1; private static Method m0; private static Method m3; private static Method m2; static { try { m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[] { Class.forName("java.lang.Object") }); m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]); m3 = Class.forName("com.ml.test.Manager").getMethod("modify", new Class[0]); m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]); } catch (NoSuchMethodException nosuchmethodexception) { throw new NoSuchMethodError(nosuchmethodexception.getMessage()); } catch (ClassNotFoundException classnotfoundexception) { throw new NoClassDefFoundError(classnotfoundexception.getMessage()); } } public $Proxy0(InvocationHandler invocationhandler) { super(invocationhandler); } @Override public final boolean equals(Object obj) { try { return ((Boolean) super.h.invoke(this, m1, new Object[] { obj })) .booleanValue(); } catch (Throwable throwable) { throw new UndeclaredThrowableException(throwable); } } @Override public final int hashCode() { try { return ((Integer) super.h.invoke(this, m0, null)).intValue(); } catch (Throwable throwable) { throw new UndeclaredThrowableException(throwable); } } public final void modify() { try { super.h.invoke(this, m3, null); return; } catch (Error e) { } catch (Throwable throwable) { throw new UndeclaredThrowableException(throwable); } } @Override public final String toString() { try { return (String) super.h.invoke(this, m2, null); } catch (Throwable throwable) { throw new UndeclaredThrowableException(throwable); } } }
cglib 动态代理产生的源码
package com.zhang.shine.cache; import java.lang.reflect.Method; import java.util.Map; import net.sf.cglib.core.Signature; import net.sf.cglib.proxy.Callback; import net.sf.cglib.proxy.Dispatcher; import net.sf.cglib.proxy.Factory; import net.sf.cglib.proxy.MethodInterceptor; import net.sf.cglib.proxy.MethodProxy; import net.sf.cglib.proxy.NoOp; import org.springframework.aop.SpringProxy; import org.springframework.aop.framework.Advised; public class Sev$$EnhancerByCGLIB$$e8033e73 extends Sev implements SpringProxy, Advised, Factory { private boolean CGLIB$BOUND; private boolean CGLIB$CONSTRUCTED; private static final ThreadLocal CGLIB$THREAD_CALLBACKS; private static final Callback[] CGLIB$STATIC_CALLBACKS; private MethodInterceptor CGLIB$CALLBACK_0; private MethodInterceptor CGLIB$CALLBACK_1; private NoOp CGLIB$CALLBACK_2; private Dispatcher CGLIB$CALLBACK_3; private Dispatcher CGLIB$CALLBACK_4; private MethodInterceptor CGLIB$CALLBACK_5; private MethodInterceptor CGLIB$CALLBACK_6; private static final Method CGLIB$getSort$0$Method; private static final MethodProxy CGLIB$getSort$0$Proxy; private static final Object[] CGLIB$emptyArgs; private static final Method CGLIB$getSort$1$Method; private static final MethodProxy CGLIB$getSort$1$Proxy; private static final Method CGLIB$hashCode$3$Method; private static final MethodProxy CGLIB$hashCode$3$Proxy; private static final Method CGLIB$clone$4$Method; private static final MethodProxy CGLIB$clone$4$Proxy; private static final Method CGLIB$equals$5$Method; private static final MethodProxy CGLIB$equals$5$Proxy; private static final Method CGLIB$toString$6$Method; private static final MethodProxy CGLIB$toString$6$Proxy; static void CGLIB$STATICHOOK1() { CGLIB$THREAD_CALLBACKS = new ThreadLocal(); Class localClass; CGLIB$emptyArgs = new Object[0]; ClassLoader tmp27_17 = (localClass = Class.forName("com.zhang.shine.cache.Sev$$EnhancerByCGLIB$$e8033e73")).getClassLoader(); CGLIB$getSort$0$Proxy = MethodProxy.create(tmp27_17, (e8033e73.CGLIB$getSort$0$Method = Class.forName("com.zhang.shine.cache.Sev").getDeclaredMethod("getSort", new Class[] { Integer.TYPE, Integer.TYPE })).getDeclaringClass(), localClass, "(II)Ljava/util/Map;", "getSort", "CGLIB$getSort$0"); ClassLoader tmp74_27 = tmp27_17; CGLIB$getSort$1$Proxy = MethodProxy.create(tmp74_27, (e8033e73.CGLIB$getSort$1$Method = Class.forName("com.zhang.shine.cache.Sev").getDeclaredMethod("getSort", new Class[0])).getDeclaringClass(), localClass, "()V", "getSort", "CGLIB$getSort$1"); ClassLoader tmp109_74 = tmp74_27; CGLIB$hashCode$3$Proxy = MethodProxy.create(tmp109_74, (e8033e73.CGLIB$hashCode$3$Method = Class.forName("java.lang.Object").getDeclaredMethod("hashCode", new Class[0])).getDeclaringClass(), localClass, "()I", "hashCode", "CGLIB$hashCode$3"); ClassLoader tmp144_109 = tmp109_74; CGLIB$clone$4$Proxy = MethodProxy.create(tmp144_109, (e8033e73.CGLIB$clone$4$Method = Class.forName("java.lang.Object").getDeclaredMethod("clone", new Class[0])).getDeclaringClass(), localClass, "()Ljava/lang/Object;", "clone", "CGLIB$clone$4"); ClassLoader tmp179_144 = tmp144_109; CGLIB$equals$5$Proxy = MethodProxy.create(tmp179_144, (e8033e73.CGLIB$equals$5$Method = Class.forName("java.lang.Object").getDeclaredMethod("equals", new Class[] { Class.forName("java.lang.Object") })).getDeclaringClass(), localClass, "(Ljava/lang/Object;)Z", "equals", "CGLIB$equals$5"); CGLIB$toString$6$Proxy = MethodProxy.create(tmp179_144, (e8033e73.CGLIB$toString$6$Method = Class.forName("java.lang.Object").getDeclaredMethod("toString", new Class[0])).getDeclaringClass(), localClass, "()Ljava/lang/String;", "toString", "CGLIB$toString$6"); return; } final Map CGLIB$getSort$0(int paramInt1, int paramInt2) { return super.getSort(paramInt1, paramInt2); }
它们底层都是通过动态产生字节码,动态编译而运行的。