How to suppress Java warnings for specific directories or files such as generated code

后端 未结 13 1550
太阳男子
太阳男子 2020-12-02 07:13

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

相关标签:
13条回答
  • 2020-12-02 07:30

    I think the best you can do is enable project specific settings for displaying warnings.

    Window -> Preferences -> Java -> Compiler -> Errors/Warnings

    On the top of the form is a link for configuring project specific settings.

    0 讨论(0)
  • 2020-12-02 07:32

    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.

    0 讨论(0)
  • 2020-12-02 07:33

    I solved this by using the maven regexp replace plugin - it does not solve the cause, but heals the pain:

    <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

    0 讨论(0)
  • 2020-12-02 07:40

    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.

    0 讨论(0)
  • 2020-12-02 07:40

    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

    • Click on the projectin Package Explorer
    • Right click, properties
    • Select Java Build Path

    [2] Add directories to exclude

    • 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.
    0 讨论(0)
  • 2020-12-02 07:40

    If the eclipse project is generated from gradle using Eclipse plugin's eclipse command the Selectively ignore errors/warnings from source folders option can be set by adding this on the top level of you build.gradle file:

    eclipse.classpath.file {
        whenMerged { classpath ->
            classpath.entries.each { entry -> 
                if (entry.path.contains('build/generated/parser')) {
                    entry.entryAttributes['ignore_optional_problems'] = true
                }
            }
        }
    }
    
    

    This assumes that generated sources are in build/generated/parser folder.

    0 讨论(0)
提交回复
热议问题