1

任何机构都可以帮助了解为什么它不打印超时?当我尝试加载图像时?例如,使用 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)); 
4

1 回答 1

0

您永远不会为TracingInvocationHandler. 确保传入完整的参数,例如:

new TracingInvocationHandler(obj, System.out, obj2);

用水和米饭煮饭的机器不能只用水做米饭。

于 2013-09-02T10:51:30.470 回答