java 代理模式
1. 静态代理
1.1 概念
静态代理模式其实很常见,比如买火车票这件小事:黄牛相当于是火车站的代理,我们可以通过黄牛买票,但只能去火车站进行改签和退票。在代码实现中相当于为一个委托对象realSubject提供一个代理对象proxy,通过proxy可以调用realSubject的部分功能,并添加一些额外的业务处理,同时可以屏蔽realSubject中未开放的接口。
- RealSubject 是委托类,Proxy 是代理类;
- Subject 是委托类和代理类的接口;
- request() 是委托类和代理类的共同方法;
1.2 静态代理实现
interface Subject {
void request();
}
class RealSubject implements Subject {
@Override
public void request(){
System.out.println("RealSubject");
}
}
class Proxy implements Subject {
private Subject subject;
public Proxy(Subject subject){
this.subject = subject;
}
@Override
public void request(){
System.out.println("begin");
subject.request();
System.out.println("end");
}
}
public class ProxyTest {
public static void main(String args[]) {
RealSubject subject = new RealSubject();
Proxy proxy = new Proxy(subject);
proxy.request();
}
}
输出:
begin
RealSubject
end
静态代理实现中,一个委托类对应一个代理类,代理类在编译期间就已经确定。
2. java动态代理
2.1 概念
动态代理中,代理类并不是在Java代码中实现,而是在运行时期生成,相比静态代理,动态代理可以很方便的对委托类的方法进行统一处理,如添加方法调用次数、添加日志功能等等。
java动态代理核心类:
- java.lang.reflect.Proxy
- java.lang.reflect.InvocationHandler
实际使用:
- 实现 InvocationHandler 的 invoke 方法
- 调用 Proxy.newProxyInstance 生成代理类
2.2 java动态代理实现
比如公司员工准备出差,需要办理签证,办理签证需要准备各种材料,一些公司有专门的签证科,帮员工办理签证。
- 申请签证接口
public interface Subject {
//申请签证
public void applyVisa();
}
- 实际申请签证的人
public class RealSubject implements Subject {
//代理人为员工申请签证
@Override
public void applyVisa() {
System.out.println("apply visa for staff");
}
public void otherMethod(){
}
}
- 签证科代理员工,准备收集员工材料,代员工申请签证
public class MyInvocationHandler implements InvocationHandler {
private Object target;
public MyInvocationHandler(Object target) {
this.target = target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//准备材料
System.out.println("prepare materials");
//申请签证
Object object = method.invoke(target, args);
//通知员工领取签证
System.out.println("notice staff to receive visa");
return object;
}
public Object getProxy() {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Class<?> [] interfaces = target.getClass().getInterfaces();
return Proxy.newProxyInstance(classLoader, interfaces, this);
}
}
- java动态代理demo
public class Test {
public static void main(String[] args) throws Exception {
RealSubject realSubject = new RealSubject();
MyInvocationHandler handler = new MyInvocationHandler(realSubject);
Subject subject = (Subject) handler.getProxy();
subject.applyVisa();
//输出jdk动态代理类文件
byte[] classFile = ProxyGenerator.generateProxyClass("com.sun.proxy.$Proxy.1", subject.getClass().getInterfaces());
FileOutputStream out = new FileOutputStream("com.sun.proxy.$Proxy.1.class");
out.write(classFile);
out.flush();
}
}
输出:
prepare materials
apply visa for staff
notice staff to receive visa
使用反编译软件打开com.sun.proxy.$Proxy.1.class
- 可以看到代理类继承Proxy,并且实现了Subject接口。
- 代理类的 applyVisa 执行的是 InvocationHandler 的 invoke 方法
package com.sun.proxy.$Proxy;
import com.lilongjiu.study.java.proxy.Subject;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;
public final class 1
extends Proxy
implements Subject
{
private static Method m1;
private static Method m2;
private static Method m3;
private static Method m0;
public 1(InvocationHandler paramInvocationHandler)
{
super(paramInvocationHandler);
}
public final boolean equals(Object paramObject)
{
try
{
return ((Boolean)this.h.invoke(this, m1, new Object[] { paramObject })).booleanValue();
}
catch (Error|RuntimeException localError)
{
throw localError;
}
catch (Throwable localThrowable)
{
throw new UndeclaredThrowableException(localThrowable);
}
}
public final String toString()
{
try
{
return (String)this.h.invoke(this, m2, null);
}
catch (Error|RuntimeException localError)
{
throw localError;
}
catch (Throwable localThrowable)
{
throw new UndeclaredThrowableException(localThrowable);
}
}
public final void applyVisa()
{
try
{
this.h.invoke(this, m3, null);
return;
}
catch (Error|RuntimeException localError)
{
throw localError;
}
catch (Throwable localThrowable)
{
throw new UndeclaredThrowableException(localThrowable);
}
}
public final int hashCode()
{
try
{
return ((Integer)this.h.invoke(this, m0, null)).intValue();
}
catch (Error|RuntimeException localError)
{
throw localError;
}
catch (Throwable localThrowable)
{
throw new UndeclaredThrowableException(localThrowable);
}
}
static
{
try
{
m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[] { Class.forName("java.lang.Object") });
m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]);
m3 = Class.forName("com.lilongjiu.study.java.proxy.Subject").getMethod("applyVisa", new Class[0]);
m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]);
return;
}
catch (NoSuchMethodException localNoSuchMethodException)
{
throw new NoSuchMethodError(localNoSuchMethodException.getMessage());
}
catch (ClassNotFoundException localClassNotFoundException)
{
throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
}
}
}
- jdk动态代理使用的局限性
通过反射类Proxy和InvocationHandler回调接口实现的jdk动态代理,要求委托类必须实现一个接口,但事实上并不是所有类都有接口,对于没有实现接口的类,便无法使用该方方式实现动态代理。
jdk动态代理生成的代理与被代理对象并没有继承关系,只是实现了相同的接口
3.cglib 动态代理
3.1 概念
CGLib采用了底层的字节码技术,其原理是通过字节码技术为一个类创建子类,并在子类中采用方法拦截的技术拦截所有父类方法的调用,顺势织入横切逻辑
cglib动态代理核心类:
- net.sf.cglib.proxy.Enhancer
- net.sf.cglib.proxy.MethodInterceptor
实际使用:
- Enhancer 设置代理类,设置回调方法拦截器
- 实现 MethodInterceptor 的 intercept 方法
3.2 cglib动态代理实现
package com.lilongjiu.study.java.proxy;
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;
/**
* Created by lilongjiu.
*/
public class CgLibProxy implements MethodInterceptor {
public Object getProxy(Class<?> clazz)
{
Enhancer enhancer = new Enhancer();
//设置代理类
enhancer.setSuperclass(clazz);
//设置回调拦截
enhancer.setCallback(this);
return enhancer.create();
}
@Override
public Object intercept(Object object, Method method, Object[] arg2,
MethodProxy proxy) throws Throwable
{
//准备材料
System.out.println("prepare materials");
//申请签证
Object result = proxy.invokeSuper(object, arg2);
//通知员工领取签证
System.out.println("notice staff to receive visa");
return result;
}
public static void main(String[] args)
{
System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "/Users/lilongjiu/02workspace/temp");
CgLibProxy proxy = new CgLibProxy();
Subject subject = (Subject)proxy.getProxy(RealSubject.class);
subject.applyVisa();
}
}
* 可以看到cglib生成的代理类,继承了代理对象,代理与被代理对象是继承关系。
package com.lilongjiu.study.java.proxy;
import java.lang.reflect.Method;
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 RealSubject$$EnhancerByCGLIB$$72d04edb
extends RealSubject
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$applyVisa$0$Method;
private static final MethodProxy CGLIB$applyVisa$0$Proxy;
private static final Object[] CGLIB$emptyArgs;
private static final Method CGLIB$otherMethod$1$Method;
private static final MethodProxy CGLIB$otherMethod$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;
/* Error */
static void CGLIB$STATICHOOK1()
{
// Byte code:
// 0: new 22 java/lang/ThreadLocal
// 3: dup
// 4: invokespecial 25 java/lang/ThreadLocal:<init> ()V
// 7: putstatic 27 com/lilongjiu/study/java/proxy/RealSubject$$EnhancerByCGLIB$$72d04edb:CGLIB$THREAD_CALLBACKS Ljava/lang/ThreadLocal;
// 10: iconst_0
// 11: anewarray 76 java/lang/Object
// 14: putstatic 47 com/lilongjiu/study/java/proxy/RealSubject$$EnhancerByCGLIB$$72d04edb:CGLIB$emptyArgs [Ljava/lang/Object;
// 17: ldc -109
// 19: invokestatic 153 java/lang/Class:forName (Ljava/lang/String;)Ljava/lang/Class;
// 22: astore_0
// 23: bipush 8
// 25: anewarray 97 java/lang/String
// 28: dup
// 29: iconst_0
// 30: ldc -102
// 32: aastore
// 33: dup
// 34: iconst_1
// 35: ldc -101
// 37: aastore
// 38: dup
// 39: iconst_2
// 40: ldc -100
// 42: aastore
// 43: dup
// 44: iconst_3
// 45: ldc -99
// 47: aastore
// 48: dup
// 49: iconst_4
// 50: ldc -98
// 52: aastore
// 53: dup
// 54: iconst_5
// 55: ldc -97
// 57: aastore
// 58: dup
// 59: bipush 6
// 61: ldc -96
// 63: aastore
// 64: dup
// 65: bipush 7
// 67: ldc -95
// 69: aastore
// 70: ldc -93
// 72: invokestatic 153 java/lang/Class:forName (Ljava/lang/String;)Ljava/lang/Class;
// 75: dup
// 76: astore_1
// 77: invokevirtual 167 java/lang/Class:getDeclaredMethods ()[Ljava/lang/reflect/Method;
// 80: invokestatic 173 net/sf/cglib/core/ReflectUtils:findMethods ([Ljava/lang/String;[Ljava/lang/reflect/Method;)[Ljava/lang/reflect/Method;
// 83: dup
// 84: iconst_0
// 85: aaload
// 86: putstatic 74 com/lilongjiu/study/java/proxy/RealSubject$$EnhancerByCGLIB$$72d04edb:CGLIB$equals$2$Method Ljava/lang/reflect/Method;
// 89: aload_1
// 90: aload_0
// 91: ldc -101
// 93: ldc -102
// 95: ldc -82
// 97: invokestatic 180 net/sf/cglib/proxy/MethodProxy:create (Ljava/lang/Class;Ljava/lang/Class;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Lnet/sf/cglib/proxy/MethodProxy;
// 100: putstatic 78 com/lilongjiu/study/java/proxy/RealSubject$$EnhancerByCGLIB$$72d04edb:CGLIB$equals$2$Proxy Lnet/sf/cglib/proxy/MethodProxy;
// 103: dup
// 104: iconst_1
// 105: aaload
// 106: putstatic 93 com/lilongjiu/study/java/proxy/RealSubject$$EnhancerByCGLIB$$72d04edb:CGLIB$toString$3$Method Ljava/lang/reflect/Method;
// 109: aload_1
// 110: aload_0
// 111: ldc -99
// 113: ldc -100
// 115: ldc -75
// 117: invokestatic 180 net/sf/cglib/proxy/MethodProxy:create (Ljava/lang/Class;Ljava/lang/Class;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Lnet/sf/cglib/proxy/MethodProxy;
// 120: putstatic 95 com/lilongjiu/study/java/proxy/RealSubject$$EnhancerByCGLIB$$72d04edb:CGLIB$toString$3$Proxy Lnet/sf/cglib/proxy/MethodProxy;
// 123: dup
// 124: iconst_2
// 125: aaload
// 126: putstatic 106 com/lilongjiu/study/java/proxy/RealSubject$$EnhancerByCGLIB$$72d04edb:CGLIB$hashCode$4$Method Ljava/lang/reflect/Method;
// 129: aload_1
// 130: aload_0
// 131: ldc -97
// 133: ldc -98
// 135: ldc -74
// 137: invokestatic 180 net/sf/cglib/proxy/MethodProxy:create (Ljava/lang/Class;Ljava/lang/Class;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Lnet/sf/cglib/proxy/MethodProxy;
// 140: putstatic 108 com/lilongjiu/study/java/proxy/RealSubject$$EnhancerByCGLIB$$72d04edb:CGLIB$hashCode$4$Proxy Lnet/sf/cglib/proxy/MethodProxy;
// 143: dup
// 144: iconst_3
// 145: aaload
// 146: putstatic 124 com/lilongjiu/study/java/proxy/RealSubject$$EnhancerByCGLIB$$72d04edb:CGLIB$clone$5$Method Ljava/lang/reflect/Method;
// 149: aload_1
// 150: aload_0
// 151: ldc -95
// 153: ldc -96
// 155: ldc -73
// 157: invokestatic 180 net/sf/cglib/proxy/MethodProxy:create (Ljava/lang/Class;Ljava/lang/Class;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Lnet/sf/cglib/proxy/MethodProxy;
// 160: putstatic 126 com/lilongjiu/study/java/proxy/RealSubject$$EnhancerByCGLIB$$72d04edb:CGLIB$clone$5$Proxy Lnet/sf/cglib/proxy/MethodProxy;
// 163: pop
// 164: iconst_4
// 165: anewarray 97 java/lang/String
// 168: dup
// 169: iconst_0
// 170: ldc -72
// 172: aastore
// 173: dup
// 174: iconst_1
// 175: ldc -71
// 177: aastore
// 178: dup
// 179: iconst_2
// 180: ldc -70
// 182: aastore
// 183: dup
// 184: iconst_3
// 185: ldc -71
// 187: aastore
// 188: ldc -68
// 190: invokestatic 153 java/lang/Class:forName (Ljava/lang/String;)Ljava/lang/Class;
// 193: dup
// 194: astore_1
// 195: invokevirtual 167 java/lang/Class:getDeclaredMethods ()[Ljava/lang/reflect/Method;
// 198: invokestatic 173 net/sf/cglib/core/ReflectUtils:findMethods ([Ljava/lang/String;[Ljava/lang/reflect/Method;)[Ljava/lang/reflect/Method;
// 201: dup
// 202: iconst_0
// 203: aaload
// 204: putstatic 45 com/lilongjiu/study/java/proxy/RealSubject$$EnhancerByCGLIB$$72d04edb:CGLIB$applyVisa$0$Method Ljava/lang/reflect/Method;
// 207: aload_1
// 208: aload_0
// 209: ldc -71
// 211: ldc -72
// 213: ldc -67
// 215: invokestatic 180 net/sf/cglib/proxy/MethodProxy:create (Ljava/lang/Class;Ljava/lang/Class;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Lnet/sf/cglib/proxy/MethodProxy;
// 218: putstatic 49 com/lilongjiu/study/java/proxy/RealSubject$$EnhancerByCGLIB$$72d04edb:CGLIB$applyVisa$0$Proxy Lnet/sf/cglib/proxy/MethodProxy;
// 221: dup
// 222: iconst_1
// 223: aaload
// 224: putstatic 63 com/lilongjiu/study/java/proxy/RealSubject$$EnhancerByCGLIB$$72d04edb:CGLIB$otherMethod$1$Method Ljava/lang/reflect/Method;
// 227: aload_1
// 228: aload_0
// 229: ldc -71
// 231: ldc -70
// 233: ldc -66
// 235: invokestatic 180 net/sf/cglib/proxy/MethodProxy:create (Ljava/lang/Class;Ljava/lang/Class;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Lnet/sf/cglib/proxy/MethodProxy;
// 238: putstatic 65 com/lilongjiu/study/java/proxy/RealSubject$$EnhancerByCGLIB$$72d04edb:CGLIB$otherMethod$1$Proxy Lnet/sf/cglib/proxy/MethodProxy;
// 241: pop
// 242: return
// 243: athrow
}
final void CGLIB$applyVisa$0()
{
super.applyVisa();
}
public final void applyVisa()
{
MethodInterceptor tmp4_1 = this.CGLIB$CALLBACK_0;
if (tmp4_1 == null)
{
tmp4_1;
CGLIB$BIND_CALLBACKS(this);
}
if (this.CGLIB$CALLBACK_0 != null) {
return;
}
super.applyVisa();
}
final void CGLIB$otherMethod$1()
{
super.otherMethod();
}
public final void otherMethod()
{
MethodInterceptor tmp4_1 = this.CGLIB$CALLBACK_0;
if (tmp4_1 == null)
{
tmp4_1;
CGLIB$BIND_CALLBACKS(this);
}
if (this.CGLIB$CALLBACK_0 != null) {
return;
}
super.otherMethod();
}
final boolean CGLIB$equals$2(Object paramObject)
{
return super.equals(paramObject);
}
public final boolean equals(Object paramObject)
{
MethodInterceptor tmp4_1 = this.CGLIB$CALLBACK_0;
if (tmp4_1 == null)
{
tmp4_1;
CGLIB$BIND_CALLBACKS(this);
}
MethodInterceptor tmp17_14 = this.CGLIB$CALLBACK_0;
if (tmp17_14 != null)
{
Object tmp41_36 = tmp17_14.intercept(this, CGLIB$equals$2$Method, new Object[] { paramObject }, CGLIB$equals$2$Proxy);
tmp41_36;
return tmp41_36 == null ? false : ((Boolean)tmp41_36).booleanValue();
}
return super.equals(paramObject);
}
final String CGLIB$toString$3()
{
return super.toString();
}
public final String toString()
{
MethodInterceptor tmp4_1 = this.CGLIB$CALLBACK_0;
if (tmp4_1 == null)
{
tmp4_1;
CGLIB$BIND_CALLBACKS(this);
}
MethodInterceptor tmp17_14 = this.CGLIB$CALLBACK_0;
if (tmp17_14 != null) {
return (String)tmp17_14.intercept(this, CGLIB$toString$3$Method, CGLIB$emptyArgs, CGLIB$toString$3$Proxy);
}
return super.toString();
}
final int CGLIB$hashCode$4()
{
return super.hashCode();
}
public final int hashCode()
{
MethodInterceptor tmp4_1 = this.CGLIB$CALLBACK_0;
if (tmp4_1 == null)
{
tmp4_1;
CGLIB$BIND_CALLBACKS(this);
}
MethodInterceptor tmp17_14 = this.CGLIB$CALLBACK_0;
if (tmp17_14 != null)
{
Object tmp36_31 = tmp17_14.intercept(this, CGLIB$hashCode$4$Method, CGLIB$emptyArgs, CGLIB$hashCode$4$Proxy);
tmp36_31;
return tmp36_31 == null ? 0 : ((Number)tmp36_31).intValue();
}
return super.hashCode();
}
final Object CGLIB$clone$5()
throws CloneNotSupportedException
{
return super.clone();
}
protected final Object clone()
throws CloneNotSupportedException
{
MethodInterceptor tmp4_1 = this.CGLIB$CALLBACK_0;
if (tmp4_1 == null)
{
tmp4_1;
CGLIB$BIND_CALLBACKS(this);
}
MethodInterceptor tmp17_14 = this.CGLIB$CALLBACK_0;
if (tmp17_14 != null) {
return tmp17_14.intercept(this, CGLIB$clone$5$Method, CGLIB$emptyArgs, CGLIB$clone$5$Proxy);
}
return super.clone();
}
/* Error */
public static MethodProxy CGLIB$findMethodProxy(net.sf.cglib.core.Signature arg0)
{
// Byte code:
// 0: aload_0
// 1: invokevirtual 129 java/lang/Object:toString ()Ljava/lang/String;
// 4: dup
// 5: invokevirtual 130 java/lang/Object:hashCode ()I
// 8: lookupswitch default:+132->140, -508378822:+60->68, -299588156:+72->80, 332556358:+84->92, 1826985398:+96->104, 1913648695:+108->116, 1984935277:+120->128
// 68: ldc -124
// 70: invokevirtual 133 java/lang/Object:equals (Ljava/lang/Object;)Z
// 73: ifeq +68 -> 141
// 76: getstatic 126 com/lilongjiu/study/java/proxy/RealSubject$$EnhancerByCGLIB$$72d04edb:CGLIB$clone$5$Proxy Lnet/sf/cglib/proxy/MethodProxy;
// 79: areturn
// 80: ldc -121
// 82: invokevirtual 133 java/lang/Object:equals (Ljava/lang/Object;)Z
// 85: ifeq +56 -> 141
// 88: getstatic 65 com/lilongjiu/study/java/proxy/RealSubject$$EnhancerByCGLIB$$72d04edb:CGLIB$otherMethod$1$Proxy Lnet/sf/cglib/proxy/MethodProxy;
// 91: areturn
// 92: ldc -119
// 94: invokevirtual 133 java/lang/Object:equals (Ljava/lang/Object;)Z
// 97: ifeq +44 -> 141
// 100: getstatic 49 com/lilongjiu/study/java/proxy/RealSubject$$EnhancerByCGLIB$$72d04edb:CGLIB$applyVisa$0$Proxy Lnet/sf/cglib/proxy/MethodProxy;
// 103: areturn
// 104: ldc -117
// 106: invokevirtual 133 java/lang/Object:equals (Ljava/lang/Object;)Z
// 109: ifeq +32 -> 141
// 112: getstatic 78 com/lilongjiu/study/java/proxy/RealSubject$$EnhancerByCGLIB$$72d04edb:CGLIB$equals$2$Proxy Lnet/sf/cglib/proxy/MethodProxy;
// 115: areturn
// 116: ldc -115
// 118: invokevirtual 133 java/lang/Object:equals (Ljava/lang/Object;)Z
// 121: ifeq +20 -> 141
// 124: getstatic 95 com/lilongjiu/study/java/proxy/RealSubject$$EnhancerByCGLIB$$72d04edb:CGLIB$toString$3$Proxy Lnet/sf/cglib/proxy/MethodProxy;
// 127: areturn
// 128: ldc -113
// 130: invokevirtual 133 java/lang/Object:equals (Ljava/lang/Object;)Z
// 133: ifeq +8 -> 141
// 136: getstatic 108 com/lilongjiu/study/java/proxy/RealSubject$$EnhancerByCGLIB$$72d04edb:CGLIB$hashCode$4$Proxy Lnet/sf/cglib/proxy/MethodProxy;
// 139: areturn
// 140: pop
// 141: aconst_null
// 142: areturn
}
public RealSubject$$EnhancerByCGLIB$$72d04edb()
{
CGLIB$BIND_CALLBACKS(this);
}
public static void CGLIB$SET_THREAD_CALLBACKS(Callback[] paramArrayOfCallback)
{
CGLIB$THREAD_CALLBACKS.set(paramArrayOfCallback);
}
public static void CGLIB$SET_STATIC_CALLBACKS(Callback[] paramArrayOfCallback)
{
CGLIB$STATIC_CALLBACKS = paramArrayOfCallback;
}
private static final void CGLIB$BIND_CALLBACKS(Object paramObject)
{
72d04edb local72d04edb = (72d04edb)paramObject;
if (!local72d04edb.CGLIB$BOUND)
{
local72d04edb.CGLIB$BOUND = true;
Object tmp23_20 = CGLIB$THREAD_CALLBACKS.get();
if (tmp23_20 == null)
{
tmp23_20;
CGLIB$STATIC_CALLBACKS;
}
local72d04edb.CGLIB$CALLBACK_0 = (tmp31_28 == null ? tmp31_28 : (MethodInterceptor)((Callback[])tmp23_20)[0]);
}
}
public Object newInstance(Callback[] paramArrayOfCallback)
{
CGLIB$SET_THREAD_CALLBACKS(paramArrayOfCallback);
CGLIB$SET_THREAD_CALLBACKS(null);
return new 72d04edb();
}
public Object newInstance(Callback paramCallback)
{
CGLIB$SET_THREAD_CALLBACKS(new Callback[] { paramCallback });
CGLIB$SET_THREAD_CALLBACKS(null);
return new 72d04edb();
}
/* Error */
public Object newInstance(Class[] arg1, Object[] arg2, Callback[] arg3)
{
// Byte code:
// 0: aload_3
// 1: invokestatic 209 com/lilongjiu/study/java/proxy/RealSubject$$EnhancerByCGLIB$$72d04edb:CGLIB$SET_THREAD_CALLBACKS ([Lnet/sf/cglib/proxy/Callback;)V
// 4: new 2 com/lilongjiu/study/java/proxy/RealSubject$$EnhancerByCGLIB$$72d04edb
// 7: dup
// 8: aload_1
// 9: dup
// 10: arraylength
// 11: tableswitch default:+24->35, 0:+17->28
// 28: pop
// 29: invokespecial 210 com/lilongjiu/study/java/proxy/RealSubject$$EnhancerByCGLIB$$72d04edb:<init> ()V
// 32: goto +17 -> 49
// 35: goto +3 -> 38
// 38: pop
// 39: new 216 java/lang/IllegalArgumentException
// 42: dup
// 43: ldc -38
// 45: invokespecial 221 java/lang/IllegalArgumentException:<init> (Ljava/lang/String;)V
// 48: athrow
// 49: aconst_null
// 50: invokestatic 209 com/lilongjiu/study/java/proxy/RealSubject$$EnhancerByCGLIB$$72d04edb:CGLIB$SET_THREAD_CALLBACKS ([Lnet/sf/cglib/proxy/Callback;)V
// 53: areturn
}
public Callback getCallback(int paramInt)
{
CGLIB$BIND_CALLBACKS(this);
switch (paramInt)
{
case 0:
break;
}
return null;
}
public void setCallback(int paramInt, Callback paramCallback)
{
switch (paramInt)
{
case 0:
this.CGLIB$CALLBACK_0 = ((MethodInterceptor)paramCallback);
break;
}
}
public Callback[] getCallbacks()
{
CGLIB$BIND_CALLBACKS(this);
return new Callback[] { this.CGLIB$CALLBACK_0 };
}
public void setCallbacks(Callback[] paramArrayOfCallback)
{
this.CGLIB$CALLBACK_0 = ((MethodInterceptor)paramArrayOfCallback[0]);
}
static {}
}
Spring 动态代理(二) - ProxyFactoryBean
Spring 动态代理(三) - BeanNameAutoProxyCreator