Is there a way to disable short circuit evaluation in Java?
boolean-logic, java, semantics
Solution
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.
Problem
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() -_-