-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.mxy.design.proxy; | ||
|
||
import org.junit.Test; | ||
import sun.misc.ProxyGenerator; | ||
|
||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
|
||
/** | ||
* JDK 生成代理类工具 | ||
*/ | ||
public class ProxyGeneratorUtils { | ||
/** | ||
* 把代理类的字节码写到硬盘上 | ||
* @param path 保存路径 | ||
*/ | ||
public static void writeProxyClassToHardDisk(String path) throws IOException { | ||
// 第一种方法,这种方式在刚才分析ProxyGenerator时已经知道了 | ||
// System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", true); | ||
// 第二种方法 | ||
// 获取代理类的字节码 | ||
byte[] classFile = ProxyGenerator.generateProxyClass("$Proxy11", SchoolImpl.class.getInterfaces()); | ||
FileOutputStream out = null; | ||
out = new FileOutputStream(path); | ||
out.write(classFile); | ||
out.flush(); | ||
} | ||
|
||
@Test | ||
public void write() throws IOException { | ||
ProxyGeneratorUtils.writeProxyClassToHardDisk("./$Proxy11.class"); | ||
} | ||
} |