这只是我目前正在工作的一部分,我想使用 currentTimeMillis() 来打印加载图像的时间 为什么它不起作用?
package method;
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;
public TracingInvocationHandler(Object target, PrintWriter out) {
this.target = target;
this.out = out;
}
@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(target, args);
out.println("Image " + method.getName() + " (...) returned.");
long endTime = System.currentTimeMillis();
System.out.printf(" [%s] %s Image %s took %d ms:",new Date().toString(), method.getName(),args[0], (endTime - startTime) + "ms");
return result;
}
public static Object createProxy(Object target, PrintWriter out) {
Class<?> targetClass = target.getClass();
ClassLoader currentClassLoader = targetClass.getClassLoader();
Class<?>[] interfaces = targetClass.getInterfaces();
InvocationHandler handler = new TracingInvocationHandler(target, out);
return Proxy.newProxyInstance(currentClassLoader, interfaces, handler);
}