My blog has moved!

You will be automatically redirected to the new address. If that does not occur, visit
http://utku-utkan.appspot.com/
and update your bookmarks.

Pages

Wednesday, August 8, 2007

SuppressWarnings

SuppressWarnings is an annotation introduced with Java 1.5, but it is ignored until JDK 1.5.0_06. Its purpose is to suppress compiler warnings by placing the annotation declaration prior to a class, method, parameter, or variable declaration. SuppressWarnings annotation takes an array of warnings that are to be suppressed by the compiler in the annotated element. These warnings are specific to the compiler vendor. Following is the list of warnings supported by the Eclipse compiler. There are also code snippets for the most of the warnings in order to demonstrate the usage of SuppressWarnings annotation on them. You can download these code samples as a single Eclipse project by clicking here.

all
SuppressWarnings("all") is used for suppressing all warnings.

boxing
SuppressWarnings("boxing") is used for suppressing warnings relative to boxing/unboxing operations. This is an optional diagnosis and disabled by default. However, it can be enabled on the "Java > Compiler > Errors/Warnings" preference page using the "Potential programming problems > Boxing and unboxing conversions" preference.

package com.hoydaa.sw;

public class Boxing {
    @SuppressWarnings("boxing")
    public void foo() {
        // int is boxed to Integer
        Integer a = 0;
        // Integer is unboxed to int
        int b = a;
    }
}


cast
SuppressWarnings("cast") is used for suppressing warnings relative to cast operations.

dep-ann
SuppressWarnings("dep-ann") is used for suppressing warnings relative to deprecated annotation.

deprecation
SuppressWarnings("deprecation") is used for suppressing warnings relative to deprecation. This is an optional diagnosis and enabled by default. However, it can be disabled on the "Java > Compiler > Errors/Warnings" preference page using the "Deprecated and restricted API > Deprecated API" preference.

package com.hoydaa.sw;

import java.util.Date;

public class Deprecation {
    @SuppressWarnings("deprecation")
    public void foo() {
        Date date = new Date();
        date.getHours();
    }
}


fallthrough
SuppressWarnings("fallthrough") is used for suppressing warnings relative to missing breaks in switch statements. This is an optional diagnosis and disabled by default. However, it can be enabled on the "Java > Compiler > Errors/Warnings" preference page using the "Potential programming problems > 'switch' case fall-through" preference.

package com.hoydaa.sw;

public class Fallthrough {
    @SuppressWarnings("fallthrough")
    public void foo() {
        switch (0) {
            case 0:
                System.out.println("0");
            case 1:
                System.out.println("1");
        }
    }
}


finally
SuppressWarnings("finally") is used for suppressing warnings relative to finally block that don't return. This is an optional diagnosis and enabled by default. However, it can be disabled on the "Java > Compiler > Errors/Warnings" preference page using the "Potential programming problems > 'finally' does not complete normally" preference.

package com.hoydaa.sw;

public class Finally {
    @SuppressWarnings("finally")
    public void foo() {
        try {
        } finally {
            return;
        }
    }
}


hiding
SuppressWarnings("hiding") is used for suppressing warnings relative to (1) field declarations that hide other fields, and (2) local declarations that hide other fields or variables.

(1) is an optional diagnosis and disabled by default. However, it can be enabled on the "Java > Compiler > Errors/Warnings" preference page using the "Name shadowing and conflicts > Field declaration hides another field or variable" preference.

package com.hoydaa.sw;

public class Hiding1 extends Foo {
    @SuppressWarnings("hiding")
    protected Object obj;
}

class Foo {
    protected Object obj;
}


(2) is also an optional diagnosis and disabled by default. However, it can be enabled on the "Java > Compiler > Errors/Warnings" preference page using the "Name shadowing and conflicts > Local variable declaration hides another field or variable" preference.

package com.hoydaa.sw;

public class Hiding2 {
    protected Object obj;

    public void foo() {
        @SuppressWarnings("hiding")
        Object obj = new Object();
        obj.toString();
    }
}


incomplete-switch
SuppressWarnings("incomplete-switch") is used for suppressing warnings relative to missing entries in a switch statement (enum case). This is an optional diagnosis and disabled by default. However, it can be enabled on the "Java > Compiler > Errors/Warnings" preference page using the "Potential programming problems > Enum type constant not covered on 'switch'" preference.

package com.hoydaa.sw;

public class IncompleteSwitch {
    public enum Planet { MERCURY, VENUS, EARTH, MARS, JUPITER, SATURN, URANUS, NEPTUNE, PLUTO }

    @SuppressWarnings("incomplete-switch")
    public void foo() {
        switch (Planet.EARTH) {}
    }
}


nls
SuppressWarnings("nls") is used for suppressing warnings relative to non-nls string literals. This is an optional diagnosis and disabled by default. However, it can be enabled on the "Java > Compiler > Errors/Warnings" preference page using the "Code style > Non-externalized strings (missing/unused $NON-NLS$ tag)" preference.

