Java 中有效的@SuppressWarnings 警告名称列表是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1205995/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:54:12  来源:igfitidea点击:

What is the list of valid @SuppressWarnings warning names in Java?

javaeclipsewarningscompiler-warningssuppress-warnings

提问by Ron Tuffin

What is the list of valid @SuppressWarningswarning names in Java?

@SuppressWarningsJava中的有效警告名称列表是什么?

The bit that comes in between the ("")in @SuppressWarnings("").

介于 in 之间的("")@SuppressWarnings("")

采纳答案by cletus

It depends on your IDE or compiler.

这取决于您的 IDE 或编译器。

Here is a listfor Eclipse Galileo:

以下是Eclipse Galileo的列表

  • allto suppress all warnings
  • boxingto suppress warnings relative to boxing/unboxing operations
  • castto suppress warnings relative to cast operations
  • dep-annto suppress warnings relative to deprecated annotation
  • deprecationto suppress warnings relative to deprecation
  • fallthroughto suppress warnings relative to missing breaks in switch statements
  • finallyto suppress warnings relative to finally block that don't return
  • hidingto suppress warnings relative to locals that hide variable
  • incomplete-switchto suppress warnings relative to missing entries in a switch statement (enum case)
  • nlsto suppress warnings relative to non-nls string literals
  • nullto suppress warnings relative to null analysis
  • restrictionto suppress warnings relative to usage of discouraged or forbidden references
  • serialto suppress warnings relative to missing serialVersionUID field for a serializable class
  • static-accessto suppress warnings relative to incorrect static access
  • synthetic-accessto suppress warnings relative to unoptimized access from inner classes
  • uncheckedto suppress warnings relative to unchecked operations
  • unqualified-field-accessto suppress warnings relative to field access unqualified
  • unusedto suppress warnings relative to unused code
  • all抑制所有警告
  • 装箱以抑制与装箱/拆箱操作相关的警告
  • 强制转换以抑制与强制转换操作相关的警告
  • dep-ann抑制与已弃用注释相关的警告
  • 弃用以抑制相对于弃用的警告
  • fallthrough以抑制与 switch 语句中缺少中断相关的警告
  • finally抑制相对于不返回的 finally 块的警告
  • 隐藏以抑制相对于隐藏变量的本地人的警告
  • 不完整切换以抑制与 switch 语句中缺少条目相关的警告(枚举案例)
  • nls抑制相对于非 nls 字符串文字的警告
  • null抑制与 null 分析相关的警告
  • 限制抑制与使用不鼓励或禁止的引用相关的警告
  • 串行以抑制与可序列化类的缺失 serialVersionUID 字段相关的警告
  • 静态访问以抑制与不正确静态访问相关的警告
  • 合成访问以抑制与来自内部类的未优化访问相关的警告
  • 未选中以抑制与未选中操作相关的警告
  • unqualified-field-access抑制与字段访问不合格相关的警告
  • 未使用以抑制与未使用代码相关的警告

Listfor Indigo adds:

Indigo列表添加:

  • javadocto suppress warnings relative to javadoc warnings
  • rawtypesto suppress warnings relative to usage of raw types
  • static-methodto suppress warnings relative to methods that could be declared as static
  • superto suppress warnings relative to overriding a method without super invocations
  • javadoc抑制相对于 javadoc 警告的警告
  • rawtypes抑制与原始类型使用相关的警告
  • 静态方法来抑制与可以声明为静态的方法相关的警告
  • super抑制与覆盖没有超级调用的方法相关的警告

Listfor Juno adds:

朱诺名单补充说:

  • resourceto suppress warnings relative to usage of resources of type Closeable
  • sync-overrideto suppress warnings because of missing synchronize when overriding a synchronized method
  • 资源禁止警告相对于类型可关闭的资源的使用
  • 由于在覆盖同步方法时缺少同步而同步覆盖以抑制警告

Kepler and Luna use the same token list as Juno (list).

Kepler 和 Luna 使用与 Juno 相同的令牌列表(list)。

