Java设计模式--静态代理与动态代理

矫情吗;* 2023-07-05 11:46 31阅读 0赞

原文网址:Java设计模式—静态代理与动态代理_IT利刃出鞘的博客-CSDN博客

其他网址

JAVA动态代理 - 简书

Spring AOP—综述_feiying0canglang的博客-CSDN博客

简介

其他网址

JDK和CGLIB动态代理区别_java_yhl_jxy的博客-CSDN博客

代理模式

为某个对象提供一个代理,以控制对这个对象的访问。 代理类和委托类有共同的父类或父接口,这样在任何使用委托类对象的地方都可以用代理对象替代。代理类负责请求的预处理、过滤、将请求分派给委托类处理、以及委托类执行完请求后的后续处理。

20180918092105430

从图中可以看出,代理接口(Subject)、代理类(ProxySubject)、委托类(RealSubject)形成一个“品”字结构。

根据代理类的生成时间不同可以将代理分为静态代理和动态代理两种。

静态代理与动态代理区别

其他网址

动态代理与静态代理区别_java_三丰的专栏-CSDN博客 静态代理和动态代理的区别 - bowang - 博客园






















静态代理 动态代理
代理类字节码创建的时间

        程序运行前就已存在代理类的字节码文件,代理类和委托类的关系在运行前就确定了。 

        程序员创建或工具生成代理类的源码,再编译代理类。

        不存在代理类的字节码文件。动态代理类的源码是在程序运行期间由JVM根据反射等机制动态的生成的,代理类和委托类的关系是在程序运行时确定。

动态代理占优势的项
接口有多个方法的处理

        如果接口增加一个方法,除了所有实现类需要实现这个方法外,所有代理类也需要实现此方法

        接口中声明的所有方法都被转移到调用处理器一个集中的方法中处理,接口方法数量比较多的时候,不需要像静态代理那样每一个方法进行中转

        在本示例中看不出来,因为invoke方法体内嵌入了具体的外围业务(记录任务处理前后时间并计算时间差),实际中可以类似Spring AOP那样配置外围业务。

JDK动态代理与CGLIB动态代理区别





























JDK动态代理

CGLIB动态代理

接口是否需实现

只能代理实现了接口的类。

可以代理没有实现接口的类。

原理

继承自Proxy,而Proxy中有InvocationHandler的实现类的引用。

调用被代理的类的方法时,会调用父类(Proxy)的InvocationHandler的invoke方法。

对指定的目标类生成一个子类,并覆盖其中方法实现增强。

是否支持final class

支持。

不支持, 因为CGLIB是生成子类来实现AOP。

执行效率

少量调用时:JDK代理效率高于CGLIB

大量调用时:jdk6比CGLIB效率低一点。JDK1.7与1.8效率高于CGLIB(1W次调用,高20%)

JDK1.6之前,效率比JDK高。

静态代理

时间统计示例

代码

  1. package org.example.a;
  2. //代理接口
  3. interface ExecuteTime {
  4. public void dealTask(String taskName);
  5. }
  6. //被代理类
  7. class ExecuteTimeImpl implements ExecuteTime {
  8. //这里打印出任务名,并休眠500ms模拟任务执行了很长时间
  9. @Override
  10. public void dealTask(String taskName) {
  11. System.out.println("Task is running: "+taskName);
  12. try {
  13. Thread.sleep(500);
  14. } catch (InterruptedException e) {
  15. e.printStackTrace();
  16. }
  17. }
  18. }
  19. //代理类
  20. class ExecuteTimeProxy implements ExecuteTime {
  21. private ExecuteTime delegate = null;
  22. public ExecuteTimeProxy(ExecuteTimeImpl ExecuteTimeImpl) {
  23. this.delegate = ExecuteTimeImpl;
  24. }
  25. @Override
  26. public void dealTask(String taskName) {
  27. long startTime = System.currentTimeMillis();
  28. delegate.dealTask(taskName);
  29. long endTime = System.currentTimeMillis();
  30. System.out.println("Elapsed time: "+(endTime - startTime)+" ms");
  31. }
  32. }
  33. //工厂方法获得代理对象
  34. class StatiProxyFactory {
  35. //对客户类来说,其并不知道返回的是代理类对象还是委托类对象。
  36. public static ExecuteTime getInstance(){
  37. return new ExecuteTimeProxy(new ExecuteTimeImpl());
  38. }
  39. }
  40. public class Demo{
  41. public static void main(String[] args) {
  42. ExecuteTime proxy = StatiProxyFactory.getInstance();
  43. proxy.dealTask("TestTask");
  44. }
  45. }

运行结果

  1. Task is running: TestTask
  2. Elapsed time: 501 ms

动态代理(JDK)

