Spring jdk动态代理源码 as cglib代理源码

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);
  }

 

 

 它们底层都是通过动态产生字节码,动态编译而运行的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值