Others will be similar but vary.

其他人将相似但有所不同。

回答by Martin McNulty

All values are permitted (unrecognized ones are ignored). The list of recognized ones is compiler specific.

允许所有值(忽略无法识别的值)。识别的列表是特定于编译器的。

In The Java Tutorialsuncheckedand deprecationare listed as the two warnings required by The Java Language Specification, therefore, they should be valid with all compilers:

Java教程uncheckeddeprecation被列为Java语言规范所要求的两个警告,因此,他们应该是有效的与所有的编译器:

Every compiler warning belongs to a category. The Java Language Specification lists two categories: deprecation and unchecked.

每个编译器警告都属于一个类别。Java 语言规范列出了两类:弃用和未选中。

The specific sections inside The Java Language Specificationwhere they are defined is not consistent across versions. In the Java SE 8 Specification uncheckedand deprecationare listed as compiler warnings in sections 9.6.4.5. @SuppressWarningsand 9.6.4.6 @Deprecated, respectively.

The Java Language Specification中定义它们的特定部分在不同版本之间并不一致。在 Java SE 8 规范中,uncheckeddeprecation在第9.6.4.5节中列为编译器警告@SuppressWarnings9.6.4.6 @Deprecated分别。

For Sun's compiler, running javac -Xgives a list of all values recognized by that version. For 1.5.0_17, the list appears to be:

对于 Sun 的编译器,运行javac -X会给出该版本识别的所有值的列表。对于 1.5.0_17,列表似乎是:

  • all
  • deprecation
  • unchecked
  • fallthrough
  • path
  • serial
  • finally
  • 全部
  • 弃用
  • 未经检查
  • 落空
  • 小路
  • 连续剧
  • 最后

回答by D. Wroblewski

The list is compiler specific. But here are the values supported in Eclipse:

