描述
动态代理是指在程序运行时生成代理类,通过操作代理类进而操作目标类。本文描述了JDK和cglib(Byte Code Generation Library)动态代理原理。两者都是生成代理类,来调用目标类的方法,但是JDK调用目标类方法使用反射,cglib调用时则使用FastClass方式,因此cglib调用比较快。
JDK流程:调用代理类的方法,调用InvocationHandler
的invoke()
,使用method.invoke()
调用目标类方法。
cglib流程:调用代理类的代理方法,调用MethodInterceptor
的interceptor()
,调用FastClass
的invoke()
,调用代理类的目标方法,目标方法调用父类方法。
JDK动态代理
JDK动态代理是操作字节码生成一个继承Proxy
、实现目标接口的代理类。代理类中调用Proxy
的属性InvocationHandler
的invoke()
。在invoke()
中使用method.invoke(target,args)
调用目标方法。
1.先编写InvocationHandler实现类,实现类中有目标类对象。
public class IHelloInvocationHandler implements InvocationHandler {
// 目标类对象
private Object target;
public IHelloInvocationHandler(Object target) {
this.target = target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("invoke before");
method.invoke(target, args);
System.out.println("invoke after");
return null;
}
2.测试demo
public static void main(String[] args) {
IHello target = new IHelloImpl();
Class extends IHello> aClass = target.getClass();
ClassLoader classLoader = aClass.getClassLoader();
Class>[] interfaces = aClass.getInterfaces();
IHelloInvocationHandler invocationHandler = new IHelloInvocationHandler(target);
IHello o = (IHello) Proxy.newProxyInstance(classLoader, interfaces, invocationHandler);
o.sayHello();
byte[] bytes = ProxyGenerator.generateProxyClass("$Proxy0.class", o.getClass().getInterfaces());
String path = "/Users/lucy/Desktop/$Proxy0.class";
FileOutputStream out = null;
try {
out = new FileOutputStream(path);
out.write(bytes);
out.flush();
} catch (java.io.IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3.反编译代理类
/*
* Decompiled with CFR 0.146.
*
* Could not load the following classes:
* $Proxy0.class
* com.redoor.proxy.jdk.IHello
*/
package $Proxy0;
import com.redoor.proxy.jdk.IHello;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;
public final class class
extends Proxy
implements IHello {
private static Method m1;
private static Method m3;
private static Method m2;
private static Method m0;
public class(InvocationHandler invocationHandler) throws {
super(invocationHandler);
}
public final boolean equals(Object object) throws {
try {
return (Boolean)this.h.invoke((Object)this, m1, new Object[]{object});
}
catch (Error | RuntimeException throwable) {
throw throwable;
}
catch (Throwable throwable) {
throw new UndeclaredThrowableException(throwable);
}
}
public final void sayHello() throws {
try {
this.h.invoke((Object)this, m3, null);
return;
}
catch (Error | RuntimeException throwable) {
throw throwable;
}
catch (Throwable throwable) {
throw new UndeclaredThrowableException(throwable);
}
}
public final String toString() throws {
try {
return (String)this.h.invoke((Object)this, m2, null);
}
catch (Error | RuntimeException throwable) {
throw throwable;
}
catch (Throwable throwable) {
throw new UndeclaredThrowableException(throwable);
}
}
public final int hashCode() throws {
try {
return (Integer)this.h.invoke((Object)this, m0, null);
}
catch (Error | RuntimeException throwable) {
throw throwable;
}
catch (Throwable throwable) {
throw new UndeclaredThrowableException(throwable);
}
}
static {
try {
m1 = Class.forName("java.lang.Object").getMethod("equals", Class.forName("java.lang.Object"));
m3 = Class.forName("com.redoor.proxy.jdk.IHello").getMethod("sayHello", new Class[0]);
m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]);
m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]);
return;
}
catch (NoSuchMethodException noSuchMethodException) {
throw new NoSuchMethodError(noSuchMethodException.getMessage());
}
catch (ClassNotFoundException classNotFoundException) {
throw new NoClassDefFoundError(classNotFoundException.getMessage());
}
}
}
cglib动态代理
cglib动态代理调用流程。o.sayHello()
调用代理类的代理方法。该方法调用MethodInterceptor的intercept()
。然后调用MethodProxy.invokerSuper()
。在调用FastClass的invoke()
。在调用代理类中的父类方法。
1.编写MethodInterceptor
public class UserMethodInterceptor implements MethodInterceptor {
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
System.out.println("intercept before");
Object o1 = methodProxy.invokeSuper(o, objects);
System.out.println("intercept after");
return o1;
}
}
2.测试demo
public static void main(String[] args) {
// 指定cglib生成类的目录位置
System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "/Users/lucy/Desktop/cglibProxy");
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(User.class);
enhancer.setCallback(new UserMethodInterceptor());
User o = (User) enhancer.create();
o.sayHello();
}
3.反编译生成类
可以看到在/Users/lucy/Desktop/cglibProxy/com/redoor/proxy/cglib
目录下生成了3个类,第2个类代理类,就是目标类的子类。第1个类是代理类的FastClass,第3个类是目标类的FastClass。
FastClass是对方法建立索引,使用索引去调用方法。避免使用反射,提高调用速率。
User$$EnhancerByCGLIB$$3668a66c$$FastClassByCGLIB$$cb19fc35.class
User$$EnhancerByCGLIB$$3668a66c.class
User$$FastClassByCGLIB$$5801fa18.class
- 反编译代理类
可以看到cglib为每个代理方法生成了两个方法。第一个是调用父类方法,也就是调用目标类方法。第二个是代理类方法,首先判断MethodInterceptor是否存在,如果存在就调用methodInterceptor.intercept()
。
/*
* Decompiled with CFR 0.146.
*
* Could not load the following classes:
* com.redoor.proxy.cglib.User
* net.sf.cglib.core.ReflectUtils
* net.sf.cglib.core.Signature
* net.sf.cglib.proxy.Callback
* net.sf.cglib.proxy.Factory
* net.sf.cglib.proxy.MethodInterceptor
* net.sf.cglib.proxy.MethodProxy
*/
package com.redoor.proxy.cglib;
import com.redoor.proxy.cglib.User;
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 User$$EnhancerByCGLIB$$3668a66c
extends User
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$sayHello$0$Method;
private static final MethodProxy CGLIB$sayHello$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> class_ = Class.forName("com.redoor.proxy.cglib.User$$EnhancerByCGLIB$$3668a66c");
Class> class_2 = Class.forName("java.lang.Object");
Method[] arrmethod = ReflectUtils.findMethods((String[])new String[]{"equals", "(Ljava/lang/Object;)Z", "toString", "()Ljava/lang/String;", "hashCode", "()I", "clone", "()Ljava/lang/Object;"}, (Method[])class_2.getDeclaredMethods());
CGLIB$equals$1$Method = arrmethod[0];
CGLIB$equals$1$Proxy = MethodProxy.create(class_2, class_, (String)"(Ljava/lang/Object;)Z", (String)"equals", (String)"CGLIB$equals$1");
CGLIB$toString$2$Method = arrmethod[1];
CGLIB$toString$2$Proxy = MethodProxy.create(class_2, class_, (String)"()Ljava/lang/String;", (String)"toString", (String)"CGLIB$toString$2");
CGLIB$hashCode$3$Method = arrmethod[2];
CGLIB$hashCode$3$Proxy = MethodProxy.create(class_2, class_, (String)"()I", (String)"hashCode", (String)"CGLIB$hashCode$3");
CGLIB$clone$4$Method = arrmethod[3];
CGLIB$clone$4$Proxy = MethodProxy.create(class_2, class_, (String)"()Ljava/lang/Object;", (String)"clone", (String)"CGLIB$clone$4");
class_2 = Class.forName("com.redoor.proxy.cglib.User");
CGLIB$sayHello$0$Method = ReflectUtils.findMethods((String[])new String[]{"sayHello", "()V"}, (Method[])class_2.getDeclaredMethods())[0];
CGLIB$sayHello$0$Proxy = MethodProxy.create(class_2, class_, (String)"()V", (String)"sayHello", (String)"CGLIB$sayHello$0");
}
final void CGLIB$sayHello$0() {
super.sayHello();
}
public final void sayHello() {
MethodInterceptor methodInterceptor = this.CGLIB$CALLBACK_0;
if (methodInterceptor == null) {
User$$EnhancerByCGLIB$$3668a66c.CGLIB$BIND_CALLBACKS((Object)this);
methodInterceptor = this.CGLIB$CALLBACK_0;
}
if (methodInterceptor != null) {
Object object = methodInterceptor.intercept((Object)this, CGLIB$sayHello$0$Method, CGLIB$emptyArgs, CGLIB$sayHello$0$Proxy);
return;
}
super.sayHello();
}
final boolean CGLIB$equals$1(Object object) {
return super.equals(object);
}
public final boolean equals(Object object) {
MethodInterceptor methodInterceptor = this.CGLIB$CALLBACK_0;
if (methodInterceptor == null) {
User$$EnhancerByCGLIB$$3668a66c.CGLIB$BIND_CALLBACKS((Object)this);
methodInterceptor = this.CGLIB$CALLBACK_0;
}
if (methodInterceptor != null) {
Object object2;
return methodInterceptor.intercept((Object)this, CGLIB$equals$1$Method, new Object[]{object}, CGLIB$equals$1$Proxy) == null ? false : (Boolean)object2;
}
return super.equals(object);
}
final String CGLIB$toString$2() {
return super.toString();
}
public final String toString() {
MethodInterceptor methodInterceptor = this.CGLIB$CALLBACK_0;
if (methodInterceptor == null) {
User$$EnhancerByCGLIB$$3668a66c.CGLIB$BIND_CALLBACKS((Object)this);
methodInterceptor = this.CGLIB$CALLBACK_0;
}
if (methodInterceptor != null) {
return (String)methodInterceptor.intercept((Object)this, CGLIB$toString$2$Method, CGLIB$emptyArgs, CGLIB$toString$2$Proxy);
}
return super.toString();
}
final int CGLIB$hashCode$3() {
return super.hashCode();
}
public final int hashCode() {
MethodInterceptor methodInterceptor = this.CGLIB$CALLBACK_0;
if (methodInterceptor == null) {
User$$EnhancerByCGLIB$$3668a66c.CGLIB$BIND_CALLBACKS((Object)this);
methodInterceptor = this.CGLIB$CALLBACK_0;
}
if (methodInterceptor != null) {
Object object;
return methodInterceptor.intercept((Object)this, CGLIB$hashCode$3$Method, CGLIB$emptyArgs, CGLIB$hashCode$3$Proxy) == null ? 0 : ((Number)object).intValue();
}
return super.hashCode();
}
final Object CGLIB$clone$4() throws CloneNotSupportedException {
return super.clone();
}
protected final Object clone() throws CloneNotSupportedException {
MethodInterceptor methodInterceptor = this.CGLIB$CALLBACK_0;
if (methodInterceptor == null) {
User$$EnhancerByCGLIB$$3668a66c.CGLIB$BIND_CALLBACKS((Object)this);
methodInterceptor = this.CGLIB$CALLBACK_0;
}
if (methodInterceptor != null) {
return methodInterceptor.intercept((Object)this, CGLIB$clone$4$Method, CGLIB$emptyArgs, CGLIB$clone$4$Proxy);
}
return super.clone();
}
public static MethodProxy CGLIB$findMethodProxy(Signature signature) {
switch (signature.toString().hashCode()) {
case -508378822: {
String string;
if (!string.equals("clone()Ljava/lang/Object;")) break;
return CGLIB$clone$4$Proxy;
}
case 1535311470: {
String string;
if (!string.equals("sayHello()V")) break;
return CGLIB$sayHello$0$Proxy;
}
case 1826985398: {
String string;
if (!string.equals("equals(Ljava/lang/Object;)Z")) break;
return CGLIB$equals$1$Proxy;
}
case 1913648695: {
String string;
if (!string.equals("toString()Ljava/lang/String;")) break;
return CGLIB$toString$2$Proxy;
}
case 1984935277: {
String string;
if (!string.equals("hashCode()I")) break;
return CGLIB$hashCode$3$Proxy;
}
default: {
}
}
return null;
}
public User$$EnhancerByCGLIB$$3668a66c() {
User$$EnhancerByCGLIB$$3668a66c user$$EnhancerByCGLIB$$3668a66c = this;
User$$EnhancerByCGLIB$$3668a66c.CGLIB$BIND_CALLBACKS((Object)user$$EnhancerByCGLIB$$3668a66c);
}
public static void CGLIB$SET_THREAD_CALLBACKS(Callback[] arrcallback) {
CGLIB$THREAD_CALLBACKS.set(arrcallback);
}
public static void CGLIB$SET_STATIC_CALLBACKS(Callback[] arrcallback) {
CGLIB$STATIC_CALLBACKS = arrcallback;
}
private static final void CGLIB$BIND_CALLBACKS(Object object) {
User$$EnhancerByCGLIB$$3668a66c user$$EnhancerByCGLIB$$3668a66c = (User$$EnhancerByCGLIB$$3668a66c)((Object)object);
if (!user$$EnhancerByCGLIB$$3668a66c.CGLIB$BOUND) {
user$$EnhancerByCGLIB$$3668a66c.CGLIB$BOUND = true;
Object object2 = CGLIB$THREAD_CALLBACKS.get();
if (object2 != null || (object2 = CGLIB$STATIC_CALLBACKS) != null) {
user$$EnhancerByCGLIB$$3668a66c.CGLIB$CALLBACK_0 = (MethodInterceptor)((Callback[])object2)[0];
}
}
}
public Object newInstance(Callback[] arrcallback) {
User$$EnhancerByCGLIB$$3668a66c.CGLIB$SET_THREAD_CALLBACKS(arrcallback);
User$$EnhancerByCGLIB$$3668a66c.CGLIB$SET_THREAD_CALLBACKS(null);
return new User$$EnhancerByCGLIB$$3668a66c();
}
public Object newInstance(Callback callback) {
User$$EnhancerByCGLIB$$3668a66c.CGLIB$SET_THREAD_CALLBACKS(new Callback[]{callback});
User$$EnhancerByCGLIB$$3668a66c.CGLIB$SET_THREAD_CALLBACKS(null);
return new User$$EnhancerByCGLIB$$3668a66c();
}
/*
* Unable to fully structure code
* Enabled aggressive block sorting
* Lifted jumps to return sites
*/
public Object newInstance(Class[] var1_1, Object[] var2_2, Callback[] var3_3) {
User$$EnhancerByCGLIB$$3668a66c.CGLIB$SET_THREAD_CALLBACKS(var3_3);
switch (var1_1.length) {
case 0: {
** break;
}
}
throw new IllegalArgumentException("Constructor not found");
// 1 sources:
User$$EnhancerByCGLIB$$3668a66c.CGLIB$SET_THREAD_CALLBACKS(null);
return new User$$EnhancerByCGLIB$$3668a66c();
}
public Callback getCallback(int n) {
MethodInterceptor methodInterceptor;
User$$EnhancerByCGLIB$$3668a66c.CGLIB$BIND_CALLBACKS((Object)this);
switch (n) {
case 0: {
User$$EnhancerByCGLIB$$3668a66c user$$EnhancerByCGLIB$$3668a66c;
methodInterceptor = user$$EnhancerByCGLIB$$3668a66c.CGLIB$CALLBACK_0;
break;
}
default: {
methodInterceptor = null;
}
}
return methodInterceptor;
}
public void setCallback(int n, Callback callback) {
switch (n) {
case 0: {
this.CGLIB$CALLBACK_0 = (MethodInterceptor)callback;
break;
}
}
}
public Callback[] getCallbacks() {
User$$EnhancerByCGLIB$$3668a66c.CGLIB$BIND_CALLBACKS((Object)this);
User$$EnhancerByCGLIB$$3668a66c user$$EnhancerByCGLIB$$3668a66c = this;
return new Callback[]{this.CGLIB$CALLBACK_0};
}
public void setCallbacks(Callback[] arrcallback) {
Callback[] arrcallback2 = arrcallback;
Callback[] arrcallback3 = arrcallback2;
User$$EnhancerByCGLIB$$3668a66c user$$EnhancerByCGLIB$$3668a66c = this;
this.CGLIB$CALLBACK_0 = (MethodInterceptor)arrcallback2[0];
}
static {
User$$EnhancerByCGLIB$$3668a66c.CGLIB$STATICHOOK1();
}
}
- 反编译代理类的FastClass
/*
* Decompiled with CFR 0.146.
*
* Could not load the following classes:
* net.sf.cglib.core.Signature
* net.sf.cglib.proxy.Callback
* net.sf.cglib.reflect.FastClass
*/
package com.redoor.proxy.cglib;
import com.redoor.proxy.cglib.User$$EnhancerByCGLIB$$3668a66c;
import java.lang.reflect.InvocationTargetException;
import net.sf.cglib.core.Signature;
import net.sf.cglib.proxy.Callback;
import net.sf.cglib.reflect.FastClass;
public class User$$EnhancerByCGLIB$$3668a66c$$FastClassByCGLIB$$cb19fc35
extends FastClass {
public User$$EnhancerByCGLIB$$3668a66c$$FastClassByCGLIB$$cb19fc35(Class class_) {
super(class_);
}
public int getIndex(Signature signature) {
switch (signature.toString().hashCode()) {
case -2055565910: {
String string;
if (!string.equals("CGLIB$SET_THREAD_CALLBACKS([Lnet/sf/cglib/proxy/Callback;)V")) break;
return 9;
}
case -1882565338: {
String string;
if (!string.equals("CGLIB$equals$1(Ljava/lang/Object;)Z")) break;
return 15;
}
case -1457535688: {
String string;
if (!string.equals("CGLIB$STATICHOOK1()V")) break;
return 14;
}
case -1411842725: {
String string;
if (!string.equals("CGLIB$hashCode$3()I")) break;
return 17;
}
case -894172689: {
String string;
if (!string.equals("newInstance(Lnet/sf/cglib/proxy/Callback;)Ljava/lang/Object;")) break;
return 4;
}
case -623122092: {
String string;
if (!string.equals("CGLIB$findMethodProxy(Lnet/sf/cglib/core/Signature;)Lnet/sf/cglib/proxy/MethodProxy;")) break;
return 12;
}
case -508378822: {
String string;
if (!string.equals("clone()Ljava/lang/Object;")) break;
return 3;
}
case -419626537: {
String string;
if (!string.equals("setCallbacks([Lnet/sf/cglib/proxy/Callback;)V")) break;
return 7;
}
case 291273791: {
String string;
if (!string.equals("CGLIB$sayHello$0()V")) break;
return 13;
}
case 560567118: {
String string;
if (!string.equals("setCallback(ILnet/sf/cglib/proxy/Callback;)V")) break;
return 20;
}
case 811063227: {
String string;
if (!string.equals("newInstance([Ljava/lang/Class;[Ljava/lang/Object;[Lnet/sf/cglib/proxy/Callback;)Ljava/lang/Object;")) break;
return 6;
}
case 973717575: {
String string;
if (!string.equals("getCallbacks()[Lnet/sf/cglib/proxy/Callback;")) break;
return 11;
}
case 1221173700: {
String string;
if (!string.equals("newInstance([Lnet/sf/cglib/proxy/Callback;)Ljava/lang/Object;")) break;
return 5;
}
case 1230699260: {
String string;
if (!string.equals("getCallback(I)Lnet/sf/cglib/proxy/Callback;")) break;
return 10;
}
case 1306468936: {
String string;
if (!string.equals("CGLIB$toString$2()Ljava/lang/String;")) break;
return 16;
}
case 1535311470: {
String string;
if (!string.equals("sayHello()V")) break;
return 19;
}
case 1584330438: {
String string;
if (!string.equals("CGLIB$SET_STATIC_CALLBACKS([Lnet/sf/cglib/proxy/Callback;)V")) break;
return 8;
}
case 1800494055: {
String string;
if (!string.equals("CGLIB$clone$4()Ljava/lang/Object;")) break;
return 18;
}
case 1826985398: {
String string;
if (!string.equals("equals(Ljava/lang/Object;)Z")) break;
return 0;
}
case 1913648695: {
String string;
if (!string.equals("toString()Ljava/lang/String;")) break;
return 1;
}
case 1984935277: {
String string;
if (!string.equals("hashCode()I")) break;
return 2;
}
default: {
}
}
return -1;
}
public int getIndex(String string, Class[] arrclass) {
Class[] arrclass2 = arrclass;
block0 : switch (string.hashCode()) {
case -2012993625: {
String string2;
if (!string2.equals("sayHello")) break;
arrclass2 = arrclass2;
switch (arrclass2.length) {
case 0: {
return 19;
}
}
break;
}
case -1983192202: {
String string2;
if (!string2.equals("CGLIB$sayHello$0")) break;
arrclass2 = arrclass2;
switch (arrclass2.length) {
case 0: {
return 13;
}
}
break;
}
case -1776922004: {
String string2;
if (!string2.equals("toString")) break;
arrclass2 = arrclass2;
switch (arrclass2.length) {
case 0: {
return 1;
}
}
break;
}
case -1295482945: {
String string2;
if (!string2.equals("equals")) break;
arrclass2 = arrclass2;
switch (arrclass2.length) {
case 1: {
arrclass2 = arrclass2;
if (!arrclass2[0].getName().equals("java.lang.Object")) break block0;
return 0;
}
}
break;
}
case -1053468136: {
String string2;
if (!string2.equals("getCallbacks")) break;
arrclass2 = arrclass2;
switch (arrclass2.length) {
case 0: {
return 11;
}
}
break;
}
case -124978609: {
String string2;
if (!string2.equals("CGLIB$equals$1")) break;
arrclass2 = arrclass2;
switch (arrclass2.length) {
case 1: {
arrclass2 = arrclass2;
if (!arrclass2[0].getName().equals("java.lang.Object")) break block0;
return 15;
}
}
break;
}
case -60403779: {
String string2;
if (!string2.equals("CGLIB$SET_STATIC_CALLBACKS")) break;
arrclass2 = arrclass2;
switch (arrclass2.length) {
case 1: {
arrclass2 = arrclass2;
if (!arrclass2[0].getName().equals("[Lnet.sf.cglib.proxy.Callback;")) break block0;
return 8;
}
}
break;
}
case -29025555: {
String string2;
if (!string2.equals("CGLIB$hashCode$3")) break;
arrclass2 = arrclass2;
switch (arrclass2.length) {
case 0: {
return 17;
}
}
break;
}
case 85179481: {
String string2;
if (!string2.equals("CGLIB$SET_THREAD_CALLBACKS")) break;
arrclass2 = arrclass2;
switch (arrclass2.length) {
case 1: {
arrclass2 = arrclass2;
if (!arrclass2[0].getName().equals("[Lnet.sf.cglib.proxy.Callback;")) break block0;
return 9;
}
}
break;
}
case 94756189: {
String string2;
if (!string2.equals("clone")) break;
arrclass2 = arrclass2;
switch (arrclass2.length) {
case 0: {
return 3;
}
}
break;
}
case 147696667: {
String string2;
if (!string2.equals("hashCode")) break;
arrclass2 = arrclass2;
switch (arrclass2.length) {
case 0: {
return 2;
}
}
break;
}
case 161998109: {
String string2;
if (!string2.equals("CGLIB$STATICHOOK1")) break;
arrclass2 = arrclass2;
switch (arrclass2.length) {
case 0: {
return 14;
}
}
break;
}
case 495524492: {
String string2;
if (!string2.equals("setCallbacks")) break;
arrclass2 = arrclass2;
switch (arrclass2.length) {
case 1: {
arrclass2 = arrclass2;
if (!arrclass2[0].getName().equals("[Lnet.sf.cglib.proxy.Callback;")) break block0;
return 7;
}
}
break;
}
case 1154623345: {
String string2;
if (!string2.equals("CGLIB$findMethodProxy")) break;
arrclass2 = arrclass2;
switch (arrclass2.length) {
case 1: {
arrclass2 = arrclass2;
if (!arrclass2[0].getName().equals("net.sf.cglib.core.Signature")) break block0;
return 12;
}
}
break;
}
case 1543336189: {
String string2;
if (!string2.equals("CGLIB$toString$2")) break;
arrclass2 = arrclass2;
switch (arrclass2.length) {
case 0: {
return 16;
}
}
break;
}
case 1811874389: {
String string2;
if (!string2.equals("newInstance")) break;
arrclass2 = arrclass2;
switch (arrclass2.length) {
case 1: {
arrclass2 = arrclass2;
switch (arrclass2[0].getName().hashCode()) {
case -845341380: {
String string3;
if (!string3.equals("net.sf.cglib.proxy.Callback")) break block0;
return 4;
}
case 1730110032: {
String string3;
if (!string3.equals("[Lnet.sf.cglib.proxy.Callback;")) break block0;
return 5;
}
default: {
break;
}
}
break block0;
}
case 3: {
arrclass2 = arrclass2;
if (!arrclass2[0].getName().equals("[Ljava.lang.Class;")) break block0;
arrclass2 = arrclass2;
if (!arrclass2[1].getName().equals("[Ljava.lang.Object;")) break block0;
arrclass2 = arrclass2;
if (!arrclass2[2].getName().equals("[Lnet.sf.cglib.proxy.Callback;")) break block0;
return 6;
}
}
break;
}
case 1817099975: {
String string2;
if (!string2.equals("setCallback")) break;
arrclass2 = arrclass2;
switch (arrclass2.length) {
case 2: {
arrclass2 = arrclass2;
if (!arrclass2[0].getName().equals("int")) break block0;
arrclass2 = arrclass2;
if (!arrclass2[1].getName().equals("net.sf.cglib.proxy.Callback")) break block0;
return 20;
}
}
break;
}
case 1905679803: {
String string2;
if (!string2.equals("getCallback")) break;
arrclass2 = arrclass2;
switch (arrclass2.length) {
case 1: {
arrclass2 = arrclass2;
if (!arrclass2[0].getName().equals("int")) break block0;
return 10;
}
}
break;
}
case 1951977610: {
String string2;
if (!string2.equals("CGLIB$clone$4")) break;
arrclass2 = arrclass2;
switch (arrclass2.length) {
case 0: {
return 18;
}
}
break;
}
default: {
break;
}
}
return -1;
}
public int getIndex(Class[] arrclass) {
switch (arrclass.length) {
case 0: {
return 0;
}
}
return -1;
}
public Object invoke(int n, Object object, Object[] arrobject) throws InvocationTargetException {
User$$EnhancerByCGLIB$$3668a66c user$$EnhancerByCGLIB$$3668a66c = (User$$EnhancerByCGLIB$$3668a66c)((Object)object);
try {
switch (n) {
case 0: {
return new Boolean(user$$EnhancerByCGLIB$$3668a66c.equals(arrobject[0]));
}
case 1: {
return user$$EnhancerByCGLIB$$3668a66c.toString();
}
case 2: {
return new Integer(user$$EnhancerByCGLIB$$3668a66c.hashCode());
}
case 3: {
return user$$EnhancerByCGLIB$$3668a66c.clone();
}
case 4: {
return user$$EnhancerByCGLIB$$3668a66c.newInstance((Callback)arrobject[0]);
}
case 5: {
return user$$EnhancerByCGLIB$$3668a66c.newInstance((Callback[])arrobject[0]);
}
case 6: {
return user$$EnhancerByCGLIB$$3668a66c.newInstance((Class[])arrobject[0], (Object[])arrobject[1], (Callback[])arrobject[2]);
}
case 7: {
user$$EnhancerByCGLIB$$3668a66c.setCallbacks((Callback[])arrobject[0]);
return null;
}
case 8: {
User$$EnhancerByCGLIB$$3668a66c.CGLIB$SET_STATIC_CALLBACKS((Callback[])arrobject[0]);
return null;
}
case 9: {
User$$EnhancerByCGLIB$$3668a66c.CGLIB$SET_THREAD_CALLBACKS((Callback[])arrobject[0]);
return null;
}
case 10: {
return user$$EnhancerByCGLIB$$3668a66c.getCallback(((Number)arrobject[0]).intValue());
}
case 11: {
return user$$EnhancerByCGLIB$$3668a66c.getCallbacks();
}
case 12: {
return User$$EnhancerByCGLIB$$3668a66c.CGLIB$findMethodProxy((Signature)arrobject[0]);
}
case 13: {
user$$EnhancerByCGLIB$$3668a66c.CGLIB$sayHello$0();
return null;
}
case 14: {
User$$EnhancerByCGLIB$$3668a66c.CGLIB$STATICHOOK1();
return null;
}
case 15: {
return new Boolean(user$$EnhancerByCGLIB$$3668a66c.CGLIB$equals$1(arrobject[0]));
}
case 16: {
return user$$EnhancerByCGLIB$$3668a66c.CGLIB$toString$2();
}
case 17: {
return new Integer(user$$EnhancerByCGLIB$$3668a66c.CGLIB$hashCode$3());
}
case 18: {
return user$$EnhancerByCGLIB$$3668a66c.CGLIB$clone$4();
}
case 19: {
user$$EnhancerByCGLIB$$3668a66c.sayHello();
return null;
}
case 20: {
user$$EnhancerByCGLIB$$3668a66c.setCallback(((Number)arrobject[0]).intValue(), (Callback)arrobject[1]);
return null;
}
}
}
catch (Throwable throwable) {
throw new InvocationTargetException(throwable);
}
throw new IllegalArgumentException("Cannot find matching method/constructor");
}
public Object newInstance(int n, Object[] arrobject) throws InvocationTargetException {
try {
switch (n) {
case 0: {
return new User$$EnhancerByCGLIB$$3668a66c();
}
}
}
catch (Throwable throwable) {
throw new InvocationTargetException(throwable);
}
throw new IllegalArgumentException("Cannot find matching method/constructor");
}
public int getMaxIndex() {
return 20;
}
}
- 反编译目标类的FastClass
/*
* Decompiled with CFR 0.146.
*
* Could not load the following classes:
* com.redoor.proxy.cglib.User
* net.sf.cglib.core.Signature
* net.sf.cglib.reflect.FastClass
*/
package com.redoor.proxy.cglib;
import com.redoor.proxy.cglib.User;
import java.lang.reflect.InvocationTargetException;
import net.sf.cglib.core.Signature;
import net.sf.cglib.reflect.FastClass;
public class User$$FastClassByCGLIB$$5801fa18
extends FastClass {
public User$$FastClassByCGLIB$$5801fa18(Class class_) {
super(class_);
}
public int getIndex(Signature signature) {
switch (signature.toString().hashCode()) {
case 1535311470: {
String string;
if (!string.equals("sayHello()V")) break;
return 0;
}
case 1826985398: {
String string;
if (!string.equals("equals(Ljava/lang/Object;)Z")) break;
return 1;
}
case 1913648695: {
String string;
if (!string.equals("toString()Ljava/lang/String;")) break;
return 2;
}
case 1984935277: {
String string;
if (!string.equals("hashCode()I")) break;
return 3;
}
default: {
}
}
return -1;
}
public int getIndex(String string, Class[] arrclass) {
Class[] arrclass2 = arrclass;
block0 : switch (string.hashCode()) {
case -2012993625: {
String string2;
if (!string2.equals("sayHello")) break;
arrclass2 = arrclass2;
switch (arrclass2.length) {
case 0: {
return 0;
}
}
break;
}
case -1776922004: {
String string2;
if (!string2.equals("toString")) break;
arrclass2 = arrclass2;
switch (arrclass2.length) {
case 0: {
return 2;
}
}
break;
}
case -1295482945: {
String string2;
if (!string2.equals("equals")) break;
arrclass2 = arrclass2;
switch (arrclass2.length) {
case 1: {
arrclass2 = arrclass2;
if (!arrclass2[0].getName().equals("java.lang.Object")) break block0;
return 1;
}
}
break;
}
case 147696667: {
String string2;
if (!string2.equals("hashCode")) break;
arrclass2 = arrclass2;
switch (arrclass2.length) {
case 0: {
return 3;
}
}
break;
}
default: {
break;
}
}
return -1;
}
public int getIndex(Class[] arrclass) {
switch (arrclass.length) {
case 0: {
return 0;
}
}
return -1;
}
public Object invoke(int n, Object object, Object[] arrobject) throws InvocationTargetException {
User user = (User)object;
try {
switch (n) {
case 0: {
user.sayHello();
return null;
}
case 1: {
return new Boolean(user.equals(arrobject[0]));
}
case 2: {
return user.toString();
}
case 3: {
return new Integer(user.hashCode());
}
}
}
catch (Throwable throwable) {
throw new InvocationTargetException(throwable);
}
throw new IllegalArgumentException("Cannot find matching method/constructor");
}
public Object newInstance(int n, Object[] arrobject) throws InvocationTargetException {
try {
switch (n) {
case 0: {
return new User();
}
}
}
catch (Throwable throwable) {
throw new InvocationTargetException(throwable);
}
throw new IllegalArgumentException("Cannot find matching method/constructor");
}
public int getMaxIndex() {
return 3;
}
}
问题
cfr反编译cglib生成类时有点问题
引用
https://www.jianshu.com/p/269afd0a52e6
https://www.jianshu.com/p/269afd0a52e6
https://zhuanlan.zhihu.com/p/35144462
https://www.throwable.club/2018/12/16/cglib-dynamic-proxy-analyze/