获得JDK动态代理生成类$Proxy0的内容

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012736913/article/details/87098795

因为:

       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();
}

执行后,生成:

猜你喜欢

转载自blog.csdn.net/u012736913/article/details/87098795
今日推荐