动态代理具体步骤

  1. 通过实现 InvocationHandler 接口创建自己的调用处理器;
  2. 通过为 Proxy 类指定 ClassLoader 对象和一组 interface 来创建动态代理类;
  3. 通过反射机制获得动态代理类的构造函数,其唯一参数类型是调用处理器接口类型;
  4. 通过构造函数创建动态代理类实例,构造时调用处理器对象作为参数被传入。

时间统计示例

代码

  1. package org.example.a;
  2. import java.lang.reflect.InvocationHandler;
  3. import java.lang.reflect.Method;
  4. import java.lang.reflect.Proxy;
  5. //代理接口
  6. interface ExecutionTime {
  7. public void dealTask(String taskName);
  8. public void sayHello(String name);
  9. }
  10. //被代理类
  11. class ExecutionTimeImpl implements ExecutionTime {
  12. //这里打印出任务名,并休眠500ms模拟任务执行了很长时间
  13. @Override
  14. public void dealTask(String taskName) {
  15. System.out.println("Task is running: "+taskName);
  16. try {
  17. Thread.sleep(500);
  18. } catch (InterruptedException e) {
  19. e.printStackTrace();
  20. }
  21. }
  22. @Override
  23. public void sayHello(String name) {
  24. System.out.println("hello" + name);
  25. }
  26. }
  27. // 动态代理类对应的调用处理程序类
  28. class ExecutionTimeProxy implements InvocationHandler {
  29. private Object delegate = null;
  30. public ExecutionTimeProxy(ExecutionTime delegate) {
  31. this.delegate = delegate;
  32. }
  33. // proxy:代理类代理的真实代理对象。例如:com.sun.proxy.$Proxy0
  34. @Override
  35. public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  36. long startTime = System.currentTimeMillis();
  37. method.invoke(delegate, args);
  38. long endTime = System.currentTimeMillis();
  39. System.out.println("Elapsed time: "+(endTime - startTime)+" ms");
  40. return null;
  41. }
  42. }
  43. //生成动态代理对象的工厂
  44. class DynamicProxyFactory {
  45. public static ExecutionTime getInstance(){
  46. ExecutionTime delegate = new ExecutionTimeImpl();
  47. InvocationHandler handler = new ExecutionTimeProxy(delegate);
  48. ExecutionTime proxy = (ExecutionTime) Proxy.newProxyInstance(
  49. delegate.getClass().getClassLoader(),
  50. delegate.getClass().getInterfaces(),
  51. handler);
  52. return proxy;
  53. }
  54. }
  55. public class Demo{
  56. public static void main(String[] args) {
  57. System.setProperty("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
  58. ExecutionTime proxy = DynamicProxyFactory.getInstance();
  59. System.out.println(proxy.getClass());
  60. System.out.println("-----------------------------------");
  61. proxy.dealTask("TestTask");
  62. }
  63. }

运行结果

  1. class org.example.a.$Proxy0
  2. -----------------------------------
  3. Task is running: TestTask
  4. Elapsed time: 500 ms

ArrayList示例

  1. package org.example.a;
  2. import java.lang.reflect.InvocationHandler;
  3. import java.lang.reflect.Method;
  4. import java.lang.reflect.Proxy;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. public class Demo {
  8. public static void main(String[] args) {
  9. final ArrayList<String> list = new ArrayList<>();
  10. List<String> listProxy=(List<String>) Proxy.newProxyInstance(
  11. list.getClass().getClassLoader(),
  12. list.getClass().getInterfaces(),
  13. new InvocationHandler() {
  14. @Override
  15. public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  16. return method.invoke(list,args);
  17. }
  18. });
  19. listProxy.add("hello") ;
  20. listProxy.add("world") ;
  21. System.out.println(list);
  22. }
  23. }

运行结果

  1. [hello, world]

原理

生成代理类的源码

其他网址

JDK动态代理为什么不能代理类 - 知乎

查看自动生成的proxy class

默认在运行时程序自动生成proxy class,若想看下自动生成的proxy class有三种方法

  1. 设置参数-Dsun.misc.ProxyGenerator.saveGeneratedFiles=true
  2. 程序里调用:System.setProperty(“sun.misc.ProxyGenerator.saveGeneratedFiles”, “true”);
  3. 在程序中直接调用ProxyGenerator.generateProxyClass()来生成动态代理类,如下所示:

    String name = “$Proxy00”;
    byte[] data = ProxyGenerator.generateProxyClass(“com.sun.proxy.” + name, new Class[] { Hello.class } );
    FileOutputStream out = new FileOutputStream(“/tmp/“ + name + “.class” );
    out.write(data);
    out.close();

以前边的“时间统计示例”为例,在main方法中最前边添加

  1. System.setProperty("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");

执行结果:生成了$Proxy0.class

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ZlaXlpbmcwY2FuZ2xhbmc_size_16_color_FFFFFF_t_70

