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 2008]. 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 ignoring unchecked warnings are discussed extensively in guideline MSC00-J. Do not mix generic with non-generic raw types in new code.
According to the Java API [API 2006] Annotation Type SuppressWarnings
documentation:
As a matter of style, programmers should always use this annotation on the most deeply nested element where it is effective. If you want to suppress a warning in a particular method, you should annotate that method rather than its class.
...
Failure to reduce the scope of the @SuppressWarnings
annotation can lead to runtime exceptions and break type safety guarantees.
Rule Guideline | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
SCP04- J | medium | probable | high | P4 | L3 |
...