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

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

问题描述

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

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

我读到,如果您有类似以下内容,if 语句将被消除:

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?

或者这个:

static final SomeEnum VALUE = SomeEnum.FOO;

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

由于分析程序以找到所有死代码(可能类似于停机问题)非常困难/不可能,因此我认为只有少数明确定义的构造(如上面的第一个示例),javac 将可靠地识别和删除.是否有这些结构的完整列表?

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 似乎找到了答案(让我把它们放在一起):

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

14.21. 无法访问的语句"" 的 JLS 规定,一般来说,代码中任何无法到达的语句都被认为是编译时错误,唯一的例外是对 if 语句的特殊处理专门允许条件编译.

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;
}

(当然,else 部分是可选的)

(the else-part is optional, of course)

允许代码消除的所有其他构造,例如 while (false) ...,都是不允许的,并且会导致编译时错误,而不是导致条件编译.

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.

可以在 JLS 的15.28. 常量表达式"一章.可以在此处找到包含更多示例的另一个很棒的页面:Java 中的编译时间常量

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

注意:编译器不需要删除 if 语句的无法访问"部分.javac 似乎可以可靠地做到这一点,但其他编译器可能不会.确定知道的唯一方法是通过反编译检查输出,例如使用 Jon Skeet 建议的 javap -c.

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天全站免登陆