$Proxy0.class代码如下

  1. //
  2. // Source code recreated from a .class file by IntelliJ IDEA
  3. // (powered by Fernflower decompiler)
  4. //
  5. package org.example.a;
  6. import java.lang.reflect.InvocationHandler;
  7. import java.lang.reflect.Method;
  8. import java.lang.reflect.Proxy;
  9. import java.lang.reflect.UndeclaredThrowableException;
  10. final class $Proxy0 extends Proxy implements ExecutionTime {
  11. private static Method m1;
  12. private static Method m4;
  13. private static Method m2;
  14. private static Method m3;
  15. private static Method m0;
  16. static {
  17. try {
  18. m1 = Class.forName("java.lang.Object").getMethod("equals", Class.forName("java.lang.Object"));
  19. m4 = Class.forName("org.example.a.ExecutionTime").getMethod("sayHello", Class.forName("java.lang.String"));
  20. m2 = Class.forName("java.lang.Object").getMethod("toString");
  21. m3 = Class.forName("org.example.a.ExecutionTime").getMethod("dealTask", Class.forName("java.lang.String"));
  22. m0 = Class.forName("java.lang.Object").getMethod("hashCode");
  23. } catch (NoSuchMethodException var2) {
  24. throw new NoSuchMethodError(var2.getMessage());
  25. } catch (ClassNotFoundException var3) {
  26. throw new NoClassDefFoundError(var3.getMessage());
  27. }
  28. }
  29. public $Proxy0(InvocationHandler var1) throws {
  30. super(var1);
  31. }
  32. public final void dealTask(String var1) throws {
  33. try {
  34. super.h.invoke(this, m3, new Object[]{var1});
  35. } catch (RuntimeException | Error var3) {
  36. throw var3;
  37. } catch (Throwable var4) {
  38. throw new UndeclaredThrowableException(var4);
  39. }
  40. }
  41. public final void sayHello(String var1) throws {
  42. try {
  43. super.h.invoke(this, m4, new Object[]{var1});
  44. } catch (RuntimeException | Error var3) {
  45. throw var3;
  46. } catch (Throwable var4) {
  47. throw new UndeclaredThrowableException(var4);
  48. }
  49. }
  50. public final boolean equals(Object var1) throws {
  51. try {
  52. return (Boolean)super.h.invoke(this, m1, new Object[]{var1});
  53. } catch (RuntimeException | Error var3) {
  54. throw var3;
  55. } catch (Throwable var4) {
  56. throw new UndeclaredThrowableException(var4);
  57. }
  58. }
  59. public final String toString() throws {
  60. try {
  61. return (String)super.h.invoke(this, m2, (Object[])null);
  62. } catch (RuntimeException | Error var2) {
  63. throw var2;
  64. } catch (Throwable var3) {
  65. throw new UndeclaredThrowableException(var3);
  66. }
  67. }
  68. public final int hashCode() throws {
  69. try {
  70. return (Integer)super.h.invoke(this, m0, (Object[])null);
  71. } catch (RuntimeException | Error var2) {
  72. throw var2;
  73. } catch (Throwable var3) {
  74. throw new UndeclaredThrowableException(var3);
  75. }
  76. }
  77. }

JDK动态代理只能代理接口

类Proxy的作用是保存自定义的InvocationHandler,便于在方法代理时执行自定义InvocationHandler的逻辑。由于$Proxy0已经继承了类Proxy,所以不能再extends一个类了,所以只能implements一个接口了。

执行流程

创建代理类

Proxy.newProxyInstance时,入参:代理类的类加载器和接口、InvocationHandler接口的实现类。返回:proxy(Proxy$0类型)

执行流程

proxy.dealTask(Proxy$0的dealTask方法)
super.h.invoke(this, m1, new Object[]{var1}) //Proxy父类的h(前边保存的InvocationHandler接口对象)的invoke方法
//m1是通过反射获得的方法,var1是传进来的String类型的参数
ExecutionTimeProxy(InvocationHandler接口对象)的invoke方法
method.invoke(delegate, args); //Method类的invoke方法

生成Proxy.class的流程

Proxy.newProxyInstance=> ProxyClassFactory.apply=> ProxyGenerator.generateProxyClass=> .class文件=> Class对象=> Proxy实例

