如何抑制特定函数的 Java 编译器警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/593996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to suppress Java compiler warnings for specific functions
提问by Ron Tuffin
We are always taught to make sure we use a breakin switch statements to avoid fall-through.
我们总是被教导要确保在 switch 语句中使用break以避免失败。
The Java compiler warns about these situations to help us not make trivial (but drastic) errors.
Java 编译器会针对这些情况发出警告,以帮助我们避免出现轻微(但严重)的错误。
I have, however, used case fall-through as a feature (we don't have to get into it here, but it provides a very elegant solution).
但是,我使用了 case fall-through 作为一个特性(我们不必在这里讨论它,但它提供了一个非常优雅的解决方案)。
However the compiler spits out massive amounts of warnings that may obscure warnings that I need to know about. I know how I can change the compiler to ignore ALL fall-through warnings, but I would like to implement this on a method-by-method basis to avoid missing a place where I did not intend for fall-through to happen.
然而,编译器会吐出大量警告,这些警告可能会掩盖我需要了解的警告。我知道如何更改编译器以忽略所有失败警告,但我想在逐个方法的基础上实现这一点,以避免错过我不打算发生失败的地方。
Any Ideas?
有任何想法吗?
采纳答案by starblue
If you really, really must do this, and you are sure you are not making a mistake, check out the @SuppressWarnings annotation. I suppose in your case you need
如果您真的,真的必须这样做,并且您确定自己没有犯错,请查看@SuppressWarnings 注释。我想在你的情况下你需要
@SuppressWarnings("fallthrough")
回答by Romain Linsolas
回答by Nicolas
To complete other answer about SuppressWarnings
:
要完成关于 的其他答案SuppressWarnings
:
@SuppressWarnings("fallthrough")
Try to supress all the fall-through warning at the compiler level is a bad thing: as you've explained, the cases where you need to pass through the warning are clearly identified. Thus, it should be explicitly written in the code (the @SuppressWarnings("fallthrough")
annotation with an optionnal comment is welcome). Doing so, you'll still have the fall-through warning if you really forget a break somewhere elese in your code.
尝试在编译器级别抑制所有失败警告是一件坏事:正如您所解释的那样,您需要通过警告的情况被清楚地识别出来。因此,它应该明确地写在代码中(@SuppressWarnings("fallthrough")
欢迎带有可选注释的注释)。这样做,如果您真的忘记了代码中其他地方的中断,您仍然会收到失败警告。
回答by Maxwell
You could create a structure of 'if' statements in place of the switch. Might not be as visually pleasing however neither is warning supression.
您可以创建一个“if”语句结构来代替 switch。视觉效果可能不那么令人愉悦,但警告抑制也不是。
回答by Jay
@SuppressWarnings("fallthrough")
Java has always followed the C-style of switch statements, where you need to explicititly break out of a switch unless you wish to simply fall through and execute the code in the case below. This can be dangerous of course and errors of this kind can be very hard to track down. In the following example, case 1 lacks a break:
Java 一直遵循 C 风格的 switch 语句,在这种情况下,您需要明确地跳出 switch ,除非您希望简单地执行以下示例中的代码。当然,这可能很危险,而且此类错误很难追查。在以下示例中,案例 1 缺少中断:
@SuppressWarnings("fallthrough")
public void fallthroughTest(int i)
{
switch (i)
{
case 1:
// Execute both 1 and 2
case 2:
// Execute only 1
}
}