如何抑制特定目录或文件(例如生成的代码)的 Java 警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1127920/
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
How to suppress Java warnings for specific directories or files such as generated code
提问by Chris Conway
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?
我正在使用一个解析器生成器,它创建了一些难看的代码。结果,我的 Eclipse 项目有几十个来自生成的源文件的警告。我知道我可以使用@SuppressWarning
注释来抑制特定元素中的特定警告,但是当解析器生成器再次运行时,我手动添加的任何注释都将丢失。有没有办法配置 Eclipse 以抑制特定文件或目录的警告?
回答by jjnguy
I think the best you can do is enable project specific settings for displaying warnings.
我认为您能做的最好的事情就是启用项目特定设置以显示警告。
Window -> Preferences -> Java -> Compiler -> Errors/Warnings
窗口 -> 首选项 -> Java -> 编译器 -> 错误/警告
On the top of the form is a link for configuring project specific settings.
表单顶部是用于配置项目特定设置的链接。
回答by Uri
I don't think Eclipse inherently provides a way to do this at the directory level (but I'm not sure).
我不认为 Eclipse 本身就提供了一种在目录级别执行此操作的方法(但我不确定)。
You could have the generated files go into a separate Java project, and control warnings for that specific project.
您可以将生成的文件放入单独的 Java 项目,并控制该特定项目的警告。
I generally prefer to place automatically-generated code in a separate project anyway.
无论如何,我通常更喜欢将自动生成的代码放在单独的项目中。
回答by Greg
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.
您只能在项目级别取消警告。但是,您可以配置问题选项卡以抑制来自文件或包的警告。进入配置内容菜单并使用“工作集:”范围。
回答by Jorn
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.
我正在对一些使用 Ant 生成 Java 解析器的 ANTLR 语法执行此操作。Ant 构建脚本将 添加@SuppressWarnings("all")
到一个 Java 文件中,并添加到另一个@Override
中的一些方法中。如果你有兴趣,我可以看看它是如何完成的。
回答by Manuel Bernhardt
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.
在 ANTLR 2 的情况下,可以通过@SuppressWarnings
在语法文件中的类声明之前附加来抑制生成代码中的警告,例如
{@SuppressWarnings("all")} class MyBaseParser extends Parser;
回答by Knubo
I solved this by using the maven regexp replace plugin - it does not solve the cause, but heals the pain:
我通过使用 maven regexp 替换插件解决了这个问题 - 它没有解决原因,但治愈了痛苦:
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>maven-replacer-plugin</artifactId>
<version>1.3.2</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>target/generated-sources/antlr/**/*.java</include>
</includes>
<regex>true</regex>
<regexFlags>
<regexFlag>MULTILINE</regexFlag>
</regexFlags>
<replacements>
<replacement>
<token>^public class</token>
<value>@SuppressWarnings("all") public class</value>
</replacement>
</replacements>
</configuration>
</plugin>
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
有关如何不生成重复 @SupressWarnings 的改进,请参阅下面的评论
回答by Henrik Heimbuerger
There is a ticket for this, Bug 220928, that has since been completed for Eclipse 3.8. Please see this answerfor details.
对此有一张票,Bug 220928,此后已为 Eclipse 3.8 完成。有关详细信息,请参阅此答案。
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.
如果您坚持使用 Eclipse 3.7 或更低版本:用户“Marc”对该票的评论在评论 35 中创建了(或至少链接到)一个名为“warningcleaner”的插件。在等待将此功能集成到 Eclipse 中时,我使用它取得了很大的成功。
It's really quite simple:
这真的很简单:
- Install plugin.
- Right-click project and select "Add/remove generated code nature".
- Open the project settings (right-click and select "properties").
- Open the tab 'Warning Cleaner'.
- Select the source folders you want to ignore the warnings from.
- 安装插件。
- 右键单击项目并选择“添加/删除生成的代码性质”。
- 打开项目设置(右键单击并选择“属性”)。
- 打开“警告清洁器”选项卡。
- 选择要忽略警告的源文件夹。
回答by djb
User @Jorn hinted at Ant code to do this. Here's what I have
用户@Jorn 暗示 Ant 代码可以做到这一点。这是我所拥有的
<echo>Adding @SuppressWarnings("all") to ANTLR generated parser/lexer *.java</echo>
<echo> in ${project.build.directory}/generated-sources/antlr/</echo>
<replace dir="${project.build.directory}/generated-sources/antlr/"
summary="true"
includes="**/*.java"
token="public class"
value='@SuppressWarnings("all") public class' />
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.
请注意,Ant 的 <replace> 进行文本替换,而不是正则表达式替换,因此它不能像 maven regexp 替换插件那样使用标记中的 ^ 元字符来匹配行的开头。
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.
我在我的 Maven pom 中从 maven-antrun-plugin 运行 Antlr 的同时这样做,因为 ANTLR maven 插件不能很好地与 Cobertura maven 插件配合使用。
(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)
(我意识到这不是原始问题的答案,但我无法在评论/回复另一个答案中格式化 Ant 代码,只能在答案中)
回答by Darren
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
这可以通过从构建路径中排除某些目录来完成(以下示例使用 Eclipse 3.5 给出)
[1] 调出 Java 构建路径
- Click on the projectin Package Explorer
- Right click, properties
- Select Java Build Path
- 单击 Package Explorer 中的项目
- 右键,属性
- 选择 Java 构建路径
[2] Add directories to exclude
[2] 添加要排除的目录
- The Source tab should contain details of the project source folders
- Expand the source folder and locate the 'Excluded:' property
- Select 'Excluded:' and click Edit
- Add folders into the Exclusion patterns using the Add/Add Multiple options
- Click Finish, then ok for Eclipse to rebuild.
- 源选项卡应包含项目源文件夹的详细信息
- 展开源文件夹并找到“排除:”属性
- 选择“排除:”并单击“编辑”
- 使用添加/添加多个选项将文件夹添加到排除模式中
- 单击完成,然后确定 Eclipse 重建。
回答by Henrik Heimbuerger
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
从版本 3.8 M6 开始,Eclipse(确切地说:JDT)为此具有内置功能。它可以通过项目的构建路径进行配置:项目属性 > Java 构建路径 > 编译器 > 源
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.
在此宣布:Eclipse 3.8 和 4.2 M6 - 新的和值得注意的,称为Selectively ignore errors/warnings from source folders。这也是截图的来源。这是在之前链接的Bug 220928上开发的新功能。