動(dòng)態(tài)代理
JDK動(dòng)態(tài)代理
源碼
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class DynamicProxyTest {
interface IHello {
void sayHello();
}
static class Hello implements IHello {
@Override
public void sayHello() {
System.out.println("hello");
}
}
static class DynamicProxy implements InvocationHandler {
Object originalObj;
public DynamicProxy(Object originalObj) {
this.originalObj = originalObj;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("say hello before");
Object result = method.invoke(originalObj, args);
System.out.println("say hello after");
return result;
}
}
public static void main(String[] args) {
Hello hello = new Hello();
DynamicProxy dynamicProxy = new DynamicProxy(hello);
IHello helloProxy = (IHello) Proxy.newProxyInstance(hello.getClass().getClassLoader(), hello.getClass().getInterfaces(), dynamicProxy);
helloProxy.sayHello();
}
}
Class文件
javac -encoding UTF-8 DynamicProxyTest.java
DynamicProxyTest$IHello
interface DynamicProxyTest$IHello {
void sayHello();
}
DynamicProxyTest$Hello
class DynamicProxyTest$Hello implements IHello {
DynamicProxyTest$Hello() {
}
public void sayHello() {
System.out.println("hello");
}
}
DynamicProxyTest$DynamicProxy
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
class DynamicProxyTest$DynamicProxy implements InvocationHandler {
Object originalObj;
public DynamicProxyTest$DynamicProxy(Object var1) {
this.originalObj = var1;
}
public Object invoke(Object var1, Method var2, Object[] var3) throws Throwable {
System.out.println("say hello before");
Object var4 = var2.invoke(this.originalObj, var3);
System.out.println("say hello after");
return var4;
}
}
DynamicProxyTest
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class DynamicProxyTest {
public DynamicProxyTest() {
}
public static void main(String[] var0) {
DynamicProxyTest.Hello var1 = new DynamicProxyTest.Hello();
DynamicProxyTest.DynamicProxy var2 = new DynamicProxyTest.DynamicProxy(var1);
DynamicProxyTest.IHello var3 = (DynamicProxyTest.IHello)Proxy.newProxyInstance(var1.getClass().getClassLoader(), var1.getClass().getInterfaces(), var2);
var3.sayHello();
}
static class DynamicProxy implements InvocationHandler {
Object originalObj;
public DynamicProxy(Object var1) {
this.originalObj = var1;
}
public Object invoke(Object var1, Method var2, Object[] var3) throws Throwable {
System.out.println("say hello before");
Object var4 = var2.invoke(this.originalObj, var3);
System.out.println("say hello after");
return var4;
}
}
static class Hello implements DynamicProxyTest.IHello {
Hello() {
}
public void sayHello() {
System.out.println("hello");
}
}
interface IHello {
void sayHello();
}
}
字節(jié)碼
javap -c DynamicProxyTest$Hello
警告: 二進(jìn)制文件DynamicProxyTest$Hello包含com.james.example.DynamicProxyTest$Hello
Compiled from "DynamicProxyTest.java"
class com.james.example.DynamicProxyTest$Hello implements com.james.example.DynamicProxyTest$IHello {
com.james.example.DynamicProxyTest$Hello();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public void sayHello();
javap -c DynamicProxyTest$DynamicProxy
警告: 二進(jìn)制文件DynamicProxyTest$DynamicProxy包含com.james.example.DynamicProxyTest$DynamicProxy
Compiled from "DynamicProxyTest.java"
class com.james.example.DynamicProxyTest$DynamicProxy implements java.lang.reflect.InvocationHandler {
java.lang.Object originalObj;
public com.james.example.DynamicProxyTest$DynamicProxy(java.lang.Object);
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: aload_0
5: aload_1
6: putfield #2 // Field originalObj:Ljava/lang/Object;
9: return
public java.lang.Object invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[]) throws java.lang.Throwable;
Code:
0: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #4 // String say hello before
5: invokevirtual #5 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: aload_2
9: aload_0
10: getfield #2 // Field originalObj:Ljava/lang/Object;
13: aload_3
14: invokevirtual #6 // Method java/lang/reflect/Method.invoke:(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;
17: astore 4
19: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
22: ldc #7 // String say hello after
24: invokevirtual #5 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
27: aload 4
29: areturn
}
javap -c DynamicProxyTest
Compiled from "DynamicProxyTest.java"
public class com.james.example.DynamicProxyTest {
public com.james.example.DynamicProxyTest();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: new #2 // class com/james/example/DynamicProxyTest$Hello
3: dup
4: invokespecial #3 // Method com/james/example/DynamicProxyTest$Hello."<init>":()V
7: astore_1
8: new #4 // class com/james/example/DynamicProxyTest$DynamicProxy
11: dup
12: aload_1
13: invokespecial #5 // Method com/james/example/DynamicProxyTest$DynamicProxy."<init>":(Ljava/lang/Object;)V
16: astore_2
17: aload_1
18: invokevirtual #6 // Method java/lang/Object.getClass:()Ljava/lang/Class;
21: invokevirtual #7 // Method java/lang/Class.getClassLoader:()Ljava/lang/ClassLoader;
24: aload_1
25: invokevirtual #6 // Method java/lang/Object.getClass:()Ljava/lang/Class;
28: invokevirtual #8 // Method java/lang/Class.getInterfaces:()[Ljava/lang/Class;
31: aload_2
32: invokestatic #9 // Method java/lang/reflect/Proxy.newProxyInstance:(Ljava/lang/ClassLoader;[Ljava/lang/Class;Ljava/lang/reflect/InvocationHandler;)Ljava/lang/Object;
35: checkcast #10 // class com/james/example/DynamicProxyTest$IHello
38: astore_3
39: aload_3