如何抑制特定目录或文件(例如生成的代码)的 Java 警告
我正在使用一个解析器生成器,它创建了一些难看的代码。 因此,我的 Eclipse 项目有几十个来自生成的源文件的警告。 我知道我可以使用 @SuppressWarning 注释来抑制特定元素中的特定警告,但是当解析器生成器再次运行时,我手动添加的任何注释都将丢失。 有没有办法配置 Eclipse 以抑制特定文件或目录的警告?
I'm using a parser generator that creates somewhat ugly code. As a result my Eclipse project has several dozen warnings emanating from generated source files. I know I can use the @SuppressWarning
annotation to suppress particular warnings in particular elements, but any annotations I add by hand will be lost when the parser generator runs again. Is there a way to configure Eclipse to suppress warnings for a particular file or directory?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
从版本 3.8 M6 开始,Eclipse(确切地说:JDT)为此提供了内置功能。 它可以通过项目的构建路径进行配置:项目属性> Java 构建路径 > 编译器> 来源
此处公布:Eclipse 3.8 和 4.2 M6 - 新增功能和值得注意的,称为有选择地忽略源文件夹中的错误/警告。 这也是屏幕截图的来源。 这是根据之前链接的 Bug 220928 开发的新功能。
Starting with version 3.8 M6, Eclipse (to be exact: the JDT) has built-in functionality for this. It is configurable through a project's build path: Project properties > Java Build Path > Compiler > Source
Announced here: Eclipse 3.8 and 4.2 M6 - New and Noteworthy, called Selectively ignore errors/warnings from source folders. That's also where the screenshot is from. This is the new feature developed on the previously linked Bug 220928.
有一个针对此问题的票证,Bug 220928,此后已已针对 Eclipse 3.8 完成。 有关详细信息,请参阅此答案。
如果您无法使用 Eclipse 3.7 或更低版本:对该票证进行评论的用户“Marc”在 评论 35。 在等待此功能集成到 Eclipse 中时,我使用它并取得了很大的成功。
这真的很简单:
There is a ticket for this, Bug 220928, that has since been completed for Eclipse 3.8. Please see this answer for details.
If you're stuck with Eclipse 3.7 or lower: The user "Marc" commenting on that ticket created (or at least links to) a plugin called 'warningcleaner' in comment 35. I'm using that with a lot of success while waiting for this feature to be integrated into Eclipse.
It's really quite simple:
我通过使用 maven regexp 替换插件解决了这个问题 - 它不能解决原因,但可以治愈痛苦:
请注意,我没有设法让 ** 符号起作用,因此您可能必须准确指定路径。
请参阅下面的评论,了解如何不生成重复的 @SupressWarnings 的改进
I solved this by using the maven regexp replace plugin - it does not solve the cause, but heals the pain:
Note that I did not manage to get the ** notation to work, so you might have to specify path exactly.
See comment below for an improvement on how not to generate duplicate @SupressWarnings
我认为您能做的最好的事情就是启用项目特定设置来显示警告。
表单顶部是用于配置项目特定设置的链接。
I think the best you can do is enable project specific settings for displaying warnings.
On the top of the form is a link for configuring project specific settings.
用户 @Jorn 暗示 Ant 代码可以做到这一点。 这是我所得到的
请注意,Ant 的是 进行文本替换,而不是正则表达式替换,
因此它不能像 Maven 正则表达式替换插件那样使用令牌中的 ^ 元字符来匹配行首。
我在执行此操作的同时,在 Maven pom 中从 maven-antrun-plugin 运行 Antlr,因为 ANTLR maven 插件与 Cobertura maven 插件不能很好地配合。
(我意识到这不是原始问题的答案,但我无法在评论/回复另一个答案中格式化 Ant 代码,只能在答案中格式化)
User @Jorn hinted at Ant code to do this. Here's what I have
Note that Ant's <replace> does text replacement, not regular expression replacement,
so it cannot use the ^ meta-character in the token to match beginning of line as the maven regexp replace plugin does.
I'm doing this at the same time that I run Antlr from maven-antrun-plugin in my Maven pom, because the ANTLR maven plugin did not play well with the Cobertura maven plugin.
(I realize this is not an answer to the original question, but I can't format Ant code in a comment/reply to another answer, only in an answer)
我不认为 Eclipse 本质上提供了在目录级别执行此操作的方法(但我不确定)。
您可以将生成的文件放入单独的 Java 项目中,并控制该特定项目的警告。
无论如何,我通常更喜欢将自动生成的代码放在单独的项目中。
I don't think Eclipse inherently provides a way to do this at the directory level (but I'm not sure).
You could have the generated files go into a separate Java project, and control warnings for that specific project.
I generally prefer to place automatically-generated code in a separate project anyway.
您只能在项目级别抑制警告。 但是,您可以配置问题选项卡以抑制来自文件或包的警告。 进入“配置内容”菜单并使用“在工作集:”范围。
You can only suppress warnings at the project level. However, you can configure your problems tab to suppress warnings from files or packages. Go into the Configure Contents menu and work with the "On working set:" scope.
这个小 python 脚本“修补”M2E 生成的
.classpath
文件,并将所需的 XML 标记添加到以target/ generated-sources
开头的所有源文件夹中。 您可以从项目的根文件夹运行它。 显然,当从M2E重新生成Eclipse项目信息时,您需要重新运行它。 显然,一切风险由您自己承担;-)This small python script "patches" the M2E-generated
.classpath
files and adds the required XML tag to all source folders starting withtarget/generated-sources
. You can just run it from you project's root folder. Obviously you need to re-run it when the Eclipse project information is re-generated from M2E. And all at your own risk, obviously ;-)我正在对一些 ANTLR 语法执行此操作,这些语法使用 Ant 生成 Java 解析器。 Ant 构建脚本将
@SuppressWarnings("all")
添加到一个 Java 文件,并将@Override
添加到另一个 Java 文件中的一些方法。如果您有兴趣,我可以查一下具体是如何完成的。
I'm doing this to a few ANTLR grammars, which generate a Java parser using Ant. The Ant build script adds the
@SuppressWarnings("all")
to one Java file, and@Override
to a few methods in another.I can look up how it's done exactly, if you're interested.
对于 ANTLR 2,可以通过在语法文件中的类声明之前附加
@SuppressWarnings
来抑制生成代码中的警告,例如In the case of ANTLR 2, it is possible to suppress warnings in generated code by appenidng
@SuppressWarnings
before the class declaration in the grammar file, e.g.这可以通过从构建路径中排除某些目录来完成(以下示例使用 Eclipse 3.5 给出)
[1] 打开 Java 构建路径
[2] 添加要排除的
This can be done by excluding certain directories from the build path (The following example is given using Eclipse 3.5)
[1] Bring up the Java Build Path
[2] Add directories to exclude
我发布 warning-cleaner 插件已经有一段时间了,现在我使用的是 Eclipse 3.8,我不再需要它了。
不过,对于那些仍然需要这个插件的人,我已经在 github 上发布了它,并在 bintray 上更新了站点。
如果您仍在使用 Eclipse 3.7 或更早版本,这可能很有用。
检查此站点了解安装详细信息。
It's been a while since I have released the warning-cleaner plugin, and now that I am using Eclipse 3.8, I have no need for it anymore.
However, for those who still need this plugin, I have released it on github with the update site on bintray.
If you are still using Eclipse 3.7 or before, this could be useful.
Check this site for installation details.
如果 eclipse 项目是使用 Eclipse 插件的
eclipse< 从 gradle 生成的/code> 命令可以通过将其添加到
build.gradle
文件的顶层来设置选择性地忽略源文件夹中的错误/警告
选项:这假设生成的源位于
build/ generated/parser
文件夹中。If the eclipse project is generated from gradle using Eclipse plugin's
eclipse
command theSelectively ignore errors/warnings from source folders
option can be set by adding this on the top level of youbuild.gradle
file:This assumes that generated sources are in
build/generated/parser
folder.