代码之家  ›  专栏  ›  技术社区  ›  Charles Duffy

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

  •  107
  • Charles Duffy  · 技术社区  · 15 年前

    我使用的是一个解析器生成器,它创建了一些难看的代码。因此,我的eclipse项目有几十个警告来自生成的源文件。我知道我可以用 @SuppressWarning 注释以在特定元素中抑制特定警告,但当解析器生成器再次运行时,我手动添加的任何注释都将丢失。有没有办法配置eclipse来禁止特定文件或目录的警告?

    12 回复  |  直到 10 年前
        1
  •  88
  •   Henrik Heimbuerger Kenneth Reitz    12 年前

    从3.8m6版开始,eclipse(确切地说:jdt)就有了这方面的内置功能。它可以通过项目的生成路径进行配置: 项目属性& gt;java构建路径&编译器;

    enter image description here

    在此宣布: Eclipse 3.8 and 4.2 M6 - New and Noteworthy ,叫做 选择性忽略源文件夹中的错误/警告 . 这也是截图的来源。这是在先前链接的 Bug 220928

        2
  •  20
  •   Community CDub    7 年前

    这有张票, Bug 220928 ,这已经在Eclipse3.8中完成。请看 this answer 详情。

    如果你被Eclipse3.7或更低版本困住了:用户“marc”评论这个票证时创建了一个名为“warningcleaner”的插件(或者至少链接到该插件)。 comment 35 . 在等待这个特性集成到eclipse中时,我使用它取得了很大的成功。

    其实很简单:

    1. 安装插件。
    2. 右键单击项目并选择“添加/删除生成的代码性质”。
    3. 打开项目设置(右键单击并选择“属性”)。
    4. 打开“警告清除器”选项卡。
    5. 选择要忽略警告的源文件夹。

    Warning Cleaner screenshot

        3
  •  17
  •   Betlista    12 年前

    我通过使用maven regexp replace插件解决了这个问题-它并不能解决问题的原因,但可以治愈痛苦:

    <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>
    

    请注意,我没有设法使**符号起作用,所以您可能必须准确地指定路径。

    有关如何不生成重复@superswarnings的改进,请参见下面的注释

        4
  •  7
  •   jjnguy Julien Chastang    15 年前

    我认为最好的办法是启用项目特定的设置来显示警告。

    窗口& gt;偏好-gt;java>编译器& gt;错误/警告

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

        5
  •  4
  •   djb    13 年前

    用户@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' />
    

    注意,ant的<replace>执行文本替换,而不是正则表达式替换, 因此它不能像maven regexp replace插件那样使用标记中的^元字符来匹配行首。

    我这样做的同时,我从我的maven pom中的maven antrun插件运行antlr,因为antlr maven插件不能很好地与cobertura maven插件一起使用。

    (我意识到这不是对原问题的回答,但我不能在评论/回答中设置蚂蚁代码,只在答案中)。

        6
  •  1
  •   Uri    15 年前

    我不认为eclipse本质上提供了在目录级别实现这一点的方法(但我不确定)。

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

    我通常更喜欢将自动生成的代码放在单独的项目中。

        7
  •  1
  •   Greg    15 年前

    只能在项目级别上禁止警告。但是,可以配置“问题”选项卡,以阻止来自文件或包的警告。进入configure contents菜单,使用“on working set:”作用域。

        8
  •  1
  •   Jorn    15 年前

    我这样做是为了几个ANTLR语法,它生成一个使用蚂蚁的Java解析器。Ant构建脚本添加 @SuppressWarnings("all") 一个Java文件,以及 @Override 在另一种方法中的一些方法。 如果你感兴趣的话,我可以查一下它是怎么做的。

        9
  •  1
  •   opajonk    12 年前

    这个小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 <attributes> 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.')
    
        10
  •  0
  •   Manuel Bernhardt    14 年前

    在antlr 2的情况下,可以通过appending来抑制生成代码中的警告。 @SuppressWarnings 在语法文件中的类声明之前,例如。

    {@SuppressWarnings("all")} class MyBaseParser extends Parser;
    
        11
  •  0
  •   Darren    13 年前

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

    (1)提出Java构建路径

    • 单击projectin包资源管理器
    • 右键单击,属性
    • 选择Java构建路径

    [2]添加要排除的目录

    • “源”选项卡应包含项目源文件夹的详细信息
    • 展开源文件夹并找到“excluded:”属性
    • 选择“排除”,然后单击“编辑”
    • 使用“添加/添加多个选项”将文件夹添加到排除模式中
    • 单击finish,然后单击ok以使eclipse重新生成。
        12
  •  0
  •   Marc    10 年前

    我已经有一段时间没有发布警告清理器插件了,现在我正在使用Eclipse3.8,我不再需要它了。 但是,对于那些仍然需要这个插件的人,我已经在github上发布了它,更新站点在bintray上。 如果您仍在使用Eclipse3.7或之前的版本,这可能会很有用。 检查 this site 有关安装的详细信息。