Java Reference
In-Depth Information
Detecting multiple for statements that use the same index variable is straightforward;
it produces false positives only in the unusual case where the value of the index variable
is intended to persist between loops.
Bibliography
[Bloch 2001]
Item 29, “Minimize the Scope of Local Variables”
[JLS 2013]
§14.4, “Local Variable Declaration Statements”
23. Minimize the scope of the @SuppressWarnings annotation
When the compiler detects potential type-safety issues arising from mixing raw types
with generic code, it issues unchecked warnings , including unchecked cast warnings,
unchecked method invocation warnings, unchecked generic array creation warnings ,
and unchecked conversion warnings [Bloch 2008]. It is permissible to use the @Sup-
pressWarnings("unchecked") annotation to suppress unchecked warnings when, and
onlywhen,thewarning-emittingcodeisguaranteedtobetypesafe.Acommonusecaseis
mixing legacy code with new client code. The perils of ignoring unchecked warnings are
discussed extensively in The CERT ® Oracle ® Secure Coding Standard for Java [Long
2012], “OBJ03-J. Do not mix generic with non-generic raw types in new code.”
According to the Java API Annotation Type SuppressWarnings documentation [API
2013],
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.
The @SuppressWarnings annotation can be used in the declaration of variables and
methods as well as an entire class. It is, however, important to narrow its scope so that
only those warnings that occur in the narrower scope are suppressed.
Noncompliant Code Example
In this noncompliant code example, the @SuppressWarnings annotation's scope encom-
passes the whole class:
Click here to view code image
@SuppressWarnings("unchecked")
class Legacy {
Search WWH ::




Custom Search