代理模式说明与特点
代理模式给某一个对象提供一个代理对象,并由代理对象控制对原对象的引用。
在有些情况下,一个客户不能或者不想直接访问另一个对象,这时需要找一个中介帮忙完成某项任务,这个中介就是代理对象。例如,购买火车票不一定要去火车站买,可以通过 12306 网站或者去火车票代售点买。又如找女朋友、找保姆、找工作等都可以通过找中介完成。
代理模式结构
-
类图
- 对应角色
- 抽象对象角色(AbstractObject): 声明了目标对象和代理对象的共同接口。
- 目标对象角色(RealObject): 定义了代理对象所代表的目标对象。
- 代理对象角色(ProxyObject): 代理对象内部含有目标对象的引用,从而可以在任何时候操作目标对象;
代码演示
代理模式又分为静态代理和动态代理 ,首先我们先看一下静态代理的实现。
静态代理演示
- 抽象对象角色
/**
* 抽象对象角色
*
* @author hui.wang
* @since 30 November 2018
*/
public interface Moveable {
void move();
}
- 目标对象角色
/**
* 目标对象角色
*
* @author hui.wang
* @since 30 November 2018
*/
public class Tank implements Moveable {
@Override
public void move() {
System.out.println("moving......");
}
}
- 代理对象角色
/**
* 代理对象角色,使用聚合实现
*
* @author hui.wang
* @since 30 November 2018
*/
public class TankTwo implements Moveable{
private Moveable moveable;
public TankTwo(Moveable moveable) {
this.moveable = moveable;
}
@Override
public void move() {
System.out.println("聚合实现");
moveable.move();
System.out.println("聚合实现");
}
}
测试
//目标对象
Tank target = new Tank();
//代理对象
TankTwo proxy = new TankTwo(target);
proxy.move();
输出
聚合实现
moving......
聚合实现
- 静态代理代码比较简单,使用简单的聚合方式,将目标对象聚合到代理对象中。
- 缺点:因为代理对象需要与目标对象实现一样的接口,所以会有很多代理类,类太多。同时,一旦接口增加方法,目标对象与代理对象都要维护。耦合太强
动态代理
接下来我们看一下动态代理代码,这里主要演示一下JDK
自带的动态代理和CgLib
的动态代理
JDK动态代理
使用JDK
动态代理一共分三步:
- 定义一个抽象接口(抽象对象)
- 实现这个抽象接口(目标对象)
- 定义一个代理对象并实现
java.lang.reflect.InvocationHandler
接口(代理对象)
代码如下:
/**
* 抽象接口
*
* @author hui.wang
* @since 14 May 2019
*/
public interface Service {
void say();
}
/**
* 目标对象
*
* @author hui.wang
* @since 14 May 2019
*/
public class ServiceImpl implements Service {
@Override
public void say() {
System.out.println("hello world");
}
}
/**
* 代理对象,实现 {@link InvocationHandler}接口
*
* @author hui.wang
* @since 14 May 2019
*/
public class ServiceProxy implements InvocationHandler {
/**
* 代理的目标对象
*/
private Object target;
public ServiceProxy(Object target) {
this.target = target;
}
/**
* 获取代理对象
*/
public <T> T getProxy() {
return (T) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("before ....");
Object result = method.invoke(target, args);
System.out.println("after ...");
return result;
}
}
测试
/**
* 设置 sun.misc.ProxyGenerator.saveGeneratedFiles 为true,将生成的代理保存
*/
System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
Service service = new ServiceProxy(new ServiceImpl()).getProxy();
service.say();
打印结果:
before ....
hello world
after ...
使用JDK
自带代理已经演示完了,这你需要注意,在测试的时候我把sun.misc.ProxyGenerator.saveGeneratedFiles
属性设置了true了,运行完程序,会将生成的代理对象的class
文件保存到你本地磁盘。
CgLib动态代理
CgLib动态代理可以不用抽象出抽象对象角色,可以直接生成目标对象的代理类。
代码如下:
/**
* 目标对象角色
*
* @author hui.wang
* @since 30 November 2018
*/
public class Tank{
public void move() {
System.out.println("moving......");
}
}
/**
* 动态代理对象, 实现 {@link MethodInterceptor}接口
*
* @author hui.wang
* @since 30 November 2018
*/
public class TankProxy implements MethodInterceptor{
/**
* 目标对象
*/
private Object target;
public TankProxy(Object target) {
this.target = target;
}
/**
* 获取代理对象
*/
public Object getProxyInstance() {
//1.工具类
Enhancer enhancer = new Enhancer();
//2.设置父类
enhancer.setSuperclass(target.getClass());
//3.设置回调函数
enhancer.setCallback(this);
//4.创建子类(代理对象)
return enhancer.create();
}
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
System.out.println("cglib proxy start.......");
//执行目标对象的方法
Object value = method.invoke(target, objects);
System.out.println("cglib proxy end.......");
return value;
}
}
测试
Tank target = new Tank();
Tank proxy = (Tank) new TankProxy(target).getProxyInstance();
proxy.move();
打印结果:
cglib proxy start.......
moving......
cglib proxy end.......
讲到这里动态代理的实现基本上是讲完了。接下来我们看一下JDK
和 CgLib
动态的实现原理。
JDK动态代理实现原理
继续上面JDK
动态代理的代码,我们分别定义了三个类,分别是
- 定义一个抽象接口(抽象对象):
Service
- 实现这个抽象接口(目标对象):
ServiceImpl
- 定义一个代理对象并实现:(代理对象) :
ServiceProxy
测试的时候,我们设置了System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true")
,然后我们发现目录里面多了一个$Proxy0
,反编译后代码如下:
package com.sun.proxy;
import com.hui.wang.jdk.learn.proxy.Service;
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 Service {
private static Method m1;
private static Method m2;
private static Method m3;
private static Method m0;
public $Proxy0(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 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 void say() throws {
try {
super.h.invoke(this, m3, (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);
}
}
static {
try {
m1 = Class.forName("java.lang.Object").getMethod("equals", Class.forName("java.lang.Object"));
m2 = Class.forName("java.lang.Object").getMethod("toString");
m3 = Class.forName("com.hui.wang.jdk.learn.proxy.Service").getMethod("say");
m0 = Class.forName("java.lang.Object").getMethod("hashCode");
} catch (NoSuchMethodException var2) {
throw new NoSuchMethodError(var2.getMessage());
} catch (ClassNotFoundException var3) {
throw new NoClassDefFoundError(var3.getMessage());
}
}
}
OK ,看到这段代码,估计你已经知道JDK的实现了。JDK帮我们生成了一个$Proxy0
类,然后这个类代理了目标对象。下面我们先看一下JDK
动态代理调用过程。
测试的时候,我们调用了这段代码
Service service = new ServiceProxy(new ServiceImpl()).getProxy();
service.say();
这里调用say()
方法就是调用生成的代理类$Proxy0
的say()
方法:
public final void say() throws {
try {
super.h.invoke(this, m3, (Object[])null);
} catch (RuntimeException | Error var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
}
这里会调用super.h.invoke(this, m3, (Object[])null);
这里的super.h
是父类的成员变量,这个变量的赋值在代理类$Proxy0
创建的时候赋值的
public $Proxy0(InvocationHandler var1) throws {
super(var1);
}
这里的var1
值就是我们的自己创建的代理类ServiceProxy
自己:
/**
* 获取代理对象
*/
public <T> T getProxy() {
return (T) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
}
继续回到super.h.invoke(this, m3, (Object[])null);
这句,其中super.h
就是ServiceProxy
实例,因此super.h.invoke(this, m3, (Object[])null);
执行的就是:
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("before ....");
Object result = method.invoke(target, args);
System.out.println("after ...");
return result;
}
所以整个代理过程就很清楚了。
接下来我们看一下细节实现。我们先从ServiceProxy
这个类开始。动态代理对象的生成是在return (T) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
这句话上面。我们点击去看一下。
/**
* @param loader 类加载器
* @param interfaces 目标对象实现的接口
* @param h InvocationHandler 实现类
*/
@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 对象
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<Void>() {
public Void run() {
// 设置 accessible
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);
}
}
代码也比较简单,先生成代理对象的class,然后用反射创建代理对象的实例。生成代理对象的class 对象主要在getProxyClass0
方法里面,我们进去看看。
/**
* 生成代理对象的class对象
*/
private static Class<?> getProxyClass0(ClassLoader loader,
Class<?>... interfaces) {
// 目标对象实现的接口不能超过 65535 个
if (interfaces.length > 65535) {
throw new IllegalArgumentException("interface limit exceeded");
}
// If the proxy class defined by the given loader implementing
// the given interfaces exists, this will simply return the cached copy;
// otherwise, it will create the proxy class via the ProxyClassFactory
return proxyClassCache.get(loader, interfaces);
}
这里逻辑很简单,所有的逻辑都在proxyClassCache.get(loader, interfaces);
里面。这里使用了WeakCache
缓存,当WeakCache
中没有缓存相应接口的代理类,则会调用ProxyClassFactory
类的apply
方法来创建代理类。我们看一下:
/**
* A factory function that generates, defines and returns the proxy class given
* the ClassLoader and array of interfaces.
*/
private static final class ProxyClassFactory
implements BiFunction<ClassLoader, Class<?>[], Class<?>>
{
// 代理类名字的前缀
private static final String proxyClassNamePrefix = "$Proxy";
// next number to use for generation of unique proxy class names
// 生成代理类的计数器
private static final AtomicLong nextUniqueNumber = new AtomicLong();
@Override
public Class<?> apply(ClassLoader loader, Class<?>[] interfaces) {
Map<Class<?>, Boolean> interfaceSet = new IdentityHashMap<>(interfaces.length);
// 接口的校验
for (Class<?> intf : interfaces) {
/**
* 校验类加载器是否能通过接口名称加载该类
*/
Class<?> interfaceClass = null;
try {
interfaceClass = Class.forName(intf.getName(), false, loader);
} catch (ClassNotFoundException e) {
}
if (interfaceClass != intf) {
throw new IllegalArgumentException(
intf + " is not visible from class loader");
}
/**
* 校验该类是否是接口类型
*/
if (!interfaceClass.isInterface()) {
throw new IllegalArgumentException(
interfaceClass.getName() + " is not an interface");
}
/**
* 校验接口是否重复
*/
if (interfaceSet.put(interfaceClass, Boolean.TRUE) != null) {
throw new IllegalArgumentException(
"repeated interface: " + interfaceClass.getName());
}
}
// 代理包名
String proxyPkg = null;
int accessFlags = Modifier.PUBLIC | Modifier.FINAL;
/**
* 非public接口,代理类的包名与接口的包名相同
*/
for (Class<?> intf : interfaces) {
int flags = intf.getModifiers();
if (!Modifier.isPublic(flags)) {
accessFlags = Modifier.FINAL;
String name = intf.getName();
int n = name.lastIndexOf('.');
String pkg = ((n == -1) ? "" : name.substring(0, n + 1));
if (proxyPkg == null) {
proxyPkg = pkg;
} else if (!pkg.equals(proxyPkg)) {
throw new IllegalArgumentException(
"non-public interfaces from different packages");
}
}
}
if (proxyPkg == null) {
// public代理接口,使用com.sun.proxy包名
proxyPkg = ReflectUtil.PROXY_PACKAGE + ".";
}
/**
* 生成代理类的名字
*/
long num = nextUniqueNumber.getAndIncrement();
String proxyName = proxyPkg + proxyClassNamePrefix + num;
/*
* 生成的代理类的字节码文件
*/
byte[] proxyClassFile = ProxyGenerator.generateProxyClass(
proxyName, interfaces, accessFlags);
try {
// 使用类加载器将代理类的字节码文件加载到JVM中
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());
}
}
}
到这里基本上讲完了JDK
动态代理的过程了。
CgLib动态代理实现原理
这里我们重新过一下代码
- 目标对象代码:
/**
* 目标对象角色
*
* @author hui.wang
* @since 30 November 2018
*/
public class Tank{
public void move() {
System.out.println("moving......");
}
/**
* 这里我加了一个 final 方法
*/
public final void finalMethod() {
System.out.println("final method....");
}
}
- 代理对象:
/**
* 动态代理对象, 实现 {@link MethodInterceptor}接口
*
* @author hui.wang
* @since 30 November 2018
*/
public class TankProxy implements MethodInterceptor {
/**
* 目标对象
*/
private Object target;
public TankProxy(Object target) {
this.target = target;
}
/**
* 获取代理对象
*/
public Object getProxyInstance() {
//1.工具类
Enhancer enhancer = new Enhancer();
//2.设置父类
enhancer.setSuperclass(target.getClass());
//3.设置回调函数
enhancer.setCallback(this);
//4.创建子类(代理对象)
return enhancer.create();
}
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
System.out.println("cglib proxy start.......");
//执行目标对象的方法
Object value = method.invoke(target, objects);
System.out.println("cglib proxy end.......");
return value;
}
}
代理对象实现了MethodInterceptor
接口。
- 测试类
System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "/Users/learn/design-pattern");
Tank target = new Tank();
Tank proxy = (Tank) new TankProxy(target).getProxyInstance();
proxy.move();
proxy.finalMethod();
System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "/Users/learn/design-pattern");
这句话是将CgLib产生的代理对象保存到本地磁盘。
- 打印结果:
cglib proxy start.......
moving......
cglib proxy end.......
final method....
这里可以看到final
修饰的方法没有被代理。
本地磁盘会保存一个class文件,反编译代码如下:
public class Tank$$EnhancerByCGLIB$$46c52377 extends Tank 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$move$0$Method;
private static final MethodProxy CGLIB$move$0$Proxy;
private static final Object[] CGLIB$emptyArgs;
private static final Method CGLIB$equals$1$Method;
private static final MethodProxy CGLIB$equals$1$Proxy;
private static final Method CGLIB$toString$2$Method;
private static final MethodProxy CGLIB$toString$2$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;
static void CGLIB$STATICHOOK1() {
CGLIB$THREAD_CALLBACKS = new ThreadLocal();
CGLIB$emptyArgs = new Object[0];
Class var0 = Class.forName("com.hui.wang.design.pattern.v3.Tank$$EnhancerByCGLIB$$46c52377");
Class var1;
CGLIB$move$0$Method = ReflectUtils.findMethods(new String[]{"move", "()V"}, (var1 = Class.forName("com.hui.wang.design.pattern.v3.Tank")).getDeclaredMethods())[0];
CGLIB$move$0$Proxy = MethodProxy.create(var1, var0, "()V", "move", "CGLIB$move$0");
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$1$Method = var10000[0];
CGLIB$equals$1$Proxy = MethodProxy.create(var1, var0, "(Ljava/lang/Object;)Z", "equals", "CGLIB$equals$1");
CGLIB$toString$2$Method = var10000[1];
CGLIB$toString$2$Proxy = MethodProxy.create(var1, var0, "()Ljava/lang/String;", "toString", "CGLIB$toString$2");
CGLIB$hashCode$3$Method = var10000[2];
CGLIB$hashCode$3$Proxy = MethodProxy.create(var1, var0, "()I", "hashCode", "CGLIB$hashCode$3");
CGLIB$clone$4$Method = var10000[3];
CGLIB$clone$4$Proxy = MethodProxy.create(var1, var0, "()Ljava/lang/Object;", "clone", "CGLIB$clone$4");
}
final void CGLIB$move$0() {
super.move();
}
public final void move() {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
if (this.CGLIB$CALLBACK_0 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_0;
}
if (var10000 != null) {
var10000.intercept(this, CGLIB$move$0$Method, CGLIB$emptyArgs, CGLIB$move$0$Proxy);
} else {
super.move();
}
}
final boolean CGLIB$equals$1(Object var1) {
return super.equals(var1);
}
public final boolean equals(Object var1) {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
if (this.CGLIB$CALLBACK_0 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_0;
}
if (var10000 != null) {
Object var2 = var10000.intercept(this, CGLIB$equals$1$Method, new Object[]{var1}, CGLIB$equals$1$Proxy);
return var2 == null ? false : (Boolean)var2;
} else {
return super.equals(var1);
}
}
final String CGLIB$toString$2() {
return super.toString();
}
public final String toString() {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
if (this.CGLIB$CALLBACK_0 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_0;
}
return var10000 != null ? (String)var10000.intercept(this, CGLIB$toString$2$Method, CGLIB$emptyArgs, CGLIB$toString$2$Proxy) : super.toString();
}
final int CGLIB$hashCode$3() {
return super.hashCode();
}
public final int hashCode() {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
if (this.CGLIB$CALLBACK_0 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_0;
}
if (var10000 != null) {
Object var1 = var10000.intercept(this, CGLIB$hashCode$3$Method, CGLIB$emptyArgs, CGLIB$hashCode$3$Proxy);
return var1 == null ? 0 : ((Number)var1).intValue();
} else {
return super.hashCode();
}
}
final Object CGLIB$clone$4() throws CloneNotSupportedException {
return super.clone();
}
protected final Object clone() throws CloneNotSupportedException {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
if (this.CGLIB$CALLBACK_0 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_0;
}
return var10000 != null ? var10000.intercept(this, CGLIB$clone$4$Method, CGLIB$emptyArgs, CGLIB$clone$4$Proxy) : super.clone();
}
public static MethodProxy CGLIB$findMethodProxy(Signature var0) {
String var10000 = var0.toString();
switch(var10000.hashCode()) {
case -508378822:
if (var10000.equals("clone()Ljava/lang/Object;")) {
return CGLIB$clone$4$Proxy;
}
break;
case 1243513348:
if (var10000.equals("move()V")) {
return CGLIB$move$0$Proxy;
}
break;
case 1826985398:
if (var10000.equals("equals(Ljava/lang/Object;)Z")) {
return CGLIB$equals$1$Proxy;
}
break;
case 1913648695:
if (var10000.equals("toString()Ljava/lang/String;")) {
return CGLIB$toString$2$Proxy;
}
break;
case 1984935277:
if (var10000.equals("hashCode()I")) {
return CGLIB$hashCode$3$Proxy;
}
}
return null;
}
public Tank$$EnhancerByCGLIB$$46c52377() {
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) {
Tank$$EnhancerByCGLIB$$46c52377 var1 = (Tank$$EnhancerByCGLIB$$46c52377)var0;
if (!var1.CGLIB$BOUND) {
var1.CGLIB$BOUND = true;
Object var10000 = CGLIB$THREAD_CALLBACKS.get();
if (var10000 == null) {
var10000 = CGLIB$STATIC_CALLBACKS;
if (CGLIB$STATIC_CALLBACKS == null) {
return;
}
}
var1.CGLIB$CALLBACK_0 = (MethodInterceptor)((Callback[])var10000)[0];
}
}
public Object newInstance(Callback[] var1) {
CGLIB$SET_THREAD_CALLBACKS(var1);
Tank$$EnhancerByCGLIB$$46c52377 var10000 = new Tank$$EnhancerByCGLIB$$46c52377();
CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
return var10000;
}
public Object newInstance(Callback var1) {
CGLIB$SET_THREAD_CALLBACKS(new Callback[]{var1});
Tank$$EnhancerByCGLIB$$46c52377 var10000 = new Tank$$EnhancerByCGLIB$$46c52377();
CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
return var10000;
}
public Object newInstance(Class[] var1, Object[] var2, Callback[] var3) {
CGLIB$SET_THREAD_CALLBACKS(var3);
Tank$$EnhancerByCGLIB$$46c52377 var10000 = new Tank$$EnhancerByCGLIB$$46c52377;
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();
}
}
代码较长,我们简单说一下。看这段代码,我们可以知道
-
CgLib
是使用继承的方式实现的代理,和JDK
不一样,不需要抽象出来interface
。 - 如果方法是
final
类型,将不会被代码。
接下来我们分析一下CgLib生成出来的这个类。
// 静态代码块,首先会被执行
static {
CGLIB$STATICHOOK1();
}
// 被静态代码块调用
static void CGLIB$STATICHOOK1() {
// 存放 ThreadLocal
CGLIB$THREAD_CALLBACKS = new ThreadLocal();
// 空参数
CGLIB$emptyArgs = new Object[0];
// 代理的 class 对象
Class var0 = Class.forName("com.hui.wang.design.pattern.v3.Tank$$EnhancerByCGLIB$$46c52377");
// 被代理的类 (var1 = Class.forName("com.hui.wang.design.pattern.v3.Tank"))
Class var1;
// 父类原始方法
CGLIB$move$0$Method = ReflectUtils.findMethods(new String[]{"move", "()V"}, (var1 = Class.forName("com.hui.wang.design.pattern.v3.Tank")).getDeclaredMethods())[0];
// 被代理的父类方法
CGLIB$move$0$Proxy = MethodProxy.create(var1, var0, "()V", "move", "CGLIB$move$0");
// Object 的原始方法集合
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());
// equals 原始方法
CGLIB$equals$1$Method = var10000[0];
// toString 代理方法
CGLIB$equals$1$Proxy = MethodProxy.create(var1, var0, "(Ljava/lang/Object;)Z", "equals", "CGLIB$equals$1");
// toString 原始方法
CGLIB$toString$2$Method = var10000[1];
// toString 代理方法
CGLIB$toString$2$Proxy = MethodProxy.create(var1, var0, "()Ljava/lang/String;", "toString", "CGLIB$toString$2");
// hashCode 原始方法
CGLIB$hashCode$3$Method = var10000[2];
// hashCode 代理方法
CGLIB$hashCode$3$Proxy = MethodProxy.create(var1, var0, "()I", "hashCode", "CGLIB$hashCode$3");
// clone 原始方法
CGLIB$clone$4$Method = var10000[3];
// clone 代理方法
CGLIB$clone$4$Proxy = MethodProxy.create(var1, var0, "()Ljava/lang/Object;", "clone", "CGLIB$clone$4");
}
// =================== 变量,在上个方法中赋值========== //
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$move$0$Method;
private static final MethodProxy CGLIB$move$0$Proxy;
private static final Object[] CGLIB$emptyArgs;
private static final Method CGLIB$equals$1$Method;
private static final MethodProxy CGLIB$equals$1$Proxy;
private static final Method CGLIB$toString$2$Method;
private static final MethodProxy CGLIB$toString$2$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;
我们通过代理类的源码可以看到,代理类会获得所有在父类继承来的方法,并且会有MethodProxy
与之对应,比如CGLIB$equals$1$Method
、CGLIB$equals$1$Proxy
;
接下来我们看一下proxy.move();
方法的调用:
public final void move() {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
if (this.CGLIB$CALLBACK_0 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_0;
}
if (var10000 != null) {
var10000.intercept(this, CGLIB$move$0$Method, CGLIB$emptyArgs, CGLIB$move$0$Proxy);
} else {
super.move();
}
}
这里就调用MethodInterceptor
的intercept
方法,这里的MethodInterceptor
就是TankProxy
对象,所以就调用到了
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
System.out.println("cglib proxy start.......");
//执行目标对象的方法
Object value = method.invoke(target, objects);
System.out.println("cglib proxy end.......");
return value;
}
这就是整个调用过程,需要说明一下methodProxy
就是父类的原始方法。