Skip to content

Commit

Permalink
refactor:增加生成Jdk代理对象工具
Browse files Browse the repository at this point in the history
  • Loading branch information
maxy19 committed Dec 14, 2019
1 parent b69409a commit ac95e86
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/test/com/mxy/design/proxy/ProxyGeneratorUtils.java
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");
}
}

0 comments on commit ac95e86

Please sign in to comment.