Wednesday, April 27, 2011

SuppressWarnings annotation : Suppressing the compiler warnings

SuppressWarnings is used to suppress compiler warnings. You can apply @SuppressWarnings to types, constructors, methods, fields, parameters, and local variables.

The following are valid parameters to @SuppressWarnings:
  1. unchecked.suppress warnings from an unchecked call or an unchecked cast
  2. path. suppress warnings about nonexistent path (classpath, sourcepath, etc) directories.
  3. serial. suppress warnings about missing serialVersionUID definitions on serializable classes.
  4. finally.suppress warnings about finally clauses that cannot complete normally.
  5. fallthrough. Check switch blocks for fall-through cases.
And there are many mores...whcih are discussed later.

    Examples


      public class Main {
          @SuppressWarnings(value={"deprecation"})
          public static void main(String[] args) {
             
      DeprecatedTest test = new DeprecatedTest();
              test.serve();

          }
      }



      Example to suppress Unchecked
      i.e it will suppress the warnings from an unchecked call or an unchecked cast
      @SuppressWarnings("unchecked")
        public static void main(String[] args) {
          ArrayList data = new ArrayList();
          data.add("hello");
          data.add("world");

          Iterator it = data.iterator();
          while (it.hasNext()) {
            System.out.println(it.next());
          }
        }


      Example to suppress 2 warnings

      Suppress 2 warnings - ie.for unchecked cast or call  OR serializable class does not define a serialVersionUID
      @SuppressWarnings (value={"unchecked""serial"})
      public class SuppressWarningsTest implements Serializable {
          public void openFile () {
              ArrayList a = new ArrayList ();
              File file = new File ("X:/java/doc.txt");
          }
      }


      Valid warning types

      Though above only few warnings were shown, but sun JDK uses a larger set of strings in the compiler.  You can determine the current set by executing:
      javac -X
      which will show you (among other things) the valid settings for -Xlint. 

      For example, Sun JDK 1.5 shows:
      • all  - suppress all warnings from this code
      • deprecation  - suppress warnings from using deprecated code
      • unchecked - suppress warnings from an unchecked call or an unchecked cast
      • fallthrough - suppress warnings if a switch falls through without finding a valid case (and no default)
      • path -Warn about nonexistent path (classpath, sourcepath, etc) directories.
      • serial - suppress warnings if a Serializable class does not define a serialVersionUID
      • finally - suppress warnings from return within a finally (which will ignore return with the try)

      And Sun JDK 1.6 adds:
      • cast
      • divzero - suppress warnings if integer divide by zero is detected
      • empty
      • overrides
      • none

      IDEs and static analysis tools typically support a large number of other possible values for @SuppressWarnings.  These values correspond to specific static analysis checks performed by the IDE.


      Eclipse

      The Eclipse warning values for Eclipse 3.3 are documented in the JDT docs.
      • all - suppress all warnings
      • boxing - suppress warnings relative to boxing/unboxing operations
      • cast - suppress warnings relative to cast operations
      • dep-ann - suppress warnings relative to deprecated annotation
      • deprecation - suppress warnings relative to deprecation
      • fallthrough - suppress warnings relative to missing breaks in switch statements
      • finally - suppress warnings relative to finally block that don't return
      • hiding - suppress warnings relative to locals that hide variable
      • incomplete-switch - suppress warnings relative to missing entries in a switch statement (enum case)
      • nls - suppress warnings relative to non-nls string literals
      • null - suppress warnings relative to null analysis
      • restriction - suppress warnings relative to usage of discouraged or forbidden references
      • serial - suppress warnings relative to missing serialVersionUID field for a serializable class
      • static-access - suppress warnings relative to incorrect static access
      • synthetic-access - suppress warnings relative to unoptimized access from inner classes
      • unchecked - suppress warnings relative to unchecked operations
      • unqualified-field-access - suppress warnings relative to field access unqualified
      • unused - suppress warnings relative to unused code


      No comments:

      Post a Comment

      Chitika