For java annotations @ SuppressWarnings, suppresswarnings
The last annotation provided by J2SE is @ SuppressWarnings. This annotation serves to give the compiler an instruction to tell it to silence some internal warnings of the annotated code element.
Background: J2SE5.0 adds several features and new warnings to the java language and promises more warnings in the future. You can add the-Xlint parameter for "javac" to control whether to report these warnings.
By default, the sun compiler outputs warnings in two simple rows. By adding the-Xlint: keyword tag (for example,-Xlint: finally), you can obtain a complete description of the keyword type.
You can cancel the warning by adding a break number-Xlint:-finally before the keyword.
Current keyword list:
All to suppress all warnings
Boxing to suppress warnings relative to boxing/unboxing operations
Cast to suppress warnings relative to cast operations
Dep-ann to suppress warnings relative to deprecated annotation
Deprecation to suppress warnings relative to deprecation
Fallthrough to suppress warnings relative to missing breaks in switch statements
Finally to suppress warnings relative to finally block that don't return
Hiding to suppress warnings relative to locals that hide variable
Incomplete-switch to suppress warnings relative to missing entries in a switch statement (enum case)
Nls to suppress warnings relative to non-nls string literals
Null to suppress warnings relative to null analysis
Rawtypes to suppress warnings relative to un-specific types when using generics on class params
Restriction to suppress warnings relative to usage of discouraged or forbidden references
Serial to suppress warnings relative to missing serialVersionUID field for a serializable class
Static-access to suppress warnings relative to incorrect static access
Synthetic-access to suppress warnings relative to unoptimized access from inner classes
Unchecked to suppress warnings relative to unchecked operations
Unqualified-field-access to suppress warnings relative to field access unqualified
Unused to suppress warnings relative to unused code
Warning when deprecation uses a class or method that is not in favor of use
Warning When unchecked executes unchecked conversions. For example, when using a set, the generic type (Generics) is not used to specify the type of the set to be saved.
Fallthrough indicates a warning when the Switch block directly leads to the following situation without a Break.
Warning when path does not exist in the class path or source file path.
Serial warning when serialVersionUID definition is missing on serializable classes.
Warning when any finally clause in finally cannot be completed normally.
All warnings about all the above situations
@ SuppressWarnings annotation allows you to selectively cancel warnings in feature code segments (classes or methods. If you see a warning, you will investigate it. If you are sure it is not a problem, you can annotate it with @ SuppressWarnings so that you will not see the warning again. Although it seems to block potential errors, it will actually improve code security. (Every warning is worth noting ).
The following is an example of using @ SuppressWarnings to cancel the deprecation warning:
@ SuppressWarnings annotation accepts a "value" variable, which is a string array that indicates the warning will be canceled. The set of valid strings varies with the compiler, but on JDK, the same keyword set can be passed to-Xlint, And the compiler is required to ignore any keywords they cannot recognize.
Because the @ SuppressWarnings annotation only receives one parameter and uses a special name "value" for this parameter, You can omit value = as a convenient Abbreviation:
public class DeprecatedUser2 {@SuppressWarnings({"deprecation"})public static void main(String[] args) {DeprecatedExample2.foo();}}
You can pass any number of string values in a single array parameter to the annotation and place the annotation at any level. For example, the following sample code indicates that the deprecation warning of the entire class will be canceled, and only the unchecked and fallthrough warnings will be canceled in the main () method code:
import java.util.*;@SuppressWarnings({"deprecation"})public class NonGenerics {@SuppressWarnings({"unchecked","fallthrough"})public static void main(String[] args) {Runtime.runFinalizersOnExit();List list = new ArrayList();list.add("foo");}public static void foo() {List list = new ArrayList();list.add("foo");}}