1. 工具使用:HSDB
java -classpath "%JAVA_HOME%/lib/sa-jdi.jar" sun.jvm.hotspot.HSDB
2. 测试代码:(对Subject中的方法增强打印开始时间和结束时间)
public class TestEnhancer {
public static class Subject {
public void sayHi() {
System.out.println("Hi...");
}
}
public static void main(String[] args) {
RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
System.out.println("pid="+runtimeMXBean.getName().split("@")[0]);
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(Subject.class);
enhancer.setCallback(new MethodInterceptor() {
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
// obj为代理后对象 method的被代理对象的方法
methodProxy.getSuperIndex();//为了调用MethodProxy.init()初始化FastClassInfo
//TODO 2:MethodProxy中内部类FastClassInfo
System.out.println("开始时间:"+System.currentTimeMillis());
Object invokeSuper = methodProxy.invokeSuper(obj, args);////TODO 3:MethodProxy#invokeSuper
System.out.println("结束时间:"+System.currentTimeMillis());
//Object invoke = methodProxy.invoke(obj, args);//TODO 4:MethodProxy#invoke
return invokeSuper;
}
});
Subject subject = (Subject)enhancer.create();
//TODO 1:enhancer.create()生成的类
subject.sayHi();
}
}
在 Enhancer 使用过程我们自定组装callbacks
enhancer.setCallbackTypes(types)
enhancer.setCallbacks(callbacks)
enhancer.setCallbackFilter() CallbackFilter中的accept通过method的类型返回callbacks位置 从0 开始
pid=8880
开始时间:1543369816360
Hi...
结束时间:1543369816360
3. 断点获取如下信息:
subject.getClass().getSimpleName()=TestEnhancer$Subject$$EnhancerByCGLIB$$842aea1f
methodProxy.fastClassInfo.f1.getClass().getSimpleName()=TestEnhancer$Subject$$FastClassByCGLIB$$d29eb9b3
methodProxy.fastClassInfo.i1=0
methodProxy.fastClassInfo.f2.getClass().getSimpleName()=TestEnhancer$Subject$$EnhancerByCGLIB$$842aea1f$$FastClassByCGLIB$$eb17910f
methodProxy.fastClassInfo.i2=18
4. 使用HSDB查看当前内存中的以TestEnhancer$Subject开头的类,并将class字节码保存
动态代理Cglib原理(图一).png
4.1 反编译:TestEnhancer&Subject&&EnhancerByCGLIB&&842aea1f
import java.lang.reflect.Method;
import org.assertj.core.internal.cglib.core.ReflectUtils;
import org.assertj.core.internal.cglib.core.Signature;
import org.assertj.core.internal.cglib.proxy.Callback;
import org.assertj.core.internal.cglib.proxy.Factory;
import org.assertj.core.internal.cglib.proxy.MethodInterceptor;
import org.assertj.core.internal.cglib.proxy.MethodProxy;
import zk.curator.TestEnhancer.Subject;
public class TestEnhancer$Subject$$EnhancerByCGLIB$$842aea1f extends Subject 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$sayHi$0$Method;
private static final MethodProxy CGLIB$sayHi$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;
public TestEnhancer$Subject$$EnhancerByCGLIB$$842aea1f() {
CGLIB$BIND_CALLBACKS(this);
}
static {
CGLIB$STATICHOOK1();
}
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).booleanValue();
} else {
return super.equals(var1);
}
}
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();
}
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();
}
}
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 Object newInstance(Callback var1) {
CGLIB$SET_THREAD_CALLBACKS(new Callback[]{var1});
TestEnhancer$Subject$$EnhancerByCGLIB$$842aea1f var10000 = new TestEnhancer$Subject$$EnhancerByCGLIB$$842aea1f();
CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
return var10000;
}
public Object newInstance(Callback[] var1) {
CGLIB$SET_THREAD_CALLBACKS(var1);
TestEnhancer$Subject$$EnhancerByCGLIB$$842aea1f var10000 = new TestEnhancer$Subject$$EnhancerByCGLIB$$842aea1f();
CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
return var10000;
}
public Object newInstance(Class[] var1, Object[] var2, Callback[] var3) {
CGLIB$SET_THREAD_CALLBACKS(var3);
TestEnhancer$Subject$$EnhancerByCGLIB$$842aea1f var10000 = new TestEnhancer$Subject$$EnhancerByCGLIB$$842aea1f;
switch(var1.length) {
case 0:
var10000.<init>();
CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
return var10000;
default:
throw new IllegalArgumentException("Constructor not found");
}
}
public void setCallback(int var1, Callback var2) {
switch(var1) {
case 0:
this.CGLIB$CALLBACK_0 = (MethodInterceptor)var2;
default:
}
}
public final void sayHi() {
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$sayHi$0$Method, CGLIB$emptyArgs, CGLIB$sayHi$0$Proxy);
} else {
super.sayHi();
}
}
public static void CGLIB$SET_STATIC_CALLBACKS(Callback[] var0) {
CGLIB$STATIC_CALLBACKS = var0;
}
public void setCallbacks(Callback[] var1) {
this.CGLIB$CALLBACK_0 = (MethodInterceptor)var1[0];
}
public static void CGLIB$SET_THREAD_CALLBACKS(Callback[] var0) {
CGLIB$THREAD_CALLBACKS.set(var0);
}
public static MethodProxy CGLIB$findMethodProxy(Signature var0) {
String var10000 = var0.toString();
switch(var10000.hashCode()) {
case -2012941911:
if(var10000.equals("sayHi()V")) {
return CGLIB$sayHi$0$Proxy;
}
break;
case -508378822:
if(var10000.equals("clone()Ljava/lang/Object;")) {
return CGLIB$clone$4$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;
}
final String CGLIB$toString$2() {
return super.toString();
}
final int CGLIB$hashCode$3() {
return super.hashCode();
}
final Object CGLIB$clone$4() throws CloneNotSupportedException {
return super.clone();
}
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 Callback[] getCallbacks() {
CGLIB$BIND_CALLBACKS(this);
return new Callback[]{this.CGLIB$CALLBACK_0};
}
final void CGLIB$sayHi$0() {
super.sayHi();
}
final boolean CGLIB$equals$1(Object var1) {
return super.equals(var1);
}
static void CGLIB$STATICHOOK1() {
CGLIB$THREAD_CALLBACKS = new ThreadLocal();
CGLIB$emptyArgs = new Object[0];
Class var0 = Class.forName("zk.curator.TestEnhancer$Subject$$EnhancerByCGLIB$$842aea1f");
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$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");
CGLIB$sayHi$0$Method = ReflectUtils.findMethods(new String[]{"sayHi", "()V"}, (var1 = Class.forName("zk.curator.TestEnhancer$Subject")).getDeclaredMethods())[0];
CGLIB$sayHi$0$Proxy = MethodProxy.create(var1, var0, "()V", "sayHi", "CGLIB$sayHi$0");
}
private static final void CGLIB$BIND_CALLBACKS(Object var0) {
TestEnhancer$Subject$$EnhancerByCGLIB$$842aea1f var1 = (TestEnhancer$Subject$$EnhancerByCGLIB$$842aea1f)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];
}
}
}
4.2 反编译:TestEnhancer&Subject&&FastClassByCGLIB&&d29eb9b3
import java.lang.reflect.InvocationTargetException;
import org.assertj.core.internal.cglib.core.Signature;
import org.assertj.core.internal.cglib.reflect.FastClass;
import zk.curator.TestEnhancer.Subject;
public class TestEnhancer$Subject$$FastClassByCGLIB$$d29eb9b3 extends FastClass {
public Object invoke(int var1, Object var2, Object[] var3) throws InvocationTargetException {
Subject var10000 = (Subject)var2;
int var10001 = var1;
try {
switch(var10001) {
case 0:
var10000.sayHi();
return null;
case 1:
return new Boolean(var10000.equals(var3[0]));
case 2:
return var10000.toString();
case 3:
return new Integer(var10000.hashCode());
}
} catch (Throwable var4) {
throw new InvocationTargetException(var4);
}
throw new IllegalArgumentException("Cannot find matching method/constructor");
}
public TestEnhancer$Subject$$FastClassByCGLIB$$d29eb9b3(Class var1) {
super(var1);
}
public Object newInstance(int var1, Object[] var2) throws InvocationTargetException {
Subject var10000 = new Subject;
Subject 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 getIndex(Class[] var1) {
switch(var1.length) {
case 0:
return 0;
default:
return -1;
}
}
public int getIndex(String var1, Class[] var2) {
switch(var1.hashCode()) {
case -1776922004:
if(var1.equals("toString")) {
switch(var2.length) {
case 0:
return 2;
}
}
break;
case -1295482945:
if(var1.equals("equals")) {
switch(var2.length) {
case 1:
if(var2[0].getName().equals("java.lang.Object")) {
return 1;
}
}
}
break;
case 109213260:
if(var1.equals("sayHi")) {
switch(var2.length) {
case 0:
return 0;
}
}
break;
case 147696667:
if(var1.equals("hashCode")) {
switch(var2.length) {
case 0:
return 3;
}
}
}
return -1;
}
public int getIndex(Signature var1) {
String var10000 = var1.toString();
switch(var10000.hashCode()) {
case -2012941911:
if(var10000.equals("sayHi()V")) {
return 0;
}
break;
case 1826985398:
if(var10000.equals("equals(Ljava/lang/Object;)Z")) {
return 1;
}
break;
case 1913648695:
if(var10000.equals("toString()Ljava/lang/String;")) {
return 2;
}
break;
case 1984935277:
if(var10000.equals("hashCode()I")) {
return 3;
}
}
return -1;
}
public int getMaxIndex() {
return 3;
}
}
4.3 反编译:TestEnhancer&Subject&&EnhancerByCGLIB&&842aea1f&&FastClassByCGLIB$$eb17910f
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package zk.curator;
import java.lang.reflect.InvocationTargetException;
import org.assertj.core.internal.cglib.core.Signature;
import org.assertj.core.internal.cglib.proxy.Callback;
import org.assertj.core.internal.cglib.reflect.FastClass;
import zk.curator.TestEnhancer.Subject..EnhancerByCGLIB..842aea1f;
public class TestEnhancer$Subject$$EnhancerByCGLIB$$842aea1f$$FastClassByCGLIB$$eb17910f extends FastClass {
public Object invoke(int var1, Object var2, Object[] var3) throws InvocationTargetException {
842aea1f var10000 = (842aea1f)var2;
int var10001 = var1;
try {
switch(var10001) {
case 0:
return new Boolean(var10000.equals(var3[0]));
case 1:
return var10000.toString();
case 2:
return new Integer(var10000.hashCode());
case 3:
return var10000.clone();
case 4:
return var10000.newInstance((Callback)var3[0]);
case 5:
return var10000.newInstance((Callback[])var3[0]);
case 6:
return var10000.newInstance((Class[])var3[0], (Object[])var3[1], (Callback[])var3[2]);
case 7:
var10000.setCallback(((Number)var3[0]).intValue(), (Callback)var3[1]);
return null;
case 8:
var10000.sayHi();
return null;
case 9:
842aea1f.CGLIB$SET_STATIC_CALLBACKS((Callback[])var3[0]);
return null;
case 10:
var10000.setCallbacks((Callback[])var3[0]);
return null;
case 11:
842aea1f.CGLIB$SET_THREAD_CALLBACKS((Callback[])var3[0]);
return null;
case 12:
return 842aea1f.CGLIB$findMethodProxy((Signature)var3[0]);
case 13:
return var10000.CGLIB$toString$2();
case 14:
return new Integer(var10000.CGLIB$hashCode$3());
case 15:
return var10000.CGLIB$clone$4();
case 16:
return var10000.getCallback(((Number)var3[0]).intValue());
case 17:
return var10000.getCallbacks();
case 18:
var10000.CGLIB$sayHi$0();
return null;
case 19:
return new Boolean(var10000.CGLIB$equals$1(var3[0]));
case 20:
842aea1f.CGLIB$STATICHOOK1();
return null;
}
} catch (Throwable var4) {
throw new InvocationTargetException(var4);
}
throw new IllegalArgumentException("Cannot find matching method/constructor");
}
public TestEnhancer$Subject$$EnhancerByCGLIB$$842aea1f$$FastClassByCGLIB$$eb17910f(Class var1) {
super(var1);
}
public Object newInstance(int var1, Object[] var2) throws InvocationTargetException {
842aea1f var10000 = new 842aea1f;
842aea1f 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 getIndex(Class[] var1) {
switch(var1.length) {
case 0:
return 0;
default:
return -1;
}
}
public int getIndex(String var1, Class[] var2) {
switch(var1.hashCode()) {
case -1776922004:
if(var1.equals("toString")) {
switch(var2.length) {
case 0:
return 1;
}
}
break;
case -1334646347:
if(var1.equals("CGLIB$sayHi$0")) {
switch(var2.length) {
case 0:
return 18;
}
}
break;
case -1295482945:
if(var1.equals("equals")) {
switch(var2.length) {
case 1:
if(var2[0].getName().equals("java.lang.Object")) {
return 0;
}
}
}
break;
case -1053468136:
if(var1.equals("getCallbacks")) {
switch(var2.length) {
case 0:
return 17;
}
}
break;
case -124978609:
if(var1.equals("CGLIB$equals$1")) {
switch(var2.length) {
case 1:
if(var2[0].getName().equals("java.lang.Object")) {
return 19;
}
}
}
break;
case -60403779:
if(var1.equals("CGLIB$SET_STATIC_CALLBACKS")) {
switch(var2.length) {
case 1:
if(var2[0].getName().equals("[Lorg.assertj.core.internal.cglib.proxy.Callback;")) {
return 9;
}
}
}
break;
case -29025555:
if(var1.equals("CGLIB$hashCode$3")) {
switch(var2.length) {
case 0:
return 14;
}
}
break;
case 85179481:
if(var1.equals("CGLIB$SET_THREAD_CALLBACKS")) {
switch(var2.length) {
case 1:
if(var2[0].getName().equals("[Lorg.assertj.core.internal.cglib.proxy.Callback;")) {
return 11;
}
}
}
break;
case 94756189:
if(var1.equals("clone")) {
switch(var2.length) {
case 0:
return 3;
}
}
break;
case 109213260:
if(var1.equals("sayHi")) {
switch(var2.length) {
case 0:
return 8;
}
}
break;
case 147696667:
if(var1.equals("hashCode")) {
switch(var2.length) {
case 0:
return 2;
}
}
break;
case 161998109:
if(var1.equals("CGLIB$STATICHOOK1")) {
switch(var2.length) {
case 0:
return 20;
}
}
break;
case 495524492:
if(var1.equals("setCallbacks")) {
switch(var2.length) {
case 1:
if(var2[0].getName().equals("[Lorg.assertj.core.internal.cglib.proxy.Callback;")) {
return 10;
}
}
}
break;
case 1154623345:
if(var1.equals("CGLIB$findMethodProxy")) {
switch(var2.length) {
case 1:
if(var2[0].getName().equals("org.assertj.core.internal.cglib.core.Signature")) {
return 12;
}
}
}
break;
case 1543336189:
if(var1.equals("CGLIB$toString$2")) {
switch(var2.length) {
case 0:
return 13;
}
}
break;
case 1811874389:
if(var1.equals("newInstance")) {
switch(var2.length) {
case 1:
String var10001 = var2[0].getName();
switch(var10001.hashCode()) {
case -1926028830:
if(var10001.equals("[Lorg.assertj.core.internal.cglib.proxy.Callback;")) {
return 5;
}
break;
case -615274520:
if(var10001.equals("org.assertj.core.internal.cglib.proxy.Callback")) {
return 4;
}
}
case 2:
default:
break;
case 3:
if(var2[0].getName().equals("[Ljava.lang.Class;") && var2[1].getName().equals("[Ljava.lang.Object;") && var2[2].getName().equals("[Lorg.assertj.core.internal.cglib.proxy.Callback;")) {
return 6;
}
}
}
break;
case 1817099975:
if(var1.equals("setCallback")) {
switch(var2.length) {
case 2:
if(var2[0].getName().equals("int") && var2[1].getName().equals("org.assertj.core.internal.cglib.proxy.Callback")) {
return 7;
}
}
}
break;
case 1905679803:
if(var1.equals("getCallback")) {
switch(var2.length) {
case 1:
if(var2[0].getName().equals("int")) {
return 16;
}
}
}
break;
case 1951977610:
if(var1.equals("CGLIB$clone$4")) {
switch(var2.length) {
case 0:
return 15;
}
}
}
return -1;
}
public int getIndex(Signature var1) {
String var10000 = var1.toString();
switch(var10000.hashCode()) {
case -2012941911:
if(var10000.equals("sayHi()V")) {
return 8;
}
break;
case -2011053341:
if(var10000.equals("newInstance([Ljava/lang/Class;[Ljava/lang/Object;[Lorg/assertj/core/internal/cglib/proxy/Callback;)Ljava/lang/Object;")) {
return 6;
}
break;
case -1937024608:
if(var10000.equals("CGLIB$sayHi$0()V")) {
return 18;
}
break;
case -1882565338:
if(var10000.equals("CGLIB$equals$1(Ljava/lang/Object;)Z")) {
return 19;
}
break;
case -1457535688:
if(var10000.equals("CGLIB$STATICHOOK1()V")) {
return 20;
}
break;
case -1411842725:
if(var10000.equals("CGLIB$hashCode$3()I")) {
return 14;
}
break;
case -1372377760:
if(var10000.equals("CGLIB$SET_STATIC_CALLBACKS([Lorg/assertj/core/internal/cglib/proxy/Callback;)V")) {
return 9;
}
break;
case -1315638312:
if(var10000.equals("setCallback(ILorg/assertj/core/internal/cglib/proxy/Callback;)V")) {
return 7;
}
break;
case -508378822:
if(var10000.equals("clone()Ljava/lang/Object;")) {
return 3;
}
break;
case -404209094:
if(var10000.equals("newInstance([Lorg/assertj/core/internal/cglib/proxy/Callback;)Ljava/lang/Object;")) {
return 5;
}
break;
case 1120488528:
if(var10000.equals("CGLIB$findMethodProxy(Lorg/assertj/core/internal/cglib/core/Signature;)Lorg/assertj/core/internal/cglib/proxy/MethodProxy;")) {
return 12;
}
break;
case 1306468936:
if(var10000.equals("CGLIB$toString$2()Ljava/lang/String;")) {
return 13;
}
break;
case 1381033007:
if(var10000.equals("newInstance(Lorg/assertj/core/internal/cglib/proxy/Callback;)Ljava/lang/Object;")) {
return 4;
}
break;
case 1406979248:
if(var10000.equals("getCallback(I)Lorg/assertj/core/internal/cglib/proxy/Callback;")) {
return 16;
}
break;
case 1692040879:
if(var10000.equals("setCallbacks([Lorg/assertj/core/internal/cglib/proxy/Callback;)V")) {
return 10;
}
break;
case 1800494055:
if(var10000.equals("CGLIB$clone$4()Ljava/lang/Object;")) {
return 15;
}
break;
case 1826985398:
if(var10000.equals("equals(Ljava/lang/Object;)Z")) {
return 0;
}
break;
case 1913648695:
if(var10000.equals("toString()Ljava/lang/String;")) {
return 1;
}
break;
case 1984935277:
if(var10000.equals("hashCode()I")) {
return 2;
}
break;
case 2097595388:
if(var10000.equals("CGLIB$SET_THREAD_CALLBACKS([Lorg/assertj/core/internal/cglib/proxy/Callback;)V")) {
return 11;
}
break;
case 2127541637:
if(var10000.equals("getCallbacks()[Lorg/assertj/core/internal/cglib/proxy/Callback;")) {
return 17;
}
}
return -1;
}
public int getMaxIndex() {
return 20;
}
}
5. 原理分析
在生成的TestEnhancer$Subject$$EnhancerByCGLIB$$xxxxxx存在两个方法sayHi()和CGLIB$sayHi$0(),在sayHi()中如果存在拦截器则走MethodInterceptor否则直接调用父类TestEnhancer$Subject的sayHi()方法
在拦截器中:
Subject 为 TestEnhancer$Subject$$EnhancerByCGLIB$$842aea1f父类
methodProxy.invoke 转为 ((Subject ) obj ).sayHi()
methodProxy.invokeSuper 转为((TestEnhancer$Subject$$EnhancerByCGLIB$$842aea1f) obj ).CGLIB$sayHi$0()
methodProxy.invoke(obj, args); obj为代理类 方法:sayHi() 回到调用MethodInterceptor //这个会变为死循环
methodProxy.invokeSuper(obj, args); obj为代理类 :CGLIB$sayHi$0() = super.sayHi()
methodProxy.invoke(obj, args); obj为Subject 方法:sayHi() 执行被代理的方法
methodProxy.invokeSuper(obj, args); obj为Subject 装换报错
网友评论