Java设计模式--静态代理与动态代理
原文网址:Java设计模式—静态代理与动态代理_IT利刃出鞘的博客-CSDN博客
其他网址
JAVA动态代理 - 简书
Spring AOP—综述_feiying0canglang的博客-CSDN博客
简介
其他网址
JDK和CGLIB动态代理区别_java_yhl_jxy的博客-CSDN博客
代理模式
为某个对象提供一个代理,以控制对这个对象的访问。 代理类和委托类有共同的父类或父接口,这样在任何使用委托类对象的地方都可以用代理对象替代。代理类负责请求的预处理、过滤、将请求分派给委托类处理、以及委托类执行完请求后的后续处理。
从图中可以看出,代理接口(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高。
静态代理
时间统计示例
代码
package org.example.a;
//代理接口
interface ExecuteTime {
public void dealTask(String taskName);
}
//被代理类
class ExecuteTimeImpl implements ExecuteTime {
//这里打印出任务名,并休眠500ms模拟任务执行了很长时间
@Override
public void dealTask(String taskName) {
System.out.println("Task is running: "+taskName);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//代理类
class ExecuteTimeProxy implements ExecuteTime {
private ExecuteTime delegate = null;
public ExecuteTimeProxy(ExecuteTimeImpl ExecuteTimeImpl) {
this.delegate = ExecuteTimeImpl;
}
@Override
public void dealTask(String taskName) {
long startTime = System.currentTimeMillis();
delegate.dealTask(taskName);
long endTime = System.currentTimeMillis();
System.out.println("Elapsed time: "+(endTime - startTime)+" ms");
}
}
//工厂方法获得代理对象
class StatiProxyFactory {
//对客户类来说,其并不知道返回的是代理类对象还是委托类对象。
public static ExecuteTime getInstance(){
return new ExecuteTimeProxy(new ExecuteTimeImpl());
}
}
public class Demo{
public static void main(String[] args) {
ExecuteTime proxy = StatiProxyFactory.getInstance();
proxy.dealTask("TestTask");
}
}
运行结果:
Task is running: TestTask
Elapsed time: 501 ms
动态代理(JDK)
动态代理具体步骤:
- 通过实现 InvocationHandler 接口创建自己的调用处理器;
- 通过为 Proxy 类指定 ClassLoader 对象和一组 interface 来创建动态代理类;
- 通过反射机制获得动态代理类的构造函数,其唯一参数类型是调用处理器接口类型;
- 通过构造函数创建动态代理类实例,构造时调用处理器对象作为参数被传入。
时间统计示例
代码
package org.example.a;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
//代理接口
interface ExecutionTime {
public void dealTask(String taskName);
public void sayHello(String name);
}
//被代理类
class ExecutionTimeImpl implements ExecutionTime {
//这里打印出任务名,并休眠500ms模拟任务执行了很长时间
@Override
public void dealTask(String taskName) {
System.out.println("Task is running: "+taskName);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void sayHello(String name) {
System.out.println("hello" + name);
}
}
// 动态代理类对应的调用处理程序类
class ExecutionTimeProxy implements InvocationHandler {
private Object delegate = null;
public ExecutionTimeProxy(ExecutionTime delegate) {
this.delegate = delegate;
}
// proxy:代理类代理的真实代理对象。例如:com.sun.proxy.$Proxy0
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
long startTime = System.currentTimeMillis();
method.invoke(delegate, args);
long endTime = System.currentTimeMillis();
System.out.println("Elapsed time: "+(endTime - startTime)+" ms");
return null;
}
}
//生成动态代理对象的工厂
class DynamicProxyFactory {
public static ExecutionTime getInstance(){
ExecutionTime delegate = new ExecutionTimeImpl();
InvocationHandler handler = new ExecutionTimeProxy(delegate);
ExecutionTime proxy = (ExecutionTime) Proxy.newProxyInstance(
delegate.getClass().getClassLoader(),
delegate.getClass().getInterfaces(),
handler);
return proxy;
}
}
public class Demo{
public static void main(String[] args) {
System.setProperty("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
ExecutionTime proxy = DynamicProxyFactory.getInstance();
System.out.println(proxy.getClass());
System.out.println("-----------------------------------");
proxy.dealTask("TestTask");
}
}
运行结果
class org.example.a.$Proxy0
-----------------------------------
Task is running: TestTask
Elapsed time: 500 ms
ArrayList示例
package org.example.a;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;
public class Demo {
public static void main(String[] args) {
final ArrayList<String> list = new ArrayList<>();
List<String> listProxy=(List<String>) Proxy.newProxyInstance(
list.getClass().getClassLoader(),
list.getClass().getInterfaces(),
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return method.invoke(list,args);
}
});
listProxy.add("hello") ;
listProxy.add("world") ;
System.out.println(list);
}
}
运行结果
[hello, world]
原理
生成代理类的源码
其他网址
JDK动态代理为什么不能代理类 - 知乎
查看自动生成的proxy class
默认在运行时程序自动生成proxy class,若想看下自动生成的proxy class有三种方法
- 设置参数-Dsun.misc.ProxyGenerator.saveGeneratedFiles=true
- 程序里调用:System.setProperty(“sun.misc.ProxyGenerator.saveGeneratedFiles”, “true”);
在程序中直接调用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方法中最前边添加
System.setProperty("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
执行结果:生成了$Proxy0.class
$Proxy0.class代码如下
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.example.a;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;
final class $Proxy0 extends Proxy implements ExecutionTime {
private static Method m1;
private static Method m4;
private static Method m2;
private static Method m3;
private static Method m0;
static {
try {
m1 = Class.forName("java.lang.Object").getMethod("equals", Class.forName("java.lang.Object"));
m4 = Class.forName("org.example.a.ExecutionTime").getMethod("sayHello", Class.forName("java.lang.String"));
m2 = Class.forName("java.lang.Object").getMethod("toString");
m3 = Class.forName("org.example.a.ExecutionTime").getMethod("dealTask", Class.forName("java.lang.String"));
m0 = Class.forName("java.lang.Object").getMethod("hashCode");
} catch (NoSuchMethodException var2) {
throw new NoSuchMethodError(var2.getMessage());
} catch (ClassNotFoundException var3) {
throw new NoClassDefFoundError(var3.getMessage());
}
}
public $Proxy0(InvocationHandler var1) throws {
super(var1);
}
public final void dealTask(String var1) throws {
try {
super.h.invoke(this, m3, new Object[]{var1});
} catch (RuntimeException | Error var3) {
throw var3;
} catch (Throwable var4) {
throw new UndeclaredThrowableException(var4);
}
}
public final void sayHello(String 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 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 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 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);
}
}
}
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修饰的类进行代理。
时间统计示例
依赖
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.2.5</version>
</dependency>
较低版本的cglib可能需要此依赖:
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>5.0.1</version>
</dependency>
代码
package org.example.a;
import net.sf.cglib.core.DebuggingClassWriter;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
//被代理类
class ExecutionTimeReality {
//这里打印出任务名,并休眠500ms模拟任务执行了很长时间
public void dealTask(String taskName) {
System.out.println("Task is running: " + taskName);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//这里打印出任务名,并休眠500ms模拟任务执行了很长时间
public void sayHello(String name) {
System.out.println("hello: " + name);
}
}
//代理类
class ExecutionTimeProxy implements MethodInterceptor {
@Override
public Object intercept(Object obj, Method method, Object[] args,
MethodProxy proxy) throws Throwable {
long startTime = System.currentTimeMillis();
proxy.invokeSuper(obj, args);
long endTime = System.currentTimeMillis();
System.out.println("Elapsed time: " + (endTime - startTime) + " ms");
return null;
}
}
//工厂方法获得代理对象
class DynamicProxyFactory {
public static Object getInstance() {
Object target = new ExecutionTimeReality();
ExecutionTimeProxy proxySubject = new ExecutionTimeProxy();
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(target.getClass());
enhancer.setCallback(proxySubject);
return enhancer.create();
}
}
public class Demo {
public static void main(String[] args) {
System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "E:\\CglibProxyClass");
ExecutionTimeReality subject = (ExecutionTimeReality) DynamicProxyFactory.getInstance();
subject.dealTask("TestTask");
}
}
执行结果
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最开始写此代码:
System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "E:\\CglibProxyClass");
运行后,会生成E:/CglibProxyClass文件夹,其结构如下
E:\CGLIBPROXYCLASS
├─net
│ └─sf
│ └─cglib
│ ├─core
│ │ MethodWrapper$MethodWrapperKey$$KeyFactoryByCGLIB$$d45e49f7.class
│ │
│ └─proxy
│ Enhancer$EnhancerKey$$KeyFactoryByCGLIB$$7fb24d72.class
│
└─org
└─example
└─a
ExecutionTimeReality$$EnhancerByCGLIB$$5a92c386$$FastClassByCGLIB$$bebee033.class
ExecutionTimeReality$$EnhancerByCGLIB$$5a92c386.class
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
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.example.a;
import java.lang.reflect.Method;
import net.sf.cglib.core.ReflectUtils;
import net.sf.cglib.core.Signature;
import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.Factory;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
public class ExecutionTimeReality$$EnhancerByCGLIB$$5a92c386 extends ExecutionTimeReality implements Factory {
private boolean CGLIB$BOUND;
public static Object CGLIB$FACTORY_DATA;
private static final ThreadLocal CGLIB$THREAD_CALLBACKS;
private static final Callback[] CGLIB$STATIC_CALLBACKS;
private MethodInterceptor CGLIB$CALLBACK_0;
private static Object CGLIB$CALLBACK_FILTER;
private static final Method CGLIB$dealTask$0$Method;
private static final MethodProxy CGLIB$dealTask$0$Proxy;
private static final Object[] CGLIB$emptyArgs;
private static final Method CGLIB$sayHello$1$Method;
private static final MethodProxy CGLIB$sayHello$1$Proxy;
private static final Method CGLIB$equals$2$Method;
private static final MethodProxy CGLIB$equals$2$Proxy;
private static final Method CGLIB$toString$3$Method;
private static final MethodProxy CGLIB$toString$3$Proxy;
private static final Method CGLIB$hashCode$4$Method;
private static final MethodProxy CGLIB$hashCode$4$Proxy;
private static final Method CGLIB$clone$5$Method;
private static final MethodProxy CGLIB$clone$5$Proxy;
static void CGLIB$STATICHOOK1() {
CGLIB$THREAD_CALLBACKS = new ThreadLocal();
CGLIB$emptyArgs = new Object[0];
Class var0 = Class.forName("org.example.a.ExecutionTimeReality$$EnhancerByCGLIB$$5a92c386");
Class var1;
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());
CGLIB$equals$2$Method = var10000[0];
CGLIB$equals$2$Proxy = MethodProxy.create(var1, var0, "(Ljava/lang/Object;)Z", "equals", "CGLIB$equals$2");
CGLIB$toString$3$Method = var10000[1];
CGLIB$toString$3$Proxy = MethodProxy.create(var1, var0, "()Ljava/lang/String;", "toString", "CGLIB$toString$3");
CGLIB$hashCode$4$Method = var10000[2];
CGLIB$hashCode$4$Proxy = MethodProxy.create(var1, var0, "()I", "hashCode", "CGLIB$hashCode$4");
CGLIB$clone$5$Method = var10000[3];
CGLIB$clone$5$Proxy = MethodProxy.create(var1, var0, "()Ljava/lang/Object;", "clone", "CGLIB$clone$5");
var10000 = ReflectUtils.findMethods(new String[]{"dealTask", "(Ljava/lang/String;)V", "sayHello", "(Ljava/lang/String;)V"}, (var1 = Class.forName("org.example.a.ExecutionTimeReality")).getDeclaredMethods());
CGLIB$dealTask$0$Method = var10000[0];
CGLIB$dealTask$0$Proxy = MethodProxy.create(var1, var0, "(Ljava/lang/String;)V", "dealTask", "CGLIB$dealTask$0");
CGLIB$sayHello$1$Method = var10000[1];
CGLIB$sayHello$1$Proxy = MethodProxy.create(var1, var0, "(Ljava/lang/String;)V", "sayHello", "CGLIB$sayHello$1");
}
final void CGLIB$dealTask$0(String var1) {
super.dealTask(var1);
}
public final void dealTask(String var1) {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
if (var10000 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_0;
}
if (var10000 != null) {
var10000.intercept(this, CGLIB$dealTask$0$Method, new Object[]{var1}, CGLIB$dealTask$0$Proxy);
} else {
super.dealTask(var1);
}
}
final void CGLIB$sayHello$1(String var1) {
super.sayHello(var1);
}
public final void sayHello(String var1) {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
if (var10000 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_0;
}
if (var10000 != null) {
var10000.intercept(this, CGLIB$sayHello$1$Method, new Object[]{var1}, CGLIB$sayHello$1$Proxy);
} else {
super.sayHello(var1);
}
}
final boolean CGLIB$equals$2(Object var1) {
return super.equals(var1);
}
public final boolean equals(Object var1) {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
if (var10000 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_0;
}
if (var10000 != null) {
Object var2 = var10000.intercept(this, CGLIB$equals$2$Method, new Object[]{var1}, CGLIB$equals$2$Proxy);
return var2 == null ? false : (Boolean)var2;
} else {
return super.equals(var1);
}
}
final String CGLIB$toString$3() {
return super.toString();
}
public final String toString() {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
if (var10000 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_0;
}
return var10000 != null ? (String)var10000.intercept(this, CGLIB$toString$3$Method, CGLIB$emptyArgs, CGLIB$toString$3$Proxy) : super.toString();
}
final int CGLIB$hashCode$4() {
return super.hashCode();
}
public final int hashCode() {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
if (var10000 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_0;
}
if (var10000 != null) {
Object var1 = var10000.intercept(this, CGLIB$hashCode$4$Method, CGLIB$emptyArgs, CGLIB$hashCode$4$Proxy);
return var1 == null ? 0 : ((Number)var1).intValue();
} else {
return super.hashCode();
}
}
final Object CGLIB$clone$5() throws CloneNotSupportedException {
return super.clone();
}
protected final Object clone() throws CloneNotSupportedException {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
if (var10000 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_0;
}
return var10000 != null ? var10000.intercept(this, CGLIB$clone$5$Method, CGLIB$emptyArgs, CGLIB$clone$5$Proxy) : super.clone();
}
public static MethodProxy CGLIB$findMethodProxy(Signature var0) {
String var10000 = var0.toString();
switch(var10000.hashCode()) {
case -1165719922:
if (var10000.equals("dealTask(Ljava/lang/String;)V")) {
return CGLIB$dealTask$0$Proxy;
}
break;
case -508378822:
if (var10000.equals("clone()Ljava/lang/Object;")) {
return CGLIB$clone$5$Proxy;
}
break;
case 771401912:
if (var10000.equals("sayHello(Ljava/lang/String;)V")) {
return CGLIB$sayHello$1$Proxy;
}
break;
case 1826985398:
if (var10000.equals("equals(Ljava/lang/Object;)Z")) {
return CGLIB$equals$2$Proxy;
}
break;
case 1913648695:
if (var10000.equals("toString()Ljava/lang/String;")) {
return CGLIB$toString$3$Proxy;
}
break;
case 1984935277:
if (var10000.equals("hashCode()I")) {
return CGLIB$hashCode$4$Proxy;
}
}
return null;
}
public ExecutionTimeReality$$EnhancerByCGLIB$$5a92c386() {
CGLIB$BIND_CALLBACKS(this);
}
public static void CGLIB$SET_THREAD_CALLBACKS(Callback[] var0) {
CGLIB$THREAD_CALLBACKS.set(var0);
}
public static void CGLIB$SET_STATIC_CALLBACKS(Callback[] var0) {
CGLIB$STATIC_CALLBACKS = var0;
}
private static final void CGLIB$BIND_CALLBACKS(Object var0) {
ExecutionTimeReality$$EnhancerByCGLIB$$5a92c386 var1 = (ExecutionTimeReality$$EnhancerByCGLIB$$5a92c386)var0;
if (!var1.CGLIB$BOUND) {
var1.CGLIB$BOUND = true;
Object var10000 = CGLIB$THREAD_CALLBACKS.get();
if (var10000 == null) {
var10000 = CGLIB$STATIC_CALLBACKS;
if (var10000 == null) {
return;
}
}
var1.CGLIB$CALLBACK_0 = (MethodInterceptor)((Callback[])var10000)[0];
}
}
public Object newInstance(Callback[] var1) {
CGLIB$SET_THREAD_CALLBACKS(var1);
ExecutionTimeReality$$EnhancerByCGLIB$$5a92c386 var10000 = new ExecutionTimeReality$$EnhancerByCGLIB$$5a92c386();
CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
return var10000;
}
public Object newInstance(Callback var1) {
CGLIB$SET_THREAD_CALLBACKS(new Callback[]{var1});
ExecutionTimeReality$$EnhancerByCGLIB$$5a92c386 var10000 = new ExecutionTimeReality$$EnhancerByCGLIB$$5a92c386();
CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
return var10000;
}
public Object newInstance(Class[] var1, Object[] var2, Callback[] var3) {
CGLIB$SET_THREAD_CALLBACKS(var3);
ExecutionTimeReality$$EnhancerByCGLIB$$5a92c386 var10000 = new ExecutionTimeReality$$EnhancerByCGLIB$$5a92c386;
switch(var1.length) {
case 0:
var10000.<init>();
CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
return var10000;
default:
throw new IllegalArgumentException("Constructor not found");
}
}
public Callback getCallback(int var1) {
CGLIB$BIND_CALLBACKS(this);
MethodInterceptor var10000;
switch(var1) {
case 0:
var10000 = this.CGLIB$CALLBACK_0;
break;
default:
var10000 = null;
}
return var10000;
}
public void setCallback(int var1, Callback var2) {
switch(var1) {
case 0:
this.CGLIB$CALLBACK_0 = (MethodInterceptor)var2;
default:
}
}
public Callback[] getCallbacks() {
CGLIB$BIND_CALLBACKS(this);
return new Callback[]{this.CGLIB$CALLBACK_0};
}
public void setCallbacks(Callback[] var1) {
this.CGLIB$CALLBACK_0 = (MethodInterceptor)var1[0];
}
static {
CGLIB$STATICHOOK1();
}
}
我们通过代理类的源码可以看到,代理类会获得所有在父类继承来的方法,并且会有MethodProxy与之对应,比如:
private static final Method CGLIB$dealTask$0$Method;
private static final MethodProxy CGLIB$dealTask$0$Proxy;
private static final Method CGLIB$sayHello$1$Method;
private static final MethodProxy CGLIB$sayHello$1$Proxy;
另外,从代理类的类型来看,它直接继承要代理的类,所以可以代理的没有接口的类。其实,它跟接口没关系了,有接口也不会用到。
public class ExecutionTimeReality$$EnhancerByCGLIB$$5a92c386 extends ExecutionTimeReality implements Factory
方法的调用
final void CGLIB$dealTask$0(String var1) {
super.dealTask(var1);
}
public final void dealTask(String var1) {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
if (var10000 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_0;
}
if (var10000 != null) {
var10000.intercept(this, CGLIB$dealTask$0$Method, new Object[]{var1}, CGLIB$dealTask$0$Proxy);
} else {
super.dealTask(var1);
}
}
FastClass机制
Cglib动态代理执行代理方法效率之所以比JDK的高是因为Cglib采用了FastClass机制,它的原理简单来说就是:为代理类和被代理类各生成一个Class,这个Class会为代理类或被代理类的方法分配一个index(int类型)。这个index当做一个入参,FastClass就可以直接定位要调用的方法直接进行调用,这样省去了反射调用,所以调用效率比JDK动态代理通过反射调用高。
ExecutionTimeReality$FastClassByCGLIB$81389d66
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.example.a;
import java.lang.reflect.InvocationTargetException;
import net.sf.cglib.core.Signature;
import net.sf.cglib.reflect.FastClass;
public class ExecutionTimeReality$$FastClassByCGLIB$$81389d66 extends FastClass {
public ExecutionTimeReality$$FastClassByCGLIB$$81389d66(Class var1) {
super(var1);
}
public int getIndex(Signature var1) {
String var10000 = var1.toString();
switch(var10000.hashCode()) {
case -1165719922:
if (var10000.equals("dealTask(Ljava/lang/String;)V")) {
return 0;
}
break;
case 771401912:
if (var10000.equals("sayHello(Ljava/lang/String;)V")) {
return 1;
}
break;
case 1826985398:
if (var10000.equals("equals(Ljava/lang/Object;)Z")) {
return 2;
}
break;
case 1913648695:
if (var10000.equals("toString()Ljava/lang/String;")) {
return 3;
}
break;
case 1984935277:
if (var10000.equals("hashCode()I")) {
return 4;
}
}
return -1;
}
public int getIndex(String var1, Class[] var2) {
switch(var1.hashCode()) {
case -2012993625:
if (var1.equals("sayHello")) {
switch(var2.length) {
case 1:
if (var2[0].getName().equals("java.lang.String")) {
return 1;
}
}
}
break;
case -1776922004:
if (var1.equals("toString")) {
switch(var2.length) {
case 0:
return 3;
}
}
break;
case -1295482945:
if (var1.equals("equals")) {
switch(var2.length) {
case 1:
if (var2[0].getName().equals("java.lang.Object")) {
return 2;
}
}
}
break;
case 147696667:
if (var1.equals("hashCode")) {
switch(var2.length) {
case 0:
return 4;
}
}
break;
case 510300177:
if (var1.equals("dealTask")) {
switch(var2.length) {
case 1:
if (var2[0].getName().equals("java.lang.String")) {
return 0;
}
}
}
}
return -1;
}
public int getIndex(Class[] var1) {
switch(var1.length) {
case 0:
return 0;
default:
return -1;
}
}
public Object invoke(int var1, Object var2, Object[] var3) throws InvocationTargetException {
ExecutionTimeReality var10000 = (ExecutionTimeReality)var2;
int var10001 = var1;
try {
switch(var10001) {
case 0:
var10000.dealTask((String)var3[0]);
return null;
case 1:
var10000.sayHello((String)var3[0]);
return null;
case 2:
return new Boolean(var10000.equals(var3[0]));
case 3:
return var10000.toString();
case 4:
return new Integer(var10000.hashCode());
}
} catch (Throwable var4) {
throw new InvocationTargetException(var4);
}
throw new IllegalArgumentException("Cannot find matching method/constructor");
}
public Object newInstance(int var1, Object[] var2) throws InvocationTargetException {
ExecutionTimeReality var10000 = new ExecutionTimeReality;
ExecutionTimeReality var10001 = var10000;
int var10002 = var1;
try {
switch(var10002) {
case 0:
var10001.<init>();
return var10000;
}
} catch (Throwable var3) {
throw new InvocationTargetException(var3);
}
throw new IllegalArgumentException("Cannot find matching method/constructor");
}
public int getMaxIndex() {
return 4;
}
}
还没有评论,来说两句吧...