【发布时间】:2013-09-02 10:49:35
【问题描述】:
任何机构都可以帮助了解为什么它不打印超时? 当我尝试加载图像时?例如,使用 java eclipse..currenttimemillis() 加载图像需要多长时间 我想提前对你说声谢谢
import java.io.PrintWriter;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Date;
public class TracingInvocationHandler implements InvocationHandler {
private Object target;
private PrintWriter out;
private Object obj;
public TracingInvocationHandler(Object target, PrintWriter out,Object obj) {
this.target = target;
this.out = out;
this.obj = obj;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
long startTime = System.currentTimeMillis();
Object result = null;
out.println("Image " + method.getName() + " (...) entered.");
result = method.invoke(obj, args);
out.println("Image " + method.getName() + " (...) returned.");
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
String strTotalTime = String.valueOf(totalTime);
System.out.printf(" [%s] %s Image %s took %d ms:",new Date().toString(), method.getName(),args[0],strTotalTime);
return result;
}
public static Object createProxy(Object target, PrintWriter out,Object obj) {
Class<?> targetClass = target.getClass();
ClassLoader currentClassLoader = targetClass.getClassLoader();
Class<?>[] interfaces = targetClass.getInterfaces();
InvocationHandler handler = new TracingInvocationHandler(target, out,obj);
return Proxy.newProxyInstance(currentClassLoader, interfaces, handler);
}
public static Object newInstance(Object obj){ //create a new instance
ClassLoader loader = obj.getClass().getClassLoader();
Class[] classes = obj.getClass().getInterfaces();
return Proxy.newProxyInstance(
loader, classes, new TracingInvocationHandler(obj));
}
}
error message: the constructor TracingInvocationHanlder(Object) is undefined
error message at last line : new TracingInvocationHandler(obj));
【问题讨论】:
-
对代码块使用一致且符合逻辑的缩进。代码的缩进是为了帮助人们理解程序流程。