Introduction
While developing the software, the programmer should always give notice to warnings. Whenever a warned code practice is desired, @SuppressWarnings should be used. For example, sometimes, you do need a collection without specifying its parameterized type, e.g., object marshalling:
public void writeExternal(ObjectOutput out) throws IOException {
// construct a proper ArrayList, to be deserialized as ArrayCollection in the client side.
ArrayList list = new ArrayList(cachedObjects);
out.writeObject(list); // -> ArrayCollection.
}
The Eclipse IDE throws this warning on above code:
ArrayList is a raw type. References to generic type ArrayList<E> should be parameterized
There are two ways to remove the warning. 1) Refactor the code, change ArrayList to ArrayList<Object>; 2) use @SuppressWarnings to disable the warning:
@SuppressWarnings({”unchecked”})
public void writeExternal(ObjectOutput out) throws IOException …
Usage
You can use @SuppressWarnings to annotate TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE. The set of warnings suppressed in a given element is a superset of the warnings suppressed in all containing elements.
- suppress all: @SuppressWarnings({”all”}) // use this with care
- suppress multiple warnings: @SuppressWarnings({”unchecked”,”fallthrough”})
List of Warning Types - Sun’s JDK 6.0
Use javac -X to print the list of warning types as below:
-Xlint:{all,deprecation,unchecked,fallthrough,path,serial,finally
Explanation:
- all - all kinds of warning below
- deprecation - a deprecated element (class/method/field) is used;
- unchecked - collection is used without generic parameterized type;
- fallthrough - falls-through to the next case without a break in a switch block;
- path - non-existent path in classpath, sourcepath
- serial - missing a serialVersionUID definitions on serializable classes
- finally - finally clause that fails complete normally
List of Warning Types - Eclipse JDT v3.3
As described in JDT Plug-in Developer Guide [LINK]:
The list of tokens that can be used inside an SuppressWarning annotation is:
- all to suppress all warnings
- boxing to suppress warnings relative to boxing/unboxing operations
- cast to suppress warnings relative to cast operations
- dep-ann to suppress warnings relative to deprecated annotation
- deprecation to suppress warnings relative to deprecation
- fallthrough to suppress warnings relative to missing breaks in switch statements
- finally to suppress warnings relative to finally block that don’t return
- hiding to suppress warnings relative to locals that hide variable
- incomplete-switch to suppress warnings relative to missing entries in a switch statement (enum case)
- nls to suppress warnings relative to non-nls string literals
- null to suppress warnings relative to null analysis
- restriction to suppress warnings relative to usage of discouraged or forbidden references
- serial to suppress warnings relative to missing serialVersionUID field for a serializable class
- static-access to suppress warnings relative to incorrect static access
- synthetic-access to suppress warnings relative to unoptimized access from inner classes
- unchecked to suppress warnings relative to unchecked operations
- unqualified-field-access to suppress warnings relative to field access unqualified
- unused to suppress warnings relative to unused code
Code Generation Consideration
All code generators should take care of the above mentioned warnings. If the generated Java code could contain warnings, let the user have the option to specify @SuppressWarnings.
Antlr:
Lexers and parsers generated by Antlr easily result hundreds of warnings. To fix it, copy Java.stg into a file name JavaSuppressWarnings.stg, and modify the new file as following:
<@imports>
import org.antlr.runtime.*;
<if(TREE_PARSER)>
import org.antlr.runtime.tree.*;
<endif>
import java.util.Stack;
import java.util.List;
import java.util.ArrayList;
<if(backtracking)>
import java.util.Map;
import java.util.HashMap;
<endif>
@SuppressWarnings({”unused”, “cast”})
<@end>
the set language=JavaSuppressWarnings in the options.
Still there could be some warnings about import, use Eclipse’s source -> organize imports.