有没有办法在Java中禁用短路评估? [英] Is there a way to disable short circuit evaluation in Java?
问题描述
说我有这样的代码:
boolean ret = a()&& b()&& c()&& d()&& E();
通常只有在所有其他调用a() - d()返回true时才调用e()。是否有一些编译器或JVM选项可以禁用短路评估,因此无论其他函数的结果如何,总是会调用e()?
基本上我在做UAT庞大的系统和需要测试e(),但设置环境和方案,以确保所有a(),b()等返回true是非常痛苦的...
<编辑:好吧,我想使用位AND而不是逻辑可以提供某种解决方法,但理想情况下我正在寻找一种不需要在源代码中进行任何更改的解决方案。两者都是由于正式和技术原因(正如我提到的系统很大,我们有整个过程,在暂存区域之间推广和部署代码并获得签字)。这仅用于测试,生产版需要启用延迟评估(即使用&&)
POST-MORTEM:
- 正确答案是:不,没有。
- 有用答案:你可以改变&& to&
- 我到底做了什么答案:远程调试系统,在表达式上放置断点并告诉eclipse运行e()-_-
没有用于更改布尔表达式求值的语义的编译器或JVM选项。
如果您无法修改来源,可能(尽管不能保证)选项包括:
- 通过精心设计的
前置条件设置创造性地重新创建您要测试的条件。 - 使用模拟对象。
- 破解编译器。
- 破解JVM。
- 旋转字节代码。
对不起,这些比编译器/ JVM选项或修改源要困难得多。此外,最后三个选项(以及请求的编译器/ JVM选项或修改源)违反了不修改正在测试的内容的正确测试协议。
Say I have code like this:
boolean ret = a() && b() && c() && d() && e();
Usually e() is only called if all other calls a()-d() return true. Is there maybe some compiler or JVM option to disable short circuit evaluation, so e() would be called always, regardless of other functions' results?
Basically I am doing UAT of huge system and need to test e(), however setting up environment and scenario that assures all a(), b() etc. return true is extremely painful...
EDIT: ok, I guess using bit AND instead of logical one could provide SOME sort of workaround, however ideally I am looking for a solution that does not require ANY CHANGES in the source code. Both due to formal and technical reason (as i mentioned system is big and we have whole process of promoting and deploying code between staging areas and getting sign-offs). And this is for testing only, production version needs to have lazy evaluation enabled (i.e. use &&)
POST-MORTEM:
- "Correct" answer is: No, there is not.
- "Useful" answer: you can change && to &
- "What I did in the end" answer: debug system remotely, put breakpoint on expression and told eclipse to run e() -_-
There is no compiler or JVM option for changing the semantics of boolean expression evaluation.
If you cannot modify the source, possible (albeit not guaranteed) options include:
- Creatively recreate the conditions you seek to test via elaborate setup of preconditions.
- Use mock objects.
- Hack the compiler.
- Hack the JVM.
- Twiddle the byte code.
Sorry, those are all much more difficult than a compiler/JVM option or modifying the source. Further, the last three options (as well as the requested compiler/JVM option or modifying the source) violate proper testing protocol of not modifying what's being tested.
这篇关于有没有办法在Java中禁用短路评估?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!