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

Viewed 37404

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?

13 Answers

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

enter image description here

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.

There is a ticket for this, Bug 220928, that has since been completed for Eclipse 3.8. Please see this answer for details.

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.

It's really quite simple:

  1. Install plugin.
  2. Right-click project and select "Add/remove generated code nature".
  3. Open the project settings (right-click and select "properties").
  4. Open the tab 'Warning Cleaner'.
  5. Select the source folders you want to ignore the warnings from.

Warning Cleaner screenshot

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.

I don't think Eclipse inherently provides a way to do this at the directory level (but I'm not sure).

You could have the generated files go into a separate Java project, and control warnings for that specific project.

I generally prefer to place automatically-generated code in a separate project anyway.

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.

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.

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.

Related