IntelliJ IDEA调试多次进入方法
IntelliJ IDEA debug enter method multiple times
调试以下代码时:
public class MyProxy {
public static void main(String[] args){
Consumer f = (Consumer) Proxy.newProxyInstance(
Consumer.class.getClassLoader(),
new Class[] { Consumer.class },
new Handler(new ConsumerImpl())
);
f.consume("Hello"); // set breakpoint here
System.out.println("done");
}
}
interface Consumer {
void consume(String s);
}
class ConsumerImpl implements Consumer {
public void consume(String s) {
System.out.println(s);
}
}
class Handler implements InvocationHandler {
private final Consumer original;
public Handler(Consumer original) {
this.original = original;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws IllegalAccessException, IllegalArgumentException,
InvocationTargetException {
System.out.println("BEFORE");
method.invoke(original, args);
System.out.println("AFTER");
return null;
}
}
输出为:
BEFORE
AFTER
BEFORE
AFTER
BEFORE
BEFORE
AFTER
Hello
BEFORE
AFTER
AFTER
BEFORE
AFTER
BEFORE
AFTER
BEFORE
AFTER
BEFORE
AFTER
done
BEFORE
AFTER
当调试器停在断点行时,输出已经有两行,当我进入invoke
方法时,输出如图所示。这就像我做的每一步调试器都在进入 invoke
方法。因为如果我不进入方法,输出是:
BEFORE
AFTER
BEFORE
Hello
AFTER
BEFORE
AFTER
done
BEFORE
AFTER
如果我 运行 代码,输出是预期的。
BEFORE
Hello
AFTER
done
是调试器的问题还是我操作有误?
环境:Windows64、Intellij IDEA、JDK8
这不是IDEA中的错误。如果您尝试在没有任何断点的情况下调试它,您可能会得到与预期相同的结果。
但是如果你放置一些断点,IDEA 将尝试调用 toString() 方法,然后调用它可以评估的变量的 hashCode() 方法。
最终对于所有方法,它将调用您的 InvocationHandler 实现(不仅对于 "consume" 方法,您可以打印 method.getName() 以确保调用处理程序实现中的这一点)。
调试以下代码时:
public class MyProxy {
public static void main(String[] args){
Consumer f = (Consumer) Proxy.newProxyInstance(
Consumer.class.getClassLoader(),
new Class[] { Consumer.class },
new Handler(new ConsumerImpl())
);
f.consume("Hello"); // set breakpoint here
System.out.println("done");
}
}
interface Consumer {
void consume(String s);
}
class ConsumerImpl implements Consumer {
public void consume(String s) {
System.out.println(s);
}
}
class Handler implements InvocationHandler {
private final Consumer original;
public Handler(Consumer original) {
this.original = original;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws IllegalAccessException, IllegalArgumentException,
InvocationTargetException {
System.out.println("BEFORE");
method.invoke(original, args);
System.out.println("AFTER");
return null;
}
}
输出为:
BEFORE
AFTER
BEFORE
AFTER
BEFORE
BEFORE
AFTER
Hello
BEFORE
AFTER
AFTER
BEFORE
AFTER
BEFORE
AFTER
BEFORE
AFTER
BEFORE
AFTER
done
BEFORE
AFTER
当调试器停在断点行时,输出已经有两行,当我进入invoke
方法时,输出如图所示。这就像我做的每一步调试器都在进入 invoke
方法。因为如果我不进入方法,输出是:
BEFORE
AFTER
BEFORE
Hello
AFTER
BEFORE
AFTER
done
BEFORE
AFTER
如果我 运行 代码,输出是预期的。
BEFORE
Hello
AFTER
done
是调试器的问题还是我操作有误?
环境:Windows64、Intellij IDEA、JDK8
这不是IDEA中的错误。如果您尝试在没有任何断点的情况下调试它,您可能会得到与预期相同的结果。 但是如果你放置一些断点,IDEA 将尝试调用 toString() 方法,然后调用它可以评估的变量的 hashCode() 方法。
最终对于所有方法,它将调用您的 InvocationHandler 实现(不仅对于 "consume" 方法,您可以打印 method.getName() 以确保调用处理程序实现中的这一点)。