The compiler issues unchecked warnings when it detects potential type safety issues arising from mixing raw types with generic code. This includes unchecked cast warnings, unchecked method invocation warnings, unchecked generic array creation warnings and unchecked conversion warnings [Bloch 08]. It is permissible to use the @SuppressWarnings("unchecked")
annotation to suppress unchecked warnings if and only if the warning emitting code is guaranteed to be typesafe. A common usecase is mixing legacy code with new client code. The perils of relaxing unchecked warnings are discussed extensively in MSC06-J. Avoid mixing generic and non-generic code if possible.
According to the Java API [API 06] Annotation Type SuppressWarnings
documentation:
...
This noncompliant example is from the implementation of java.util.ArrayList
. When the class is compiled, it emits an unchecked cast warning, as shown. Since As the return
statement is not a declaration, the Java Language Specification [JLS 05] makes it impossible to suppress the warning trivially. Consequently, the @SuppressWarnings
has been is used over method scope. This is can cause issues when some functionality that performs type-unsafe operations is added to the method at a later date. [Bloch 08]
...