views:

776

answers:

6

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?

+2  A: 

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.

jjnguy
A: 

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.

Uri
A: 

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.

Greg
The Configure Contents menu doesn't make any sense to me.
Chris Conway
You can't find it or you can't figure out where to navigate in it? I'm using Eclipse 3.4.1 (I think it a Ganymede install with PyDev added). It's located on the upper right hand corner of the Problems tab when you click on the little arrow icon to drop down the menu for that tab.
Greg
I can find it. I don't understand what changing the settings in that dialog would accomplish.
Chris Conway
A: 

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.

Jorn
An interesting idea. Doesn't the @SuppressWarnings need to come just before the class declaration (i.e., it's not as easy as inserting it on the first line of the file)?
Chris Conway
It does need to be placed there, but it's doable. I needed to dive deep into the ant documentation to find the right function tough, but I don't have a lot of experience with Ant.
Jorn
A: 

In the case of ANTLR 2, it is possible to suppress warnings in generated code by appenidng @SuppressWarnings before the class declaration in the grammar file, e.g.

{@SuppressWarnings("all")} class MyBaseParser extends Parser;
Manuel
+1  A: 

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.

Knubo