Search code examples
javaeclipsewekasuppress-warnings

How to avoid having to use @SuppressWarnings?


I am using an open source java library called Weka in an Eclipse Indigo project. The library seems a bit outdated so Eclipse is showing me thousands of warnings in regards to its code. A typical example is:

ArrayList is a raw type. References to generic type ArrayList<E> should be parameterized

In reference to a line like this:

ArrayList t = new ArrayList();

Eclipse's quick fix indicates that I can either convert the above code to use generics or that I can add a @SuppressWarnings 'rawtypes' annotation as a fix.

But both of these fixes involve making changes to the code of the open source project and I'd have to do these changes several (hundreds of?) times over.

Instead, might there be some global setting for Eclipse (or the Java compiler?) where I can indicate that I do not want to be warned for a specific project?


Solution

  • It has been suggested that you just turn off the "offending" warnings via Window > Preferences, but that turns them off for all projects in your workspace, including the project(s) containing your own Java 1.5 + code. That's a bad idea, 'cos you should be paying attention to the warnings if they occur in your code. (If you ignore them you are liable to get unexpected runtime exceptions.)

    Another possibility would be change the project settings to set the Java compiler's source level to Java 1.4 or earlier. You should only do this on the Weka project. The project containing your code needs to use a source level of at least Java 1.5 ... given that you are using generics.


    Thanks. Might this not cause problems later on when I deploy the project to a production server running JRE 1.6? Won't I have to have JRE 1.4 present there as well?

    No ... for a couple of reasons:

    • Unless you are modifying Weka, you can use the original JARs and ignore any build artifacts created by Eclipse.

    • The source level and the target level are different. The source level says what version of the Java language the source code must conform to. The target level says what version of the classfile format should be used. You can get the Java compiler to generate Java 1.6 bytecode format for Java 1.4 source code. (But not vice versa).

    • A Java 1.6 JRE will happily run a Java 1.4 JAR.

    Also, where do I do this? Do you mean Project > Properties > Java Compiler > Compiler compliance level?

    Yes. Note that it allows you to specify the source and target levels independently ... modulo the constraint that the Java compilers only allow certain combinations.