java编程警告_如何抑制特定目录或文件(如生成的代码)的Java警告

从版本3.8 M6开始,Eclipse(确切地说:JDT)具有内置的function。 它可以通过项目的构buildpath进行configuration: 项目属性> Java构buildpath>编译器>源

wLmKq.png

这里宣布: Eclipse 3.8和4.2 M6 – 新的和值得注意的 ,称为select性地忽略源文件夹中的错误/警告 。 这也是截图的来源。 这是在先前链接的Bug 220928上开发的新function。

有一个这样的Bug,Bug220928 ,从那以后就完成了Eclipse3.8 。 请看这个答案的细节。

如果您停留在Eclipse 3.7或更低版​​本中:用户“Marc”对该票据的评论创build(或至less链接到) 评论35中的名为“warningcleaner”的插件。 我在等待这个function被集成到Eclipse中的同时,取得了很大的成功。

这真的很简单:

安装插件。

右键单击项目,然后select“添加/删除生成的代码性质”。

打开项目设置(右键单击并select“属性”)。

打开“警告清洁”标签。

select要忽略警告的源文件夹。

x0VrW.png

我解决了这个通过使用maven正则expression式replace插件 – 它不解决的原因,但治愈的痛苦:

com.google.code.maven-replacer-pluginmaven-replacer-plugin1.3.2prepare-packagereplacetarget/generated-sources/antlr/**/*.javatrueMULTILINE^public class@SuppressWarnings("all") public class

请注意,我没有设法使**符号工作,所以您可能必须精确指定path。

请参阅下面的评论,了解如何不生成重复的@SupressWarnings

我认为你可以做的最好的是启用项目特定的设置来显示警告。

窗口 – >首选项 – > Java – >编译器 – >错误/警告

表单顶部是用于configuration项目特定设置的链接。

用户@Jorn暗示了Ant代码要做到这一点。 这是我的

Adding @SuppressWarnings("all") to ANTLR generated parser/lexer *.java in ${project.build.directory}/generated-sources/antlr/

请注意,Ant的文本replace,而不是正则expression式replace,所以它不能使用标记中的^元字符匹配Maven的正则expression式replace插件的行首。

我正在做这个,同时我在Maven pom中运行maven-antrun-plugin的Antlr,因为ANTLR maven插件没有和Cobertura maven插件配合。

(我意识到这不是对原始问题的答案,但是我不能在注释/回复到另一个答案中格式化Ant代码,只能在答案中)

我不认为Eclipse本质上提供了在目录级别执行此操作的方法(但我不确定)。

您可以将生成的文件放入单独的Java项目中,并控制该特定项目的警告。

我通常宁愿将自动生成的代码放在一个单独的项目中。

您只能在项目级别禁止警告。 但是,您可以configuration您的问题选项卡以禁止来自文件或包的警告。 进入“configuration内容”菜单并使用“在工作集:”范围。

我正在这样做几个ANTLR语法,它们使用Ant生成一个Javaparsing器。 Ant构build脚本将@SuppressWarnings("all")到一个Java文件中,并将@SuppressWarnings("all")添加到另一个Java文件中的几个方法中。 如果你有兴趣,我可以看看它是如何完成的。

对于ANTLR 2,可以在语法文件中的类声明之前通过appenidng @SuppressWarnings来抑制生成的代码中的警告,例如

{@SuppressWarnings("all")} class MyBaseParser extends Parser;

这可以通过从构buildpath中排除某些目录来完成(以下示例使用Eclipse 3.5给出)

[1]调出Java构buildpath

点击Package Explorer中的项目

右键单击属性

selectJava构buildpath

[2]添加要排除的目录

“源”选项卡应包含项目源文件夹的详细信息

展开源文件夹并find“Excluded:”属性

select“排除:”并单击编辑

使用添加/添加多个选项将文件夹添加到排除模式

点击完成,然后确定Eclipse重build。

这个小python脚本“修补”M2E生成的.classpath文件,并将所需的XML标记添加到以target/generated-sources开始的所有源文件夹中。 您可以从项目的根文件夹中运行它。 显然,当从M2E重新生成Eclipse项目信息时,需要重新运行它。 所有风险自负,显然;-)

#!/usr/bin/env python from xml.dom.minidom import parse import glob import os print('Reading .classpath files...') for root, dirs, files in os.walk('.'): for name in files: if (name == '.classpath'): classpathFile = os.path.join(root, name) print('Patching file:' + classpathFile) classpathDOM = parse(classpathFile) classPathEntries = classpathDOM.getElementsByTagName('classpathentry') for classPathEntry in classPathEntries: if classPathEntry.attributes["path"].value.startswith('target/generated-sources'): # ensure that the tag exists attributesNode = None; for attributes in classPathEntry.childNodes: if (attributes.nodeName == 'attributes'): attributesNode = attributes if (attributesNode == None): attributesNode = classpathDOM.createElement('attributes') classPathEntry.appendChild(attributesNode) # search if the 'ignore_optional_problems' entry exists hasBeenSet = 0 for node in attributesNode.childNodes: if (node.nodeName == 'attribute' and node.getAttribute('name') == 'ignore_optional_problems'): # it exists, make sure its value is true node.setAttribute('value','true') #print(node.getAttribute('name')) hasBeenSet = 1 if (not(hasBeenSet)): # it does not exist, add it x = classpathDOM.createElement("attribute") x.setAttribute('name','ignore_optional_problems') x.setAttribute('value','true') attributesNode.appendChild(x) try: f = open(classpathFile, "w") classpathDOM.writexml(f) print('Writing file:' + classpathFile) finally: f.close() print('Done.')

我已经发布了警告清除器插件已经有一段时间了,现在我正在使用Eclipse 3.8,我不再需要它了。 但是,对于那些仍然需要这个插件的人,我已经在github上发布了更新站点bintray。 如果您还在使用Eclipse 3.7或之前的版本,这可能会很有用。 检查这个网站的安装细节。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值