forked from crossoverJie/JCSprout
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJDKProxyTest.java
More file actions
44 lines (39 loc) Β· 1.27 KB
/
JDKProxyTest.java
File metadata and controls
44 lines (39 loc) Β· 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.crossoverjie.proxy;
import com.crossoverjie.proxy.jdk.CustomizeHandle;
import com.crossoverjie.proxy.jdk.ISubject;
import com.crossoverjie.proxy.jdk.impl.ISubjectImpl;
import org.junit.Test;
import sun.misc.ProxyGenerator;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Proxy;
/**
* Function: JDK 代ηεζ΅
*
* @author crossoverJie
* Date: 23/12/2017 22:40
* @since JDK 1.8
*/
public class JDKProxyTest {
@Test
public void test(){
CustomizeHandle handle = new CustomizeHandle(ISubjectImpl.class) ;
ISubject subject = (ISubject) Proxy.newProxyInstance(JDKProxyTest.class.getClassLoader(), new Class[]{ISubject.class}, handle);
subject.execute() ;
}
@Test
public void clazzTest(){
byte[] proxyClassFile = ProxyGenerator.generateProxyClass(
"$Proxy1", new Class[]{ISubject.class}, 1);
try {
FileOutputStream out = new FileOutputStream("/Users/chenjie/Documents/$Proxy1.class") ;
out.write(proxyClassFile);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}