关于ProxyGenerator.generateProxyClass(),内部主要是按照固定”模板“按照字节码格式生成对应class字节码,大致是:实现要代理接口的所有public方法及固定的方法(hashCode、equals和toString),然后调用InvocationHandler的invoke方法。(就是走到示例代码中的HelloProxy类(class HelloProxy implements InvocationHandler)。

动态代理(CGLIB)

其他网址

JDK动态代理和CGLIB代理的区别_学无止境! 无休无止! -CSDN博客

简介

JDK的动态代理机制只能代理实现了接口的类,而不能实现接口的类就不能实现JDK的动态代理。
CGLIB是针对类来实现代理的,他的原理是对指定的目标类生成一个子类,并覆盖其中方法实现增强,但因为采用的是继承,所以不能对final修饰的类进行代理。

时间统计示例

依赖

  1. <dependency>
  2. <groupId>cglib</groupId>
  3. <artifactId>cglib</artifactId>
  4. <version>3.2.5</version>
  5. </dependency>

较低版本的cglib可能需要此依赖:

  1. <dependency>
  2. <groupId>org.ow2.asm</groupId>
  3. <artifactId>asm</artifactId>
  4. <version>5.0.1</version>
  5. </dependency>

代码

  1. package org.example.a;
  2. import net.sf.cglib.core.DebuggingClassWriter;
  3. import net.sf.cglib.proxy.Enhancer;
  4. import net.sf.cglib.proxy.MethodInterceptor;
  5. import net.sf.cglib.proxy.MethodProxy;
  6. import java.lang.reflect.Method;
  7. //被代理类
  8. class ExecutionTimeReality {
  9. //这里打印出任务名,并休眠500ms模拟任务执行了很长时间
  10. public void dealTask(String taskName) {
  11. System.out.println("Task is running: " + taskName);
  12. try {
  13. Thread.sleep(500);
  14. } catch (InterruptedException e) {
  15. e.printStackTrace();
  16. }
  17. }
  18. //这里打印出任务名,并休眠500ms模拟任务执行了很长时间
  19. public void sayHello(String name) {
  20. System.out.println("hello: " + name);
  21. }
  22. }
  23. //代理类
  24. class ExecutionTimeProxy implements MethodInterceptor {
  25. @Override
  26. public Object intercept(Object obj, Method method, Object[] args,
  27. MethodProxy proxy) throws Throwable {
  28. long startTime = System.currentTimeMillis();
  29. proxy.invokeSuper(obj, args);
  30. long endTime = System.currentTimeMillis();
  31. System.out.println("Elapsed time: " + (endTime - startTime) + " ms");
  32. return null;
  33. }
  34. }
  35. //工厂方法获得代理对象
  36. class DynamicProxyFactory {
  37. public static Object getInstance() {
  38. Object target = new ExecutionTimeReality();
  39. ExecutionTimeProxy proxySubject = new ExecutionTimeProxy();
  40. Enhancer enhancer = new Enhancer();
  41. enhancer.setSuperclass(target.getClass());
  42. enhancer.setCallback(proxySubject);
  43. return enhancer.create();
  44. }
  45. }
  46. public class Demo {
  47. public static void main(String[] args) {
  48. System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "E:\\CglibProxyClass");
  49. ExecutionTimeReality subject = (ExecutionTimeReality) DynamicProxyFactory.getInstance();
  50. subject.dealTask("TestTask");
  51. }
  52. }

执行结果

Task is running: TestTask
Elapsed time: 524 ms

原理分析

其他网址

Cglib动态代理实现原理 - 糖豆包子 - 博客园

流程概述

本部分应该放到最后作为结论。但本处为了快速体会总体流程,放到最前边。

实例化Enhancer并设置要代理的类及回调(MethodInterceptor接口的实现类对象)
创建代理类(继承自被代理类)
被代理类的dealTask方法(已被代理类覆写,实际调用代理类的dealTask方法)
回调的intercept方法(ExecutionTimeProxy.intercept) //回调是在第一步中传进来的
proxy.invokeSuper(obj, args); //调用父类 invokeSuper方法是MethodProxy声明的
this.init(); //若不存在FastClass,则生成
this.fastClassInfo.f2.invoke(fci.i2, obj, args);
FastClass的invoke方法
将第一个参数与数字比对,执行对应的方法

分析源码,就要看生成的代理类源码,生成方法:在main最开始写此代码:

  1. System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "E:\\CglibProxyClass");

运行后,会生成E:/CglibProxyClass文件夹,其结构如下

  1. E:\CGLIBPROXYCLASS
  2. ├─net
  3. └─sf
  4. └─cglib
  5. ├─core
  6. MethodWrapper$MethodWrapperKey$$KeyFactoryByCGLIB$$d45e49f7.class
  7. └─proxy
  8. Enhancer$EnhancerKey$$KeyFactoryByCGLIB$$7fb24d72.class
  9. └─org
  10. └─example
  11. └─a
  12. ExecutionTimeReality$$EnhancerByCGLIB$$5a92c386$$FastClassByCGLIB$$bebee033.class
  13. ExecutionTimeReality$$EnhancerByCGLIB$$5a92c386.class
  14. ExecutionTimeReality$$FastClassByCGLIB$$81389d66.class

因为两个$在CSDN中显示不正常,所以下边用一个$表示两个$

ExecutionTimeReality$EnhancerByCGLIB$5a92c386$FastClassByCGLIB$bebee033.class

快速类1。此类暂不讨论。代码中没分析到。

ExecutionTimeReality$EnhancerByCGLIB$5a92c386.class

代理类,会调用到ExecutionTimeReality$EnhancerByCGLIB$5a92c386.class

