我搬到了一台装有最新 Sun Java 编译器的新机器,并注意到现有 Java 6 代码中出现了一些警告。 Eclipse IDE 建议我用以下内容注释分配:
@SuppressWarnings("rawtypes")
例如:
class Foo<T> {
...
}
...
@SuppressWarnings("rawtypes")
Foo foo = new Foo();
当我移回使用旧编译器 (JDK 1.6.0_20) 的机器时,我注意到这个旧编译器现在发出关于抑制“rawtypes”警告的警告,声称这种抑制不受支持,并建议将其替换为 @SuppressWarnings("unchecked")。另外,在某些地方,默认情况下,最新的编译器让我同时放置“未检查”和“原始类型” - 使用旧编译器编译该代码会产生相同的警告。
如何强制两者之间的向后/向前兼容性,以便编译器都不产生警告?
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:
@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.
How can I enforce backward/forward compatibility between the two, so that neither compiler produces warnings?
发布评论
评论(2)
您可以使用 eclipse 编译器和 javac 都支持的
@SuppressWarnings("unchecked")
。但请记住,@SuppressWarnings 注释是由编译器使用的,它可以有自己的值。 JLS 仅强制编译器理解“未选中”和“已弃用”值(目前)。
如果您使用 Helios,则需要设置一个特定选项以允许使用
@SuppressWarnings("unchecked")
而不是@SuppressWarnings("rawtypes")
,资源:
编辑:这是现在不可用的 knol用作参考的文章,最初由 Alex Miller 撰写。
You can use the
@SuppressWarnings("unchecked")
which is supported by both the eclipse compiler and 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).If you use Helios, you will need to set a specific option to allow
@SuppressWarnings("unchecked")
instead of@SuppressWarnings("rawtypes")
,Resources :
EDIT: Here is the now unavailable knol article that was used as a reference, originally written by Alex Miller.
请注意,Eclipse 3.5 不理解原始类型并标记警告以切换到未选中状态。令人沮丧的是 Eclipse 提出了 rawtypes 注释,它导致的问题多于解决的问题。他们应该坚持使用标准的。
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.