该列表是特定于编译器的。但这里是Eclipse支持的值:

  • allDeprecationdeprecation even inside deprecated code
  • allJavadocinvalid or missing javadoc
  • assertIdentifieroccurrence of assert used as identifier
  • boxingautoboxing conversion
  • charConcatwhen a char array is used in a string concatenation without being converted explicitly to a string
  • conditionAssignpossible accidental boolean assignment
  • constructorNamemethod with constructor name
  • dep-annmissing @Deprecated annotation
  • deprecationusage of deprecated type or member outside deprecated code
  • discourageduse of types matching a discouraged access rule
  • emptyBlockundocumented empty block
  • enumSwitch, incomplete-switchincomplete enum switch
  • fallthroughpossible fall-through case
  • fieldHidingfield hiding another variable
  • finalBoundtype parameter with final bound
  • finallyfinally block not completing normally
  • forbiddenuse of types matching a forbidden access rule
  • hidingmacro for fieldHiding, localHiding, typeHiding and maskedCatchBlock
  • indirectStaticindirect reference to static member
  • intfAnnotationannotation type used as super interface
  • intfNonInheritedinterface non-inherited method compatibility
  • javadocinvalid javadoc
  • localHidinglocal variable hiding another variable
  • maskedCatchBlockshidden catch block
  • nlsnon-nls string literals (lacking of tags //$NON-NLS-)
  • noEffectAssignassignment with no effect
  • nullpotential missing or redundant null check
  • nullDereferencemissing null check
  • over-annmissing @Override annotation
  • paramAssignassignment to a parameter
  • pkgDefaultMethodattempt to override package-default method
  • rawusage a of raw type (instead of a parametrized type)
  • semicolonunnecessary semicolon or empty statement
  • serialmissing serialVersionUID
  • specialParamHidingconstructor or setter parameter hiding another field
  • static-accessmacro for indirectStatic and staticReceiver
  • staticReceiverif a non static receiver is used to get a static field or call a static method
  • superoverriding a method without making a super invocation
  • suppressenable @SuppressWarnings
  • syntheticAccess, synthetic-accesswhen performing synthetic access for innerclass
  • tasksenable support for tasks tags in source code
  • typeHidingtype parameter hiding another type
  • uncheckedunchecked type operation
  • unnecessaryElseunnecessary else clause
  • unqualified-field-access, unqualifiedFieldunqualified reference to field
  • unusedmacro for unusedArgument, unusedImport, unusedLabel, unusedLocal, unusedPrivate and unusedThrown
  • unusedArgumentunused method argument
  • unusedImportunused import reference
  • unusedLabelunused label
  • unusedLocalunused local variable
  • unusedPrivateunused private member declaration
  • unusedThrownunused declared thrown exception
  • uselessTypeCheckunnecessary cast/instanceof operation
  • varargsCastvarargs argument need explicit cast
  • warningTokenunhandled warning token in @SuppressWarnings
  • allDeprecation弃用甚至在弃用的代码中
  • allJavadoc无效或缺少 javadoc
  • assertIdentifier出现断言用作标识符
  • 拳击自动装箱转换
  • charConcat当在字符串连接中使用 char 数组而不显式转换为字符串时
  • conditionAssign可能的意外布尔赋值
  • 带有构造函数名称的constructorName方法
  • dep-ann缺少 @Deprecated 注释
  • 弃用类型或弃用代码之外的成员的弃用用法
  • 不鼓励使用匹配不鼓励访问规则的类型
  • emptyBlock未记录的空块
  • enumSwitch不完整开关不完整枚举开关
  • fallthrough可能的fallthrough案例
  • fieldHiding隐藏另一个变量的字段
  • 具有最终边界的finalBound类型参数
  • finallyfinally块没有正常完成
  • 禁止使用与禁止访问规则匹配的类型
  • fieldHiding、localHiding、typeHiding 和 maskedCatchBlock 的隐藏
  • 对静态成员的indirectStatic间接引用
  • 用作超级接口的intfAnnotation注释类型
  • intfNonInherited接口非继承方法兼容性
  • javadoc无效的 javadoc
  • localHiding局部变量隐藏另一个变量
  • maskedCatchBlocks隐藏的捕获块
  • nls非 nls 字符串文字(缺少标签 //$NON-NLS-)
  • noEffectAssign赋值没有效果
  • 潜在缺失或冗余空检查
  • nullDereference缺少空检查
  • over-ann缺少 @Override 注释
  • paramAssign赋值给一个参数
  • pkgDefaultMethod尝试覆盖包默认方法
  • raw使用 a 原始类型(而不是参数化类型)
  • 分号不必要的分号或空语句
  • 串行缺少serialVersionUID
  • specialParamHiding构造函数或 setter 参数隐藏另一个字段
  • 用于indirectStatic 和staticReceiver 的静态访问
  • staticReceiver如果非静态接收器用于获取静态字段或调用静态方法
  • 超级覆盖一个方法而不进行超级调用
  • 抑制启用@SuppressWarnings
  • syntheticAccess,合成的访问执行对内部类合成访问时
  • 任务启用对源代码中任务标签的支持
  • typeHiding类型参数隐藏另一种类型
  • 未经检查的未经检查的类型操作
  • 不必要的Else不必要的else子句
  • unqualified-field-access, unqualifiedField对字段的限定引用
  • 未使用的宏,用于未使用的参数、未使用的导入、未使用的标签、未使用的本地、未使用的私人和未使用的投掷
  • unusedArgument未使用的方法参数
  • unusedImport未使用的进口参考
  • unusedLabel未使用的标签
  • unusedLocal未使用的局部变量
  • unusedPrivate未使用的私有成员声明
  • unusedThrown未使用的声明抛出的异常
  • uselessTypeCheck不必要的 cast/instanceof operation
  • varargsCastvarargs 参数需要显式转换
  • @SuppressWarnings 中的 warningToken未处理的警告标记

Sun JDK (1.6) has a shorter listof supported warnings:

Sun JDK (1.6) 有一个较短的受支持警告列表

  • deprecationCheck for use of depreciated items.
  • uncheckedGive more detail for unchecked conversion warnings that are mandated by the Java Language Specification.
  • serialWarn about missing serialVersionUID definitions on serializable classes.
  • finallyWarn about finally clauses that cannot complete normally.
  • fallthroughCheck switch blocks for fall-through cases and provide a warning message for any that are found.
  • pathCheck for a nonexistent path in environment paths (such as classpath).
  • 折旧检查使用贬值的物品。
  • unchecked 提供Java Language Specification 强制要求的 unchecked 转换警告的更多详细信息。
  • serial警告可序列化类上缺少 serialVersionUID 定义。
  • finally警告不能正常完成的 finally 子句。
  • fallthrough检查开关块是否有漏掉的情况,并为发现的任何情况提供警告消息。
  • path检查环境路径(例如类路径)中不存在的路径。

The latest available javac (1.6.0_13) for mac have the following supported warnings

mac 最新可用的 javac (1.6.0_13) 支持以下警告

  • all
  • cast
  • deprecation
  • divzero
  • empty
  • unchecked
  • fallthrough
  • path
  • serial
  • finally
  • overrides
  • 全部
  • 投掷
  • 弃用
  • 空的
  • 未经检查
  • 落空
  • 小路
  • 连续剧
  • 最后
  • 覆盖

回答by matteo

And this seems to be a much more complete list, where I found some warnings specific to Android-Studio that I couldn't find elsewhere (e.g. SynchronizeOnNonFinalField)

这似乎是一个更完整的列表,在那里我发现了一些我在其他地方找不到的特定于 Android-Studio 的警告(例如 SynchronizeOnNonFinalField)

https://jazzy.id.au/2008/10/30/list_of_suppresswarnings_arguments.html

https://jazzy.id.au/2008/10/30/list_of_suppresswarnings_arguments.html

Oh, now SO's guidelines contraddict SO's restrictions. On one hand, I am supposed to copy the list rather than providing only the link. But on the other hand, this would exceed the maximum allowed number of characters. So let's just hope the link won't break.

哦,现在 SO 的指导方针与 SO 的限制相矛盾。一方面,我应该复制列表而不是只提供链接。但另一方面,这将超过允许的最大字符数。所以让我们只希望链接不会中断。

回答by Matt Campbell

I just want to add that there is a master list of IntelliJ suppress parameters at: https://gist.github.com/vegaasen/157fbc6dce8545b7f12c

我只想补充一点,IntelliJ 抑制参数的主列表位于:https: //gist.github.com/vegaasen/157fbc6dce8545b7f12c

It looks fairly comprehensive. Partial:

它看起来相当全面。部分的:

Warning Description - Warning Name

"Magic character" MagicCharacter 
"Magic number" MagicNumber 
'Comparator.compare()' method does not use parameter ComparatorMethodParameterNotUsed 
'Connection.prepare*()' call with non-constant string JDBCPrepareStatementWithNonConstantString 
'Iterator.hasNext()' which calls 'next()' IteratorHasNextCallsIteratorNext 
'Iterator.next()' which can't throw 'NoSuchElementException' IteratorNextCanNotThrowNoSuchElementException 
'Statement.execute()' call with non-constant string JDBCExecuteWithNonConstantString 
'String.equals("")' StringEqualsEmptyString 
'StringBuffer' may be 'StringBuilder' (JDK 5.0 only) StringBufferMayBeStringBuilder 
'StringBuffer.toString()' in concatenation StringBufferToStringInConcatenation 
'assert' statement AssertStatement 
'assertEquals()' between objects of inconvertible types AssertEqualsBetweenInconvertibleTypes 
'await()' not in loop AwaitNotInLoop 
'await()' without corresponding 'signal()' AwaitWithoutCorrespondingSignal 
'break' statement BreakStatement 
'break' statement with label BreakStatementWithLabel 
'catch' generic class CatchGenericClass 
'clone()' does not call 'super.clone()' CloneDoesntCallSuperClone

回答by barclay

A new favorite for me is @SuppressWarnings("WeakerAccess")in IntelliJ, which keeps it from complaining when it thinks you should have a weaker access modifier than you are using. We have to have public access for some methods to support testing, and the @VisibleForTestingannotation doesn't prevent the warnings.

我最喜欢的是@SuppressWarnings("WeakerAccess")IntelliJ,当它认为你应该拥有比你正在使用的访问修饰符更弱的访问修饰符时,它不会抱怨。我们必须对某些方法进行公共访问以支持测试,并且@VisibleForTesting注释不会阻止警告。

ETA: "Anonymous" commented, on the page @MattCampbell linked to, the following incredibly useful note:

ETA:“匿名”在@MattCampbell 链接到的页面上评论了以下非常有用的注释:

You shouldn't need to use this list for the purpose you are describing. IntelliJ will add those SuppressWarnings for you automatically if you ask it to. It has been capable of doing this for as many releases back as I remember.

Just go to the location where you have the warning and type Alt-Enter (or select it in the Inspections list if you are seeing it there). When the menu comes up, showing the warning and offering to fix it for you (e.g. if the warning is "Method may be static" then "make static" is IntellJ's offer to fix it for you), instead of selecting "enter", just use the right arrow button to access the submenu, which will have options like "Edit inspection profile setting" and so forth. At the bottom of this list will be options like "Suppress all inspections for class", "Suppress for class", "Suppress for method", and occasionally "Suppress for statement". You probably want whichever one of these appears last on the list. Selecting one of these will add a @SuppressWarnings annotation (or comment in some cases) to your code suppressing the warning in question. You won't need to guess at which annotation to add, because IntelliJ will choose based on the warning you selected.

您不需要将这个列表用于您所描述的目的。如果您要求,IntelliJ 会自动为您添加这些 SuppressWarnings。它已经能够在我记得的尽可能多的版本中做到这一点。

只需转到您收到警告的位置并键入 Alt-Enter(如果您在那里看到它,则在检查列表中选择它)。当菜单出现时,显示警告并提供为您修复它(例如,如果警告是“方法可能是静态的”,那么“使静态”是 IntellJ 提供的为您修复它的提议),而不是选择“输入”,只需使用右箭头按钮访问子菜单,其中将包含“编辑检查配置文件设置”等选项。在此列表的底部将是“禁止所有类检查”、“禁止类”、“禁止方法”和偶尔“禁止语句”之类的选项。您可能想要列表中最后出现的任何一个。选择其中之一将在您的代码中添加@SuppressWarnings 注释(或在某些情况下为注释),以抑制相关警告。您无需猜测要添加哪个注释,因为 IntelliJ 将根据您选择的警告进行选择。

回答by hb0

I noticed that //noinspectioncan be auto-generated in IntelliJ

我注意到//noinspection可以在IntelliJ 中自动生成

  • make sure you have not already a plan @SuppressWarninigsbefore the statement
  • Now you can auto-generate the specific //noinspectionby hitting Alt+Enterwhen you have the warning selected and then use the right arrow keyto see the Suppress for ...option
  • 确保您@SuppressWarninigs在声明之前还没有计划
  • 现在,您可以自动生成特定//noinspection通过点击Alt + Enter键,当你有选择的警告,然后使用右箭头键看到打压...选项

Ended up here when I wanted to suppress a "switch has too few case labels" warning from IntelliJ. I did not find a complete List for IntelliJ's @SuppressWarningsupport but //noinspectiondid the trick for me.

当我想抑制来自 IntelliJ 的“开关的案例标签太少”警告时,到这里结束。我没有找到完整的 IntelliJ@SuppressWarning支持列表,但//noinspection对我有用。

回答by R Strauss

If you're using SonarLint, try above the method or class the whole squid string: @SuppressWarnings("squid:S1172")

如果您正在使用 SonarLint,请尝试在方法上方或对整个鱿鱼字符串进行分类:@SuppressWarnings("squid:S1172")