ExecutionTimeReality$FastClassByCGLIB$81389d66.class

快速类2。被代理类调用到。FastClass并不是跟代理类一块生成的,而是在第一次执行MethodProxy invoke/invokeSuper时生成的并放在了缓存中。

ExecutionTimeReality$EnhancerByCGLIB$5a92c386.class

  1. //
  2. // Source code recreated from a .class file by IntelliJ IDEA
  3. // (powered by Fernflower decompiler)
  4. //
  5. package org.example.a;
  6. import java.lang.reflect.Method;
  7. import net.sf.cglib.core.ReflectUtils;
  8. import net.sf.cglib.core.Signature;
  9. import net.sf.cglib.proxy.Callback;
  10. import net.sf.cglib.proxy.Factory;
  11. import net.sf.cglib.proxy.MethodInterceptor;
  12. import net.sf.cglib.proxy.MethodProxy;
  13. public class ExecutionTimeReality$$EnhancerByCGLIB$$5a92c386 extends ExecutionTimeReality implements Factory {
  14. private boolean CGLIB$BOUND;
  15. public static Object CGLIB$FACTORY_DATA;
  16. private static final ThreadLocal CGLIB$THREAD_CALLBACKS;
  17. private static final Callback[] CGLIB$STATIC_CALLBACKS;
  18. private MethodInterceptor CGLIB$CALLBACK_0;
  19. private static Object CGLIB$CALLBACK_FILTER;
  20. private static final Method CGLIB$dealTask$0$Method;
  21. private static final MethodProxy CGLIB$dealTask$0$Proxy;
  22. private static final Object[] CGLIB$emptyArgs;
  23. private static final Method CGLIB$sayHello$1$Method;
  24. private static final MethodProxy CGLIB$sayHello$1$Proxy;
  25. private static final Method CGLIB$equals$2$Method;
  26. private static final MethodProxy CGLIB$equals$2$Proxy;
  27. private static final Method CGLIB$toString$3$Method;
  28. private static final MethodProxy CGLIB$toString$3$Proxy;
  29. private static final Method CGLIB$hashCode$4$Method;
  30. private static final MethodProxy CGLIB$hashCode$4$Proxy;
  31. private static final Method CGLIB$clone$5$Method;
  32. private static final MethodProxy CGLIB$clone$5$Proxy;
  33. static void CGLIB$STATICHOOK1() {
  34. CGLIB$THREAD_CALLBACKS = new ThreadLocal();
  35. CGLIB$emptyArgs = new Object[0];
  36. Class var0 = Class.forName("org.example.a.ExecutionTimeReality$$EnhancerByCGLIB$$5a92c386");
  37. Class var1;
  38. Method[] var10000 = ReflectUtils.findMethods(new String[]{"equals", "(Ljava/lang/Object;)Z", "toString", "()Ljava/lang/String;", "hashCode", "()I", "clone", "()Ljava/lang/Object;"}, (var1 = Class.forName("java.lang.Object")).getDeclaredMethods());
  39. CGLIB$equals$2$Method = var10000[0];
  40. CGLIB$equals$2$Proxy = MethodProxy.create(var1, var0, "(Ljava/lang/Object;)Z", "equals", "CGLIB$equals$2");
  41. CGLIB$toString$3$Method = var10000[1];
  42. CGLIB$toString$3$Proxy = MethodProxy.create(var1, var0, "()Ljava/lang/String;", "toString", "CGLIB$toString$3");
  43. CGLIB$hashCode$4$Method = var10000[2];
  44. CGLIB$hashCode$4$Proxy = MethodProxy.create(var1, var0, "()I", "hashCode", "CGLIB$hashCode$4");
  45. CGLIB$clone$5$Method = var10000[3];
  46. CGLIB$clone$5$Proxy = MethodProxy.create(var1, var0, "()Ljava/lang/Object;", "clone", "CGLIB$clone$5");
  47. var10000 = ReflectUtils.findMethods(new String[]{"dealTask", "(Ljava/lang/String;)V", "sayHello", "(Ljava/lang/String;)V"}, (var1 = Class.forName("org.example.a.ExecutionTimeReality")).getDeclaredMethods());
  48. CGLIB$dealTask$0$Method = var10000[0];
  49. CGLIB$dealTask$0$Proxy = MethodProxy.create(var1, var0, "(Ljava/lang/String;)V", "dealTask", "CGLIB$dealTask$0");
  50. CGLIB$sayHello$1$Method = var10000[1];
  51. CGLIB$sayHello$1$Proxy = MethodProxy.create(var1, var0, "(Ljava/lang/String;)V", "sayHello", "CGLIB$sayHello$1");
  52. }
  53. final void CGLIB$dealTask$0(String var1) {
  54. super.dealTask(var1);
  55. }
  56. public final void dealTask(String var1) {
  57. MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
  58. if (var10000 == null) {
  59. CGLIB$BIND_CALLBACKS(this);
  60. var10000 = this.CGLIB$CALLBACK_0;
  61. }
  62. if (var10000 != null) {
  63. var10000.intercept(this, CGLIB$dealTask$0$Method, new Object[]{var1}, CGLIB$dealTask$0$Proxy);
  64. } else {
  65. super.dealTask(var1);
  66. }
  67. }
  68. final void CGLIB$sayHello$1(String var1) {
  69. super.sayHello(var1);
  70. }
  71. public final void sayHello(String var1) {
  72. MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
  73. if (var10000 == null) {
  74. CGLIB$BIND_CALLBACKS(this);
  75. var10000 = this.CGLIB$CALLBACK_0;
  76. }
  77. if (var10000 != null) {
  78. var10000.intercept(this, CGLIB$sayHello$1$Method, new Object[]{var1}, CGLIB$sayHello$1$Proxy);
  79. } else {
  80. super.sayHello(var1);
  81. }
  82. }
  83. final boolean CGLIB$equals$2(Object var1) {
  84. return super.equals(var1);
  85. }
  86. public final boolean equals(Object var1) {
  87. MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
  88. if (var10000 == null) {
  89. CGLIB$BIND_CALLBACKS(this);
  90. var10000 = this.CGLIB$CALLBACK_0;
  91. }
  92. if (var10000 != null) {
  93. Object var2 = var10000.intercept(this, CGLIB$equals$2$Method, new Object[]{var1}, CGLIB$equals$2$Proxy);
  94. return var2 == null ? false : (Boolean)var2;
  95. } else {
  96. return super.equals(var1);
  97. }
  98. }
  99. final String CGLIB$toString$3() {
  100. return super.toString();
  101. }
  102. public final String toString() {
  103. MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
  104. if (var10000 == null) {
  105. CGLIB$BIND_CALLBACKS(this);
  106. var10000 = this.CGLIB$CALLBACK_0;
  107. }
  108. return var10000 != null ? (String)var10000.intercept(this, CGLIB$toString$3$Method, CGLIB$emptyArgs, CGLIB$toString$3$Proxy) : super.toString();
  109. }
  110. final int CGLIB$hashCode$4() {
  111. return super.hashCode();
  112. }
  113. public final int hashCode() {
  114. MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
  115. if (var10000 == null) {
  116. CGLIB$BIND_CALLBACKS(this);
  117. var10000 = this.CGLIB$CALLBACK_0;
  118. }
  119. if (var10000 != null) {
  120. Object var1 = var10000.intercept(this, CGLIB$hashCode$4$Method, CGLIB$emptyArgs, CGLIB$hashCode$4$Proxy);
  121. return var1 == null ? 0 : ((Number)var1).intValue();
  122. } else {
  123. return super.hashCode();
  124. }
  125. }
  126. final Object CGLIB$clone$5() throws CloneNotSupportedException {
  127. return super.clone();
  128. }
  129. protected final Object clone() throws CloneNotSupportedException {
  130. MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
  131. if (var10000 == null) {
  132. CGLIB$BIND_CALLBACKS(this);
  133. var10000 = this.CGLIB$CALLBACK_0;
  134. }
  135. return var10000 != null ? var10000.intercept(this, CGLIB$clone$5$Method, CGLIB$emptyArgs, CGLIB$clone$5$Proxy) : super.clone();
  136. }
  137. public static MethodProxy CGLIB$findMethodProxy(Signature var0) {
  138. String var10000 = var0.toString();
  139. switch(var10000.hashCode()) {
  140. case -1165719922:
  141. if (var10000.equals("dealTask(Ljava/lang/String;)V")) {
  142. return CGLIB$dealTask$0$Proxy;
  143. }
  144. break;
  145. case -508378822:
  146. if (var10000.equals("clone()Ljava/lang/Object;")) {
  147. return CGLIB$clone$5$Proxy;
  148. }
  149. break;
  150. case 771401912:
  151. if (var10000.equals("sayHello(Ljava/lang/String;)V")) {
  152. return CGLIB$sayHello$1$Proxy;
  153. }
  154. break;
  155. case 1826985398:
  156. if (var10000.equals("equals(Ljava/lang/Object;)Z")) {
  157. return CGLIB$equals$2$Proxy;
  158. }
  159. break;
  160. case 1913648695:
  161. if (var10000.equals("toString()Ljava/lang/String;")) {
  162. return CGLIB$toString$3$Proxy;
  163. }
  164. break;
  165. case 1984935277:
  166. if (var10000.equals("hashCode()I")) {
  167. return CGLIB$hashCode$4$Proxy;
  168. }
  169. }
  170. return null;
  171. }
  172. public ExecutionTimeReality$$EnhancerByCGLIB$$5a92c386() {
  173. CGLIB$BIND_CALLBACKS(this);
  174. }
  175. public static void CGLIB$SET_THREAD_CALLBACKS(Callback[] var0) {
  176. CGLIB$THREAD_CALLBACKS.set(var0);
  177. }
  178. public static void CGLIB$SET_STATIC_CALLBACKS(Callback[] var0) {
  179. CGLIB$STATIC_CALLBACKS = var0;
  180. }
  181. private static final void CGLIB$BIND_CALLBACKS(Object var0) {
  182. ExecutionTimeReality$$EnhancerByCGLIB$$5a92c386 var1 = (ExecutionTimeReality$$EnhancerByCGLIB$$5a92c386)var0;
  183. if (!var1.CGLIB$BOUND) {
  184. var1.CGLIB$BOUND = true;
  185. Object var10000 = CGLIB$THREAD_CALLBACKS.get();
  186. if (var10000 == null) {
  187. var10000 = CGLIB$STATIC_CALLBACKS;
  188. if (var10000 == null) {
  189. return;
  190. }
  191. }
  192. var1.CGLIB$CALLBACK_0 = (MethodInterceptor)((Callback[])var10000)[0];
  193. }
  194. }
  195. public Object newInstance(Callback[] var1) {
  196. CGLIB$SET_THREAD_CALLBACKS(var1);
  197. ExecutionTimeReality$$EnhancerByCGLIB$$5a92c386 var10000 = new ExecutionTimeReality$$EnhancerByCGLIB$$5a92c386();
  198. CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
  199. return var10000;
  200. }
  201. public Object newInstance(Callback var1) {
  202. CGLIB$SET_THREAD_CALLBACKS(new Callback[]{var1});
  203. ExecutionTimeReality$$EnhancerByCGLIB$$5a92c386 var10000 = new ExecutionTimeReality$$EnhancerByCGLIB$$5a92c386();
  204. CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
  205. return var10000;
  206. }
  207. public Object newInstance(Class[] var1, Object[] var2, Callback[] var3) {
  208. CGLIB$SET_THREAD_CALLBACKS(var3);
  209. ExecutionTimeReality$$EnhancerByCGLIB$$5a92c386 var10000 = new ExecutionTimeReality$$EnhancerByCGLIB$$5a92c386;
  210. switch(var1.length) {
  211. case 0:
  212. var10000.<init>();
  213. CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
  214. return var10000;
  215. default:
  216. throw new IllegalArgumentException("Constructor not found");
  217. }
  218. }
  219. public Callback getCallback(int var1) {
  220. CGLIB$BIND_CALLBACKS(this);
  221. MethodInterceptor var10000;
  222. switch(var1) {
  223. case 0:
  224. var10000 = this.CGLIB$CALLBACK_0;
  225. break;
  226. default:
  227. var10000 = null;
  228. }
  229. return var10000;
  230. }
  231. public void setCallback(int var1, Callback var2) {
  232. switch(var1) {
  233. case 0:
  234. this.CGLIB$CALLBACK_0 = (MethodInterceptor)var2;
  235. default:
  236. }
  237. }
  238. public Callback[] getCallbacks() {
  239. CGLIB$BIND_CALLBACKS(this);
  240. return new Callback[]{this.CGLIB$CALLBACK_0};
  241. }
  242. public void setCallbacks(Callback[] var1) {
  243. this.CGLIB$CALLBACK_0 = (MethodInterceptor)var1[0];
  244. }
  245. static {
  246. CGLIB$STATICHOOK1();
  247. }
  248. }

