javac代码消除功能 [英] javac code elimination capabilities

查看:149
本文介绍了javac代码消除功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我很难找到有关 javac 的代码删除功能的信息:



我读到,如果你有类似下面的东西, if - 语句将被消除:

  static final boolean DEBUG = false; 

if(DEBUG)System.out.println(Hello World!); //将被删除

但是这样做,例如:

  static final int VALUE = 3; 

if(VALUE> 9)System.out.println(VALUE> 9 ???); //会被删除吗?

或:

  static final SomeEnum VALUE = SomeEnum.FOO; 

if(VALUE == SomeEnum.BAR)System.out.println(Bar ???); //会被删除吗?

因为分析程序找到所有死代码非常困难或不可能问题),我想象,只有几个良好定义的结构(像上面的第一个例子), javac 将可靠地识别和删除。有这些结构的完整列表吗?

解决方案

assylias似乎找到了答案让我把它放在一起):



JLS的章节14.21。无法访问的语句指定,一般来说,代码中的任何不可达语句被认为是编译时错误,唯一的例外 c>



因此, only 构造可能会导致代码消除(如果编译器选择这样做):

  if(compileTimeConstantExpression){
doThis(); //如果compileTimeConstantExpression == false则可能被删除;
} else {
doThat(); //可以删除如果compileTimeConstantExpression == true;
}

else -part是可选的,当然)



所有其他允许代码消除的结构,例如 while(false) / code>,不允许使用编译时错误,而不是导致条件编译。



$ c> compileTimeConstantExpression 可以在 JLS的章节15.28。常量表达式。另一个包含更多示例的页面可以在这里找到:编译Java中的时间常数 p>

注意:没有要求编译器删除的无法访问部分,如果 -stament 。 javac 似乎可靠地执行此操作,但其他编译器可能不会。确定的唯一方法是通过反编译来检查输出,例如使用由Jon Skeet建议的 javap -c


I'm having a hard time finding information about javac's code elimination capabilities:

I read that if you have something like the following, the if-statement will be eliminated:

static final boolean DEBUG = false;

if (DEBUG) System.out.println("Hello World!"); // will be removed

But how about this, for example:

static final int VALUE = 3;

if (VALUE > 9) System.out.println("VALUE > 9 ???"); // will this be removed?

Or this:

static final SomeEnum VALUE = SomeEnum.FOO;

if (VALUE==SomeEnum.BAR) System.out.println("Bar???"); // will this be removed?

Since it's very difficult/impossible to analyze a program to find all dead code (probably similar to the halting problem), I would imagine that there are only a few well-defined constructs (like the first example above), which javac will recognize and remove reliably. Is there a comprehensive list of these constructs?

解决方案

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.

这篇关于javac代码消除功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