simpledynamicproxy22.java
来自「JAVA编程思想第四版英文原版习题答案. pdf原版的」· Java 代码 · 共 41 行
JAVA
41 行
// typeinfo/SimpleDynamicProxy22.java
// TIJ4 Chapter Typeinfo, Exercise 22, page 598
// Modify SimpleDynamicProxy.java so that it measures method-call times.
import java.lang.reflect.*;
import java.util.*;
class DynamicProxyHandler implements InvocationHandler {
private Object proxied;
public DynamicProxyHandler(Object proxied) {
this.proxied = proxied;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
long timeIn = new Date().getTime();
System.out.println("**** proxy: " + proxy.getClass() +
", method: " + method + ", args: " + args +
", invoked at " + timeIn + " on " + (new Date()));
if(args != null)
for(Object arg : args)
System.out.println(" " + args);
long timeOut = new Date().getTime();
System.out.println("Method call-return time: " + (timeOut - timeIn) + " msecs");
return method.invoke(proxied, args);
}
}
class SimpleDynamicProxy22 {
public static void consumer(Interface iface) {
iface.doSomething();
iface.somethingElse("bonobo");
}
public static void main(String[] args) {
RealObject real = new RealObject();
consumer(real);
// Insert a proxy and call again:
Interface proxy = (Interface)Proxy.newProxyInstance(
Interface.class.getClassLoader(),
new Class[]{ Interface.class },
new DynamicProxyHandler(real));
consumer(proxy);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?