我们通过代理类的源码可以看到,代理类会获得所有在父类继承来的方法,并且会有MethodProxy与之对应,比如:

  1. private static final Method CGLIB$dealTask$0$Method;
  2. private static final MethodProxy CGLIB$dealTask$0$Proxy;
  3. private static final Method CGLIB$sayHello$1$Method;
  4. private static final MethodProxy CGLIB$sayHello$1$Proxy;

另外,从代理类的类型来看,它直接继承要代理的类,所以可以代理的没有接口的类。其实,它跟接口没关系了,有接口也不会用到。

  1. public class ExecutionTimeReality$$EnhancerByCGLIB$$5a92c386 extends ExecutionTimeReality implements Factory

方法的调用

  1. final void CGLIB$dealTask$0(String var1) {
  2. super.dealTask(var1);
  3. }
  4. public final void dealTask(String var1) {
  5. MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
  6. if (var10000 == null) {
  7. CGLIB$BIND_CALLBACKS(this);
  8. var10000 = this.CGLIB$CALLBACK_0;
  9. }
  10. if (var10000 != null) {
  11. var10000.intercept(this, CGLIB$dealTask$0$Method, new Object[]{var1}, CGLIB$dealTask$0$Proxy);
  12. } else {
  13. super.dealTask(var1);
  14. }
  15. }

