Is there a zero time start (no recompilation) switchable condition flag in Java?
I'm looking for a way to provide the fastest possible on / off flag for if conditions (I mean zero time - compile / class load / JIT time resolved) Of course, this condition changes only once each time the application runs – at startup
I know that you can conditionally compile "conditional compile time constants" and remove the entire condition from your code But what is the fastest (and possibly simple) alternative without recompiling the source code?
I can move conditions to separate classes and jar. Conditional method, I generate Jar and open those versions in the classpath when the application starts? Whether jit will be in separate Delete the call to the method in the jar. If the method is found to be empty?
I can provide two classes by implementing "classwithmycondition" in the classpath. One class will have a real implementation, and the second will have only empty methods and pass class Forname and Newinstance() instantiates one of them? Remove JIT calling empty methods from my main loop nested methods?
What can be the simplest bytecode operation to solve this problem?
Solution
You can pass a custom value on the command line and then check the value once So in your code, there is something like this:
final static boolean customProp = "true".equalsIgnoreCase(System.getProperty("customProp"));
Depending on your command line parameters, the static final value will change This sets the value to true:
java -DcustomProp="true" -jar app.jar
Although this sets the value to false:
java -jar app.jar
This gives you the benefit of a static final Boolean, but allows you to change the value without recompiling
[Edit]
As indicated in the comments, this method does not allow optimization at compile time The value of static final Boolean is set on classload and remains unchanged from there The "normal" execution of bytecode may need to evaluate each if (customprop) However, JIT occurs at run time, compiling bytecode into native code At this point, because the bytecode has runtime values, more active optimization can be carried out, such as inline or exclusion code Note that you cannot accurately predict whether or when the JIT will start