Java 如何在特定于文件的基础上禁用 Eclipse 中的编译器警告?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3624756/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 02:49:31  来源:igfitidea点击:

How can I disable compiler warnings in Eclipse on a file specific basis?

javaeclipsecompiler-constructioncompiler-warnings

提问by kutuzof

In my Eclipse project are a handful of generated .java files that I need to use for SQLJ and I can't move to a separate project (due to Administrative Overhead). These files are also regularly regenerated so editing them is unfortunately out.

在我的 Eclipse 项目中有一些生成的 .java 文件,我需要将这些文件用于 SQLJ 并且我无法移动到单独的项目(由于管理开销)。这些文件也会定期重新生成,因此很遗憾无法对其进行编辑。

Unfortunately these files generate a few hundred java compiler warnings which drown out the useful warnings I get on files that I actually can edit.

不幸的是,这些文件生成了几百个 Java 编译器警告,这些警告淹没了我在实际可以编辑的文件上收到的有用警告。

Is there any way in Eclipse to say Ignore all the warnings of a file-by-file basis? Or can I block out a specific sub-directory?

在 Eclipse 中有什么方法可以说忽略逐个文件的所有警告吗?或者我可以屏蔽一个特定的子目录吗?

采纳答案by meriton

The "problems" view in eclipse can be filtered; I always have it set to "on selected element and its children only". Granted, this is more of a work-around, but it lessens the impact of having the files in the same project. (Note that even if you must have them in the same project, you can keep them in a separate source folder).

可以过滤eclipse中的“问题”视图;我总是将它设置为“仅在选定元素及其子元素上”。当然,这更像是一种解决方法,但它减少了将文件放在同一项目中的影响。(请注意,即使您必须将它们放在同一个项目中,您也可以将它们保存在单独的源文件夹中)。

Edit: To configure the filters, find the icon with a downward arrow with tooltip "View Menu" on the top right of the problems view. Click it, and then click "configure contents".

编辑:要配置过滤器,请在问题视图的右上角找到带有工具提示“查看菜单”的向下箭头图标。单击它,然后单击“配置内容”。

回答by Stephen C

One way to do this is to add @SuppressWarning(...)annotations to the source code.

一种方法是@SuppressWarning(...)在源代码中添加注释。

Another way would be to move the troublesome code to a separate Eclipse project and use per-project compiler settings.

另一种方法是将麻烦的代码移至单独的 Eclipse 项目并使用每个项目的编译器设置。

EDIT

编辑

Surely, you can partition your code into multiple projects with the appropriate inter-project dependencies?

当然,您可以将代码划分为具有适当的项目间依赖关系的多个项目吗?

If not, I'd say you are out of realistic options. (But if you want some unrealistic ones, you could post-process the generated code to add the annotations, hack the Eclipse Java compiler to implement per-file suppression, hack the Eclipse "Problems" view to implement per-file/directory filtering of errors, etc, etc.)

如果没有,我会说你没有现实的选择。(但如果你想要一些不切实际的,你可以对生成的代码进行后处理以添加注释,破解 Eclipse Java 编译器以实现每个文件的抑制,破解 Eclipse“问题”视图以实现每个文件/目录的过滤错误等)

回答by bdhar

@SuppressWarningannotation?

@SuppressWarning注释

Per, Stephen's comment, you can find the "Per project compiler settings option here"

根据斯蒂芬的评论,您可以在此处找到“每个项目编译器设置选项”

Project->Properties->Java Compiler->Errors/Warnings

项目->属性->Java编译器->错误/警告

Enable project specific settings

启用项目特定设置

alt text

替代文字

回答by dolmen

If you want to hide warnings on a class or on a method, see the Eclipse documentation on the @SuppressWarningsannotationor Java documentation

如果要隐藏类或方法上的警告,请参阅有关@SuppressWarnings注释Java 文档的 Eclipse文档

Example:

例子:

@SuppressWarnings("unused")
public void foo() {
    String s;
}

回答by Edward Brey

Ensure the files are under the genfolder, the typical home for all generated .java files. Then in the project properties, under Java Build Path, set Ignore optional compile problemsfor that folder to Yes.

确保文件位于gen文件夹下,即所有生成的 .java 文件的典型主目录。然后在项目属性中的Java Build Path 下,将该文件夹的Ignore optional compile questions设置为Yes

If your project structure requires your files be in a folder other than gen, add that folder to the Java Build Path so that you can enable ignoring optional compile problems for it.

如果您的项目结构要求您的文件位于 以外的文件夹中gen,请将该文件夹添加到 Java 构建路径,以便您可以忽略可选的编译问题。