简体   繁体   English

如何抑制Eclipse 3.5的死代码警告

[英]How do I suppress Eclipse 3.5's warnings of dead code

I use a class for detecting email addresses which uses static final booleans to configure the matching behavior. 我使用一个类来检测使用静态最终布尔值来配置匹配行为的电子邮件地址 Since I upgraded to Eclipse 3.5 I get warnings about dead code, since Eclipse notices that one branch in this can not be reached: 自从我升级到Eclipse 3.5后,我收到有关死代码的警告,因为Eclipse注意到无法访问其中的一个分支:

private static final boolean ALLOW_DOMAIN_LITERALS = false;
private static final String domain = ALLOW_DOMAIN_LITERALS ? rfc2822Domain : rfc1035DomainName;

Oddly enough it is happy with this: 奇怪的是,它很满意:

private static final String domain;
static {
    if(ALLOW_DOMAIN_LITERALS) {
        domain = rfc2822Domain;
    } else {
        domain= rfc1035DomainName;
    }
}

since it seems to recognize the common if(DEBUG) pattern, but the ternary operator doesn't seem to count. 因为它似乎认识到常见的if(DEBUG)模式,但三元运算符似乎并不重要。

Since I'd rather not fork the class too much just to keep Eclipse happy, I'd prefer putting an @SuppressWarnings at the top instead of changing the code. 因为我不想太过分叉,只是为了让Eclipse保持开心,我宁愿把@SuppressWarnings放在顶部,而不是改变代码。 Unfortunately I can't find a matching one apart from the brute-force "all" . 不幸的是,除了蛮力"all"之外,我找不到匹配的。 Is there a value just for the dead code detection? 是否只有死代码检测的值?

UPDATE : from Adam's comment: 更新 :来自Adam的评论:

In Eclipse 3.6 and newer Eclipse versions @SuppressWarnings("unused") can now be used to suppress 'dead code' warnings. 在Eclipse 3.6和更新的Eclipse版本中,现在可以使用@SuppressWarnings("unused")来抑制“死代码”警告。 See Christopher Stock's answer . 请参阅Christopher Stock的回答

See also Eclipse 4.4(Luna) help for @SuppressWarnings. 另请参阅@SuppressWarnings的Eclipse 4.4(Luna)帮助

Original answer: 原始答案:

All SuppressWarnings values Eclipse 3.5 "knows" are listed in this page . Eclipse 3.5“know”的所有SuppressWarnings值都列在此页面中 It seems that there is no value for suppressing only the new dead-code detection. 似乎没有用于仅抑制新的死码检测的价值。 But you can use the @SuppressWarnings("all") just before the domain declaration so it will suppress warnings for only that line not for the whole class: 但是您可以在domain声明之前使用@SuppressWarnings("all") ,因此它将仅针对整个类禁止该行的警告:

private static final boolean ALLOW_DOMAIN_LITERALS = false;
@SuppressWarnings("all") 
private static final String domain = ALLOW_DOMAIN_LITERALS ? rfc2822Domain : rfc1035DomainName;

Because dead code check is a new one you can also suggest an enchancement in the Eclipse bug database for supporting the ternary operation as well. 由于死代码检查是一个新的,您还可以建议在Eclipse错误数据库中增强以支持三元操作。

在“ Potential programming problems部分的“ Windows -> Preferences > Java > Compiler > Errors/Warnings选择“ Ignore Windows -> Preferences > Java > Compiler > Errors/Warnings

You can disable the 'dead code' warnings using 您可以使用禁用“死代码”警告

@SuppressWarnings( "unused" )

See the eclipse documentation for more Information: 有关更多信息,请参阅eclipse文档:

http://help.eclipse.org/kepler/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftask-suppress_warnings.htm http://help.eclipse.org/kepler/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftask-suppress_warnings.htm

"unused" to suppress warnings relative to unused code and dead code “unused”用于禁止相对于未使用的代码和死代码的警告

Greetings 问候

Christopher 克里斯托弗

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM