javac code elimination capabilities

佐手、 提交于 2019-11-29 10:32:47

assylias seems to have found the answer (let me just put it all together):

Chapter "14.21. Unreachable Statements" of the JLS specifies, that, in general, any unreachable statement in the code is considered a compile-time-error, with the only exception being a special treatment of if-statements to specifically allow for conditional compiles.

Therefore, the only construct that may result in code elimination (if the compiler chooses to do so!) is:

if (compileTimeConstantExpression) {
    doThis(); // may be removed if compileTimeConstantExpression == false;
} else {
    doThat(); // may be removed if compileTimeConstantExpression == true;
}

(the else-part is optional, of course)

All other constructs that would allow for code elimination, like for example while (false) ..., are disallowed and cause a compile-time-error instead rather than resulting in conditional compilation.

The definition for what constitutes an acceptable compileTimeConstantExpression can be found in chapter "15.28. Constant Expressions" of the JLS. Another great page with further examples can be found here: Compile Time Constants in Java

Note: There is no requirement for a compiler to remove the "unreachable" sections of an if-stament. javac seems to do this reliably, but other compilers may not. The only way to know for sure is to check the output via decompilation, for example using javap -c as suggested by Jon Skeet.

I have run a few tests and it seems (logically) that javac removes the code iif the condition is a constant expression that evaluates to false.

In summary, constant expressions are expressions that only use constants as operands, i.e. primitives, string literals and final primitives or Strings variables that have been initialised with a constant value.

Note that this is compiler dependent as the JLS does not force the compiler to be that smart as explained at the very bottom of 14.21:

An optimizing compiler may realize that the statement x=3; will never be executed and may choose to omit the code for that statement from the generated class file.

I see that for 2nd example it gets removed too

this was my class

public class Test {

    public static void main(String[] args) throws Exception{
        final int VALUE = 3;
        if (VALUE > 9) System.out.println("VALUE > 9 ???");
    }
}

and this is decompiled version

Compiled from "Test.java"
public class Test extends java.lang.Object{
public Test();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   return

public static void main(java.lang.String[])   throws java.lang.Exception;
  Code:
   0:   return

}

My guess would be that this is implementation specific (Oracle, IBM,...).

If you're interested in Oracle's version, a good place to start looking for resources would be the OpenJDK project: http://openjdk.java.net/groups/compiler/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!