blob: cd3d2bbc759fca26ea34d67b9768e8288fab00cf [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta name="copyright" content=
"Copyright (c) IBM Corporation and others 2000, 2017. This page is made available under license. For full details see the LEGAL in the documentation book that contains this page." />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<link rel="STYLESHEET" href="../book.css" charset="ISO-8859-1" type="text/css" />
<title>Excluding warnings using @SuppressWarnings</title>
<link rel="stylesheet" type="text/css" href="../book.css" />
</head>
<body>
<a name="t_suppress_warnings"></a>
<h1>Excluding warnings using @SuppressWarnings</h1>
<p>Since Java 5.0, you can disable compilation warnings relative to a subset of a compilation unit using the <code>java.lang.SuppressWarning</code> annotation.</p>
<pre>
@SuppressWarning("unused") public void foo() {
String s;
}
</pre>
<p>Without the annotation, the compiler would complain that the local variable <code>s</code> is never used. With the annotation, the compiler silently ignores this warning locally to the
<code>foo</code> method. This enables to keep the warnings in other locations of the same compilation unit or the same project.</p>
<p>The list of tokens that can be used inside a <code>SuppressWarnings</code> annotation is:</p>
<table border="1" cellspacing="0" cellpadding="5" width="100%" summary="SuppressWarnings tokens">
<tr>
<th>
<p>Token</p>
</th>
<th>
<p>Description</p>
</th>
<th>
<p>Suppressed compiler option(s)</p>
</th>
</tr>
<tr>
<td valign="top">
<p><span class="token">all</span></p>
</td>
<td valign="top">
<p>to suppress all warnings</p>
</td>
<td valign="top">
<p>All compiler options</p>
</td>
</tr>
<tr>
<td valign="top">
<p><span class="token">boxing</span></p>
</td>
<td valign="top">
<p>to suppress warnings relative to boxing/unboxing operations</p>
</td>
<td valign="top">
<p>Boxing and unboxing conversions</p>
</td>
</tr>
<tr>
<td valign="top">
<p><span class="token">cast</span></p>
</td>
<td valign="top">
<p>to suppress warnings relative to cast operations</p>
</td>
<td valign="top">
<p>Unnecessary cast or 'instanceof' operation</p>
</td>
</tr>
<tr>
<td valign="top">
<p><span class="token">dep-ann</span></p>
</td>
<td valign="top">
<p>to suppress warnings relative to deprecated annotation</p>
</td>
<td valign="top">
<p>Missing '@Deprecated' annotation</p>
</td>
</tr>
<tr>
<td valign="top">
<p><span class="token">deprecation</span></p>
</td>
<td valign="top">
<p>to suppress warnings relative to deprecation</p>
</td>
<td valign="top">
<p>Deprecated API</p>
</td>
</tr>
<tr>
<td valign="top">
<p><span class="token">fallthrough</span></p>
</td>
<td valign="top">
<p>to suppress warnings relative to missing breaks in switch statements</p>
</td>
<td valign="top">
<p>'switch' case fall-through</p>
</td>
</tr>
<tr>
<td valign="top">
<p><span class="token">finally</span></p>
</td>
<td valign="top">
<p>to suppress warnings relative to finally block that don't return</p>
</td>
<td valign="top">
<p>'finally' does not complete normally</p>
</td>
</tr>
<tr>
<td valign="top">
<p><span class="token">hiding</span></p>
</td>
<td valign="top">
<p>to suppress warnings relative to locals that hide variable</p>
</td>
<td valign="top">
<p>Local variable declaration hides another field or variable</p>
</td>
</tr>
<tr>
<td valign="top">
<p><span class="token">incomplete-switch</span></p>
</td>
<td valign="top">
<p>to suppress warnings relative to missing entries in a switch statement (enum case)</p>
</td>
<td valign="top">
<p>Incomplete 'switch' cases on enum</p>
</td>
</tr>
<tr>
<td valign="top">
<p><span class="token">javadoc</span></p>
</td>
<td valign="top">
<p>to suppress warnings relative to javadoc warnings</p>
</td>
<td valign="top">
<p>All options in <a href="../reference/preferences/java/compiler/ref-preferences-javadoc.htm">Java Compiler Javadoc Preferences</a></p>
</td>
</tr>
<tr>
<td valign="top">
<p><span class="token">nls</span></p>
</td>
<td valign="top">
<p>to suppress warnings relative to non-nls string literals</p>
</td>
<td valign="top">
<p>Non-externalized strings</p>
</td>
</tr>
<tr>
<td valign="top">
<p><span class="token">null</span></p>
</td>
<td valign="top">
<p>to suppress warnings relative to null analysis</p>
</td>
<td valign="top">
<p>All options in <a href="../reference/preferences/java/compiler/ref-preferences-errors-warnings.htm#null_analysis">Java Compile Errors/Warnings Preferences for Null Analysis</a></p>
</td>
</tr>
<tr>
<td valign="top">
<p><span class="token">rawtypes</span></p>
</td>
<td valign="top">
<p>to suppress warnings relative to usage of raw types</p>
</td>
<td valign="top">
<p>Usage of a raw type</p>
</td>
</tr>
<tr>
<td valign="top">
<p><span class="token">resource</span></p>
</td>
<td valign="top">
<p>to suppress warnings relative to usage of resources of type Closeable</p>
</td>
<td valign="top">
<p>Resource leak<br/>
Potential resource leak<br/>
Resource not managed via try-with-resource</p>
</td>
</tr>
<tr>
<td valign="top">
<p><span class="token">restriction</span></p>
</td>
<td valign="top">
<p>to suppress warnings relative to usage of discouraged or forbidden references</p>
</td>
<td valign="top">
<p>Forbidden reference (access rules)<br/>
Discouraged reference (access rules)</p>
</td>
</tr>
<tr>
<td valign="top">
<p><span class="token">removal</span></p>
</td>
<td valign="top">
<p>to suppress warnings relative to usage of terminally deprecated API</p>
</td>
<td valign="top">
<p>Forbidden reference (access rules)<br/>
Discouraged reference (access rules)</p>
</td>
</tr>
<tr>
<td valign="top">
<p><span class="token">serial</span></p>
</td>
<td valign="top">
<p>to suppress warnings relative to missing serialVersionUID field for a serializable class</p>
</td>
<td valign="top">
<p>Serializable class without serialVersionUID</p>
</td>
</tr>
<tr>
<td valign="top">
<p><span class="token">static-access</span></p>
</td>
<td valign="top">
<p>to suppress warnings relative to incorrect static access</p>
</td>
<td valign="top">
<p>Non-static access to a static member<br/>
Indirect access to a static member</p>
</td>
</tr>
<tr>
<td valign="top">
<p><span class="token">static-method</span></p>
</td>
<td valign="top">
<p>to suppress warnings relative to methods that could be declared as static</p>
</td>
<td valign="top">
<p>Method can be static<br/>
Method can potentially be static</p>
</td>
</tr>
<tr>
<td valign="top">
<p><span class="token">super</span></p>
</td>
<td valign="top">
<p>to suppress warnings relative to overriding a method without super invocations</p>
</td>
<td valign="top">
<p></p>
</td>
</tr>
<tr>
<td valign="top">
<p><span class="token">synthetic-access</span></p>
</td>
<td valign="top">
<p>to suppress warnings relative to unoptimized access from inner classes</p>
</td>
<td valign="top">
<p></p>
</td>
</tr>
<tr>
<td valign="top">
<p><span class="token">sync-override</span></p>
</td>
<td valign="top">
<p>to suppress warnings because of missing synchronize when overriding a synchronized method</p>
</td>
<td valign="top">
<p>Missing synchronized modifier on inherited method</p>
</td>
</tr>
<tr>
<td valign="top">
<p><span class="token">unchecked</span></p>
</td>
<td valign="top">
<p>to suppress warnings relative to unchecked operations</p>
</td>
<td valign="top">
<p>Unchecked generic type operation</p>
</td>
</tr>
<tr>
<td valign="top">
<p><span class="token">unqualified-field-access</span></p>
</td>
<td valign="top">
<p>to suppress warnings relative to field access unqualified</p>
</td>
<td valign="top">
<p>Unqualified access to instance field</p>
</td>
</tr>
<tr>
<td valign="top">
<p><span class="token">unlikely-arg-type</span></p>
</td>
<td valign="top">
<p>to suppress warnings relative to unlikely argument types</p>
</td>
<td valign="top">
<p>Unqualified access to instance field</p>
</td>
</tr>
<tr>
<td valign="top">
<p><span class="token">unused</span></p>
</td>
<td valign="top">
<p>to suppress warnings relative to unused code and dead code</p>
</td>
<td valign="top">
<p>Dead code<br/>
Unused object allocation<br/>
Value of local variable is not used<br/>
Value of method parameter is not used<br/>
Value of exception parameter is not used<br/>
Unused type parameter<br/>
Unused import<br/>
Unused private member<br/>
Unused 'break' or 'continue' label
</p>
</td>
</tr>
</table>
<p>
<img border="0" src="../images/ngrelr.png" alt="Related reference"/><br/>
<a href="../reference/preferences/java/compiler/ref-preferences-errors-warnings.htm#annotations">Java Compile Errors/Warnings Preferences</a>
</p>
</body>
</html>