查看Proxy产生的代理对象
来源:互联网 发布:广联达套定额软件 编辑:程序博客网 时间:2024/06/03 14:53
package com.demo;
import java.io.FileOutputStream;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import sun.misc.ProxyGenerator;
public class DynamicProxyTest {
public static void writeProxyClassToHardDisk(String path) throws Exception {
// 第一种方法,这种方式在刚才分析ProxyGenerator时已经知道了
// System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", true);
// 第二种方法
// 获取代理类的字节码
byte[] classFile = ProxyGenerator.generateProxyClass("$Proxy11", IAImpl.class.getInterfaces());
FileOutputStream out = new FileOutputStream(path);
out.write(classFile);
out.flush();
out.close();
}
public static void main(String[] args) throws Exception {
//注意再用jd-gui.exe 工具反编译字节码文件,可以看到源文件
writeProxyClassToHardDisk("E:\\workspace2\\jucdemo\\Proxy11.class");
/*
* // 添加以下的几段代码, 就可以将代理生成的字节码保存起来了 Field field =
* System.class.getDeclaredField("props"); field.setAccessible(true);
* Properties props = (Properties) field.get(null);
* props.put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
*
* Package pkg = ProxyTest2.class.getPackage(); if (pkg != null) {
* String packagePath = pkg.getName().replace(".", File.pathSeparator);
* new File(packagePath).mkdirs(); }
*
* IA a = new IAImpl(); InvocationHandlerImpl ih = new
* InvocationHandlerImpl(a); IA proxyA = (IA)
* Proxy.newProxyInstance(a.getClass().getClassLoader(),a.getClass().
* getInterfaces(), ih);
*
* proxyA.a();
*/
}
}
interface IA {
void a();
int b(String str);
}
class IAImpl implements IA {
@Override
public void a() {
System.out.println("IAImpl.a()");
}
@Override
public int b(String str) {
System.out.println("IAImpl.b()");
return 0;
}
}
class InvocationHandlerImpl implements InvocationHandler {
private Object target;
public InvocationHandlerImpl(Object target) {
this.target = target;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("before...");
Object res = method.invoke(target, args);
System.out.println("after...");
return res;
}
}
import java.io.FileOutputStream;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import sun.misc.ProxyGenerator;
public class DynamicProxyTest {
public static void writeProxyClassToHardDisk(String path) throws Exception {
// 第一种方法,这种方式在刚才分析ProxyGenerator时已经知道了
// System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", true);
// 第二种方法
// 获取代理类的字节码
byte[] classFile = ProxyGenerator.generateProxyClass("$Proxy11", IAImpl.class.getInterfaces());
FileOutputStream out = new FileOutputStream(path);
out.write(classFile);
out.flush();
out.close();
}
public static void main(String[] args) throws Exception {
//注意再用jd-gui.exe 工具反编译字节码文件,可以看到源文件
writeProxyClassToHardDisk("E:\\workspace2\\jucdemo\\Proxy11.class");
/*
* // 添加以下的几段代码, 就可以将代理生成的字节码保存起来了 Field field =
* System.class.getDeclaredField("props"); field.setAccessible(true);
* Properties props = (Properties) field.get(null);
* props.put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
*
* Package pkg = ProxyTest2.class.getPackage(); if (pkg != null) {
* String packagePath = pkg.getName().replace(".", File.pathSeparator);
* new File(packagePath).mkdirs(); }
*
* IA a = new IAImpl(); InvocationHandlerImpl ih = new
* InvocationHandlerImpl(a); IA proxyA = (IA)
* Proxy.newProxyInstance(a.getClass().getClassLoader(),a.getClass().
* getInterfaces(), ih);
*
* proxyA.a();
*/
}
}
interface IA {
void a();
int b(String str);
}
class IAImpl implements IA {
@Override
public void a() {
System.out.println("IAImpl.a()");
}
@Override
public int b(String str) {
System.out.println("IAImpl.b()");
return 0;
}
}
class InvocationHandlerImpl implements InvocationHandler {
private Object target;
public InvocationHandlerImpl(Object target) {
this.target = target;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("before...");
Object res = method.invoke(target, args);
System.out.println("after...");
return res;
}
}
阅读全文
0 0
- 查看Proxy产生的代理对象
- Proxy(代理对象)
- Proxy 代理对象结构模式
- 用Proxy创建代理对象
- Proxy代理的应用
- Proxy代理的初识
- springAOP——代理对象的产生及方法调用
- 代理模式(Proxy)-对象结构型模式
- Proxy 代理模式 对象结构型模式
- ExtJs的数据代理proxy
- 2013-Proxy代理的使用
- ExtJs的数据代理proxy
- ExtJs的数据代理proxy
- 【JAVA】 代理Proxy的实现
- PHP的代理Proxy模式
- Java的动态代理Proxy
- java 代理proxy的介绍
- spring框架Proxy-代理模式(动态生成代理对象)
- IIS MiME 添加json类型
- VirtualBox安装Centos7
- window redis 集群配置
- 将二维关联数组不同的键按不同的顺序进行排列
- Async/Await
- 查看Proxy产生的代理对象
- bzoj 3524(主席树+二分)
- Vim--学习笔记
- ognl表达式
- 读Android源码设计模式解析与实战(第一章面向对象的六大原则)
- 关于Bootstrap模态框和table互相传值,以及实现图片上传
- Lucene学习总结之七:Lucene搜索过程解析(2)
- 卷积的输入里串联上标签y
- js 中的数组排序