IntelliJ IDEA调试多次输入方法

时间:2017-03-23 09:43:25

标签: java debugging intellij-idea dynamic-proxy

调试以下代码时:

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

是调试器的错误还是我做错了什么?

环境:Windows 64,Intellij IDEA,JDK8

1 个答案:

答案 0 :(得分:2)

这不是IDEA中的错误。如果您尝试在没有任何断点的情况下调试此方法,则可能会得到与预期相同的结果。 但是如果你设置一些断点,IDEA将尝试调用toString()方法,然后调用它可以评估的变量的hashCode()方法。

最终对于所有方法,它将调用您的InvocationHandler实现(不仅对于“consume”方法,您可以打印method.getName()以确保在调用处理程序实现中这样做。)