package com.hoydaa.sw;

public class Nls {
    @SuppressWarnings("nls")
    String str = "Non-externalized string";
}


null
SuppressWarnings("null") is used for suppressing warnings relative to null analysis. This is an optional diagnosis and disabled by default. However, it can be enabled on the "Java > Compiler > Errors/Warnings" preference page using the "Potential programming problems > Null pointer access" preference.

package com.hoydaa.sw;

public class Null {
    @SuppressWarnings("null")
    public void foo(Object obj) {
        if (obj == null) {
            obj.toString();
        }
    }
}


restriction
SuppressWarnings("restriction") is used for suppressing warnings relative to usage of discouraged or forbidden references.

serial
SuppressWarnings("serial") is used for suppressing warnings relative to missing serialVersionUID field for a serializable class. This is an optional diagnosis and enabled by default. However, it can be disabled on the "Java > Compiler > Errors/Warnings" preference page using the "Potential programming problems > Serializable class without serialVersionUID" preference.

package com.hoydaa.sw;

import java.io.Serializable;

@SuppressWarnings("serial")
public class Serial implements Serializable {}


static-access
SuppressWarnings("static-access") is used for suppressing warnings relative to incorrect static access. This is an optional diagnosis and enabled by default. However, it can be disabled on the "Java > Compiler > Errors/Warnings" preference page using the "Code style > Non-static access to static member" preference.

package com.hoydaa.sw;

import java.awt.Color;

public class StaticAccess {
    @SuppressWarnings("static-access")
    public void foo() {
        Color color = new Color(0, 0, 0);
        color = color.RED;
    }
}


synthetic-access
SuppressWarnings("synthetic-access") is used for suppressing warnings relative to unoptimized access from inner classes.

unchecked
SuppressWarnings("unchecked") is used for suppressing warnings relative to (1) unchecked generic type operations, and (2) usage of raw types.

(1) is an optional diagnosis and enabled by default. However, it can be disabled on the "Java > Compiler > Errors/Warnings" preference page using the "Generic types > Unchecked generic type operations" preference.

package com.hoydaa.sw;

import java.util.Collection;

import org.apache.commons.collections.CollectionUtils;

public class Unchecked1 {
    public void foo() {
        @SuppressWarnings("unchecked")
        Collection<Object> collection = CollectionUtils.EMPTY_COLLECTION;
    }
}


(2) is an optional diagnosis and enabled by default. However, it can be disabled on the "Java > Compiler > Errors/Warnings" preference page using the "Generic types > Usage of a raw type" preference.

package com.hoydaa.sw;

import java.util.List;

public class Unchecked2 {
    public void foo() {
        @SuppressWarnings("unchecked")
        List list;
    }
}


unqualified-field-access
SuppressWarnings("unqualified-field-access") is used for suppressing warnings relative to field access unqualified. This is an optional diagnosis and disabled by default. However, it can be enabled on the "Java > Compiler > Errors/Warnings" preference page using the "Code style > Unqualified access to instance field" preference.

package com.hoydaa.sw;

public class UnqualifiedFieldAccess {
    protected Object obj;

    @SuppressWarnings("unqualified-field-access")
    public void foo() {
        obj = new Object();
    }
}


unused
SuppressWarnings("unused") is used for suppressing warnings relative to (1) local variables never read, (2) parameters never read, (3) unused imports, (4) unused private members, and (5) unused labels.

(1) is an optional diagnosis and enabled by default. However, it can be disabled on the "Java > Compiler > Errors/Warnings" preference page using the "Unnecessary code > Local variable is never read" preference.

package com.hoydaa.sw;

public class Unused1 {
    public void foo() {
        @SuppressWarnings("unused")
        Object obj;
    }
}


(2) is an optional diagnosis and disabled by default. However, it can be enabled on the "Java > Compiler > Errors/Warnings" preference page using the "Unnecessary code > Parameter is never read" preference.

package com.hoydaa.sw;

public class Unused2 {
    public void foo(@SuppressWarnings("unused") Object obj) {}
}


(3) is an optional diagnosis and enabled by default. However, it can be disabled on the "Java > Compiler > Errors/Warnings" preference page using the "Unnecessary code > Unused import" preference.

package com.hoydaa.sw;

import java.util.List;

@SuppressWarnings("unused")
public class Unused3 {}


(4) is an optional diagnosis and enabled by default. However, it can be disabled on the "Java > Compiler > Errors/Warnings" preference page using the "Unnecessary code > Unused local or private member" preference.

package com.hoydaa.sw;

public class Unused4 {
    @SuppressWarnings("unused")
    private void foo() {}
}


(5) is an optional diagnosis and enabled by default. However, it can be disabled on the "Java > Compiler > Errors/Warnings" preference page using the "Unnecessary code > Unused 'break' or 'continue' label" preference.

package com.hoydaa.sw;

public class Unused5 {
    @SuppressWarnings("unused")
    public void foo() {
        label: while (true) {}
    }
}