为什么我不能抑制包上的警告? [英] Why Can't I SuppressWarnings on a Package?

查看:18
本文介绍了为什么我不能抑制包上的警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 javadoc 中看到 @SuppressWarnings 注释适用

TYPE,FIELD,METHOD,PARAMETER,CONSTRUCTOR,LOCAL_VARIABLE

目标.为什么它不适用于 PACKAGE?

我有一些生成的代码,其中包含一些原始类型警告.我希望能够为生成的类添加一个 package-info.java 文件(在一个单独的物理目录中,但在同一个 java 包中),它告诉 eclipse 忽略来自包中生成的类.

为什么不支持?是否有其他方法可以在整个包中抑制警告?

解决方案

在对旧错误报告(状态 - 已关闭,不会修复)的响应中解释了不允许在包级别抑制警告的原因:允许在包级别指定 SuppressWarnings.

<块引用>

警告实际上表明了潜在的问题生成的代码.

目前,SuppressWarnings 具有理想的属性只影响词法嵌套代码.这意味着你可以立即查看警告是否可能被抑制您正在阅读的代码.

这个提议会违反这个性质来解决一个不常见的问题在大多数情况下可以解决的问题.

该回复中还建议了一些变通方法.

<块引用>

  1. 使用 -source 1.4 和 -target 5 自行编译生成的代码.

  2. 请求使用抑制警告的 javacc 的更新版本或不生成导致警告的代码.

我认为第一个建议,将生成的代码放在自己的项目中,应该对你有用.第二个建议看起来更具体到错误报告中的问题.不知道你有没有用javacc.

I see from the javadoc that the @SuppressWarnings annotation applies to

TYPE,FIELD,METHOD,PARAMETER,CONSTRUCTOR,LOCAL_VARIABLE

targets. Why does it not also apply to PACKAGE?

I have some generated code which contains some raw types warnings. I'd like to be able to add a package-info.java file for the generated classes (in a separate physical directory but the same java package) which tells eclipse to ignore any raw types warnings emanating from the generated classes in package.

Why is this not supported? Is there an alternate way of suppressing a warning in an entire package?

解决方案

The reason that suppressing warnings at the package level is not allowed was explained in the response to an old bug report (Status - Closed, Will Not Fix): Allow SuppressWarnings to be specified at the package level.

The warnings actually indicates potential problems in the generated code.

Currently, SuppressWarnings have the desirable property of only affecting lexically nested code. This means that you can immediately see if a warning might be suppressed in code you're reading.

This proposal would violate this property to solve an uncommon problem which in most cases can be worked around.

There are a couple of work arounds suggested in that response as well.

  1. compile the generated code by itself using -source 1.4 and -target 5.

  2. request an updated version of javacc which either uses suppresswarnings or doesn't generate code which causes warnings.

I think the first suggestion, putting the generated code in its own project, should work for you. The second suggestion looks like its more specific to the problem in the bug report. I don't know if you're using javacc or not.

这篇关于为什么我不能抑制包上的警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