FastClass机制

Cglib动态代理执行代理方法效率之所以比JDK的高是因为Cglib采用了FastClass机制,它的原理简单来说就是:为代理类和被代理类各生成一个Class,这个Class会为代理类或被代理类的方法分配一个index(int类型)。这个index当做一个入参,FastClass就可以直接定位要调用的方法直接进行调用,这样省去了反射调用,所以调用效率比JDK动态代理通过反射调用高。

ExecutionTimeReality$FastClassByCGLIB$81389d66

  1. //
  2. // Source code recreated from a .class file by IntelliJ IDEA
  3. // (powered by Fernflower decompiler)
  4. //
  5. package org.example.a;
  6. import java.lang.reflect.InvocationTargetException;
  7. import net.sf.cglib.core.Signature;
  8. import net.sf.cglib.reflect.FastClass;
  9. public class ExecutionTimeReality$$FastClassByCGLIB$$81389d66 extends FastClass {
  10. public ExecutionTimeReality$$FastClassByCGLIB$$81389d66(Class var1) {
  11. super(var1);
  12. }
  13. public int getIndex(Signature var1) {
  14. String var10000 = var1.toString();
  15. switch(var10000.hashCode()) {
  16. case -1165719922:
  17. if (var10000.equals("dealTask(Ljava/lang/String;)V")) {
  18. return 0;
  19. }
  20. break;
  21. case 771401912:
  22. if (var10000.equals("sayHello(Ljava/lang/String;)V")) {
  23. return 1;
  24. }
  25. break;
  26. case 1826985398:
  27. if (var10000.equals("equals(Ljava/lang/Object;)Z")) {
  28. return 2;
  29. }
  30. break;
  31. case 1913648695:
  32. if (var10000.equals("toString()Ljava/lang/String;")) {
  33. return 3;
  34. }
  35. break;
  36. case 1984935277:
  37. if (var10000.equals("hashCode()I")) {
  38. return 4;
  39. }
  40. }
  41. return -1;
  42. }
  43. public int getIndex(String var1, Class[] var2) {
  44. switch(var1.hashCode()) {
  45. case -2012993625:
  46. if (var1.equals("sayHello")) {
  47. switch(var2.length) {
  48. case 1:
  49. if (var2[0].getName().equals("java.lang.String")) {
  50. return 1;
  51. }
  52. }
  53. }
  54. break;
  55. case -1776922004:
  56. if (var1.equals("toString")) {
  57. switch(var2.length) {
  58. case 0:
  59. return 3;
  60. }
  61. }
  62. break;
  63. case -1295482945:
  64. if (var1.equals("equals")) {
  65. switch(var2.length) {
  66. case 1:
  67. if (var2[0].getName().equals("java.lang.Object")) {
  68. return 2;
  69. }
  70. }
  71. }
  72. break;
  73. case 147696667:
  74. if (var1.equals("hashCode")) {
  75. switch(var2.length) {
  76. case 0:
  77. return 4;
  78. }
  79. }
  80. break;
  81. case 510300177:
  82. if (var1.equals("dealTask")) {
  83. switch(var2.length) {
  84. case 1:
  85. if (var2[0].getName().equals("java.lang.String")) {
  86. return 0;
  87. }
  88. }
  89. }
  90. }
  91. return -1;
  92. }
  93. public int getIndex(Class[] var1) {
  94. switch(var1.length) {
  95. case 0:
  96. return 0;
  97. default:
  98. return -1;
  99. }
  100. }
  101. public Object invoke(int var1, Object var2, Object[] var3) throws InvocationTargetException {
  102. ExecutionTimeReality var10000 = (ExecutionTimeReality)var2;
  103. int var10001 = var1;
  104. try {
  105. switch(var10001) {
  106. case 0:
  107. var10000.dealTask((String)var3[0]);
  108. return null;
  109. case 1:
  110. var10000.sayHello((String)var3[0]);
  111. return null;
  112. case 2:
  113. return new Boolean(var10000.equals(var3[0]));
  114. case 3:
  115. return var10000.toString();
  116. case 4:
  117. return new Integer(var10000.hashCode());
  118. }
  119. } catch (Throwable var4) {
  120. throw new InvocationTargetException(var4);
  121. }
  122. throw new IllegalArgumentException("Cannot find matching method/constructor");
  123. }
  124. public Object newInstance(int var1, Object[] var2) throws InvocationTargetException {
  125. ExecutionTimeReality var10000 = new ExecutionTimeReality;
  126. ExecutionTimeReality var10001 = var10000;
  127. int var10002 = var1;
  128. try {
  129. switch(var10002) {
  130. case 0:
  131. var10001.<init>();
  132. return var10000;
  133. }
  134. } catch (Throwable var3) {
  135. throw new InvocationTargetException(var3);
  136. }
  137. throw new IllegalArgumentException("Cannot find matching method/constructor");
  138. }
  139. public int getMaxIndex() {
  140. return 4;
  141. }
  142. }

发表评论

表情:
评论列表 (有 0 条评论,31人围观)

还没有评论,来说两句吧...

相关阅读