`
wangjianjun852
  • 浏览: 50222 次
  • 性别: Icon_minigender_2
  • 来自: 大连
社区版块
存档分类
最新评论

proxy --- 4

阅读更多
----client---
package com.wjj.myproxy;

import com.wjj.proxy.Car;


public class Client {
public static void main(String[] args) {
try {
Moveable car = new Car();
InvocationHandler h = new LogIH(car);
Moveable ca = (Moveable)Proxya.getProxy(Moveable.class,h);
ca.move();
} catch (Exception e) {
e.printStackTrace();
}
}
}


------proxy ---
package com.wjj.myproxy;

import java.io.File;
import java.io.FileWriter;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

import javax.tools.JavaCompiler;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import javax.tools.JavaCompiler.CompilationTask;

import com.wjj.proxy.Car;


public class Proxya {
public static Object getProxy(Class proxyinterface,InvocationHandler h)throws Exception{

Method[] methods = proxyinterface.getMethods();
String methodStr = "";

for(Method m : methods){
methodStr += "@Override\r\n";
methodStr +="public void " + m.getName() + "(){\r\n" +
"try{"+
"Method md = " + proxyinterface.getName() + ".class.getMethod(\"" + m.getName() + "\");\r\n"+
"h.invoke(this,md);\r\n" +
"}catch(Exception e){e.printStackTrace();\r\n}"+

"}";
}


String src = "package com.wjj.myproxy;\r\n"+
"import java.lang.reflect.Method;"+

"public class CarTimeProxy implements "+proxyinterface.getName()+" {"+

"com.wjj.myproxy.InvocationHandler h = null;"+


"public CarTimeProxy(InvocationHandler car) {"+
"super();"+
"this.h = car;"+
"}"+methodStr+

"}";

String fileName = System.getProperty("user.dir") +"/src/com/wjj/myproxy/CarTimeProxy.java";

File file = new File(fileName);
FileWriter writer = new FileWriter(file);
writer.write(src);
writer.flush();
writer.close();

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileMgr = compiler.getStandardFileManager(null, null, null);
Iterable units = fileMgr.getJavaFileObjects(fileName);
CompilationTask t = compiler.getTask(null, fileMgr, null, null, null, units);
t.call();
fileMgr.close();


URL[] urls = new URL[]{new URL("file:/" + System.getProperty("user.dir")+"/src")};
URLClassLoader ul = new URLClassLoader(urls);
Class clazz = ul.loadClass("com.wjj.myproxy.CarTimeProxy");
System.out.println(clazz);


Constructor cos = clazz.getConstructor(InvocationHandler.class);
return cos.newInstance(h);
}
}


-------------------moveable ---
package com.wjj.myproxy;

public interface Moveable {
void move();
}


---------------------invocation---
package com.wjj.myproxy;

import java.lang.reflect.Method;

public interface InvocationHandler {
public void invoke(Object o,Method m);
}



-----------invocatin impl----
package com.wjj.myproxy;

import java.lang.reflect.Method;

public class LogIH implements InvocationHandler {

private Object target;

public LogIH(Object o){
this.target = o;
}


@Override
public void invoke(Object o, Method m) {

try {
System.out.println("invocate log start--------->");
long startTime = System.currentTimeMillis();
m.invoke(target, new Object[]{});
System.out.println("invocate log stop--------->");
System.out.println(System.currentTimeMillis() - startTime);
} catch (Exception e) {
e.printStackTrace();
}
}

}



=========================

这样就 可以对任意对象,实现任意方法的代理, 其代理内写的方法, 就可以自己建一个invocationhandler 的实现类。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics