获得JDK动态代理生成类$Proxy0的内容
因为:
Person person = (Person) new MyProxy().getInstance(new Zhangsan());
System.out.println("main——>"+person.getClass()); 输出为:main——>class com.sun.proxy.$Proxy0
可通过以下方法可以得到$Proxy0的内容:
byte [] bytes = ProxyGenerator.generateProxyClass( "$Proxy0", new Class <?> [] {Person.class} ) ;
具体代码块如下:
byte [] bytes = ProxyGenerator
.generateProxyClass( "$Proxy0", new Class <?> [] {Person.class} );
String pathDir="E:\\workspace-myeclipse2018\\out";
String path="\\$Proxy0.class";
File f = new File(pathDir);
if(!f.exists()) {
f.mkdir();
}
path=f.getAbsolutePath()+path;
f = new File(path);
if(f.exists()) {
f.delete();
}
f.createNewFile();
try( FileOutputStream fos = new FileOutputStream(path)){
fos.write(bt, 0, bt.length);
}catch(Exception e) {
e.printStackTrace();
}
执行后,生成: