Java 6:不支持的@SuppressWarnings("rawtypes") 警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3646770/
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
Java 6: Unsupported @SuppressWarnings("rawtypes") warning
提问by eold
I moved to a new machine which has the latest Sun's Java compiler and noticed some warnings in the existing Java 6 code. The Eclipse IDE, suggested that I annotate the assignment with:
我搬到一台装有最新 Sun 的 Java 编译器的新机器上,并注意到现有 Java 6 代码中的一些警告。Eclipse IDE 建议我对作业进行注释:
@SuppressWarnings("rawtypes")
For example:
例如:
class Foo<T> {
...
}
...
@SuppressWarnings("rawtypes")
Foo foo = new Foo();
When I moved back to the machine with the older compiler (JDK 1.6.0_20), I have noticed that this older compiler now warns about the suppression of "rawtypes" warnings, claiming that this suppression is unsupported and proposing to replace it with @SuppressWarnings("unchecked"). Also, there were some places which the newest compiler, by default, made me to put both "unchecked" and "rawtypes" - compiling that code with the older compiler reproduces the same warning.
当我用旧的编译器(JDK 1.6.0_20)移回机器时,我注意到这个旧的编译器现在警告“原始类型”警告的抑制,声称这种抑制不受支持并建议用@SuppressWarnings 替换它(“未选中”)。此外,在某些地方,默认情况下,最新的编译器让我同时放置“unchecked”和“rawtypes”——使用旧编译器编译该代码会重现相同的警告。
How can I enforce backward/forward compatibility between the two, so that neither compiler produces warnings?
如何在两者之间强制向后/向前兼容,以便两个编译器都不会产生警告?
采纳答案by Colin Hebert
You can use the @SuppressWarnings("unchecked")
which is supported by both the eclipse compiler and javac.
您可以使用@SuppressWarnings("unchecked")
eclipse 编译器和 javac 都支持的 。
But remember the @SuppressWarnings
annotation is used by your compiler which can have its own values. The JLS only forces the compiler to understand the values "unchecked" and "deprecated" (for now).
但请记住@SuppressWarnings
,您的编译器使用的注释可以有自己的值。JLS 仅强制编译器理解值“unchecked”和“deprecated”(目前)。
Compiler vendors should document the warning names they support in conjunction with this annotation type. They are encouraged to cooperate to ensure that the same names work across multiple compilers.
编译器供应商应结合此注释类型记录他们支持的警告名称。鼓励他们合作以确保相同的名称可以跨多个编译器工作。
If you use Helios, you will need to set a specific option to allow @SuppressWarnings("unchecked")
instead of @SuppressWarnings("rawtypes")
,
如果你使用 Helios,你需要设置一个特定的选项来允许@SuppressWarnings("unchecked")
而不是@SuppressWarnings("rawtypes")
,
In case it is not possible to update the code with the new token, the
suppressRawWhenUnchecked=true
system property can be set when starting Eclipse.
如果无法使用新令牌更新代码,则
suppressRawWhenUnchecked=true
可以在启动 Eclipse 时设置系统属性。
Resources :
资源 :
- JLS - @SuppressWarnings()
- Eclipse JDT(Java Compiler, New "rawtypes" token for @SuppressWarnings annotation)
- JLS - @SuppressWarnings()
- Eclipse JDT(Java 编译器,@SuppressWarnings 注释的新“rawtypes”标记)
EDIT: Here is the now unavailable knol article that was used as a reference, originally written by Alex Miller.
编辑:这是现在不可用的 knol 文章,用作参考,最初由Alex Miller撰写。
@SuppressWarnings Annotation in Java
Standard annotation for suppressing various warnings
The SuppressWarnings annotation was added as a standard annotation in Java SE 5.
Definition
The @SuppressWarningsannotation is defined in the Java Language Specification section 9.6.1.5. This section states:
The annotation type
SuppressWarnings
supports programmer control over warnings otherwise issued by the Java compiler. It contains a single element that is an array ofString
. If a program declaration is annotated with the annotation@SuppressWarnings(value = {S1, ... , Sk})
, then a Java compiler must not report any warning identified by one of S1, ... , Sk if that warning would have been generated as a result of the annotated declaration or any of its parts.Unchecked warnings are identified by the string "
unchecked
".The subsequent sectionon
@Deprecation
also mentions that these warnings can be suppressed with@SuppressWarnings("deprecation")
.Valid Warning Types
The only two warning strings that are mentioned in the specification itself are "unchecked" and "deprecation". However, the Sun JDK uses a larger set of strings in the compiler. You can determine the current set by executing:
javac -X
which will show you (among other things) the valid settings for -Xlint.
For example, Sun JDK 1.5 shows:
- all - suppress all warnings from this code
- deprecation - suppress warnings from using deprecated code
- unchecked - suppress warnings from an unchecked call or an unchecked cast
- fallthrough - suppress warnings if a switch falls through without finding a valid case (and no default)
- path -
- serial - suppress warnings if a Serializable class does not define a serialVersionUID
- finally - suppress warnings from return within a finally (which will ignore return with the try)
And Sun JDK 1.6 adds:
- cast
- divzero - suppress warnings if integer divide by zero is detected
- empty
- overrides
- none
IDEs and static analysis tools typically support a large number of other possible values for @SuppressWarnings. These values correspond to specific static analysis checks performed by the IDE.
Eclipse
The Eclipse warning values for Eclipse 3.3 are documented in the JDT docs.
- all - suppress all warnings
- boxing - suppress warnings relative to boxing/unboxing operations
- cast - suppress warnings relative to cast operations
- dep-ann - suppress warnings relative to deprecated annotation
- deprecation - suppress warnings relative to deprecation
- fallthrough - suppress warnings relative to missing breaks in switch statements
- finally - suppress warnings relative to finally block that don't return
- hiding - suppress warnings relative to locals that hide variable
- incomplete-switch - suppress warnings relative to missing entries in a switch statement (enum case)
- nls - suppress warnings relative to non-nls string literals
- null - suppress warnings relative to null analysis
- restriction - suppress warnings relative to usage of discouraged or forbidden references
- serial - suppress warnings relative to missing serialVersionUID field for a serializable class
- static-access - suppress warnings relative to incorrect static access
- synthetic-access - suppress warnings relative to unoptimized access from inner classes
- unchecked - suppress warnings relative to unchecked operations
- unqualified-field-access - suppress warnings relative to field access unqualified
- unused - suppress warnings relative to unused code
IntelliJ
NetBeans
Examples
An example of specifying a single warning:
@SuppressWarnings("unchecked") public void methodWithScaryWarnings() { List rawList = new ArrayList(); List<String> stringList = (List<String>)rawList; }
An example of using two warnings:
@SuppressWarnings({"unchecked","deprecation"}) public void methodWithScaryWarnings() { callDeprecatedMethod(); }
Java中的@SuppressWarnings注解
抑制各种警告的标准注解
SuppressWarnings 注解是在 Java SE 5 中作为标准注解添加的。
定义
的@SuppressWarnings注释在Java语言规范定义的节9.6.1.5。本节指出:
注释类型
SuppressWarnings
支持程序员控制由 Java 编译器发出的警告。它包含一个元素,该元素是String
. 如果程序声明使用 annotation 进行注释@SuppressWarnings(value = {S1, ... , Sk})
,则 Java 编译器不得报告由 S1、...、Sk 之一标识的任何警告,如果该警告是由带注释的声明或其任何部分的结果生成的。未经检查的警告由字符串“
unchecked
”标识。随后的部分上
@Deprecation
还提到,这些警告可与被抑制@SuppressWarnings("deprecation")
。有效警告类型
规范中提到的仅有的两个警告字符串是“unchecked”和“deprecation”。但是,Sun JDK 在编译器中使用了更大的字符串集。您可以通过执行以下命令来确定当前集合:
javac -X
这将向您显示(除其他外)-Xlint 的有效设置。
例如,Sun JDK 1.5 显示:
- all - 禁止来自此代码的所有警告
- 弃用 - 禁止使用弃用代码的警告
- unchecked - 禁止来自未经检查的调用或未经检查的强制转换的警告
- fallthrough - 如果开关失败而没有找到有效案例(并且没有默认值),则抑制警告
- 小路 -
- serial - 如果 Serializable 类未定义 serialVersionUID,则取消警告
- finally - 禁止在 finally 内返回警告(这将忽略尝试返回)
Sun JDK 1.6 添加了:
- 投掷
- divzero - 如果检测到整数除以零则取消警告
- 空的
- 覆盖
- 没有任何
IDE 和静态分析工具通常支持 @SuppressWarnings 的大量其他可能值。这些值对应于 IDE 执行的特定静态分析检查。
蚀
JDT 文档中记录了 Eclipse 3.3 的 Eclipse 警告值。
- all - 取消所有警告
- 装箱 - 抑制与装箱/拆箱操作相关的警告
- cast - 抑制与转换操作相关的警告
- dep-ann - 抑制与已弃用注释相关的警告
- 弃用 - 抑制与弃用相关的警告
- fallthrough - 抑制与 switch 语句中缺少中断相关的警告
- finally - 抑制相对于不返回的 finally 块的警告
- 隐藏 - 抑制相对于隐藏变量的本地人的警告
- 不完整开关 - 抑制与 switch 语句中缺少条目相关的警告(枚举案例)
- nls - 禁止与非 nls 字符串文字相关的警告
- null - 抑制与 null 分析相关的警告
- 限制 - 禁止与使用不鼓励或禁止的引用相关的警告
- serial - 抑制与可序列化类的缺失 serialVersionUID 字段相关的警告
- 静态访问 - 禁止与不正确的静态访问相关的警告
- 合成访问 - 禁止与来自内部类的未优化访问相关的警告
- unchecked - 取消与未检查操作相关的警告
- unqualified-field-access - 抑制与字段访问不合格相关的警告
- 未使用 - 抑制与未使用代码相关的警告
智能
网豆
例子
指定单个警告的示例:
@SuppressWarnings("unchecked") public void methodWithScaryWarnings() { List rawList = new ArrayList(); List<String> stringList = (List<String>)rawList; }
使用两个警告的示例:
@SuppressWarnings({"unchecked","deprecation"}) public void methodWithScaryWarnings() { callDeprecatedMethod(); }
回答by inder
Note that Eclipse 3.5 doesnt understand rawtypes and flags a warning to switch to unchecked. It is frustrating that Eclipse came up with rawtypes annotation which causes more problems than solving. They should have just stuck with the standard one.
请注意,Eclipse 3.5 不理解原始类型并标记警告以切换到未选中状态。令人沮丧的是 Eclipse 提出了 rawtypes 注释,这导致了比解决更多的问题。他们应该坚持使用标准的。