[Java Basics] - Commonly used annotations @SuppressWarings

Java Basics - Commonly Used Annotations @SuppressWarings

I. Overview

java.lang.SuppressWarnings is one of the standard Annotations in J2SE5.0. It can be marked on classes, fields, methods, constructors and local variables.

@SuppressWarings function : Tell the compiler to ignore the specified warning, so that the warning message does not appear after the compilation is completed.

@SuppressWarings uses:

  1. @SuppressWarnings(“”)
  2. @SuppressWarnings({})
  3. @SuppressWarnings(value={})

value - The set of warnings to be suppressed by the compiler in the annotated element. Duplicate names are allowed. Second and subsequent occurrences of the name are ignored. The occurrence of unrecognized warning names is not an error: the compiler must ignore any warning names that are not recognized. But if a comment contains an unrecognized warning name, the compiler is free to emit a warning.

Compiler vendors should document the warning names they support along with the annotation types. Vendors are encouraged to cooperate with each other to ensure that the same names are used across multiple compilers.

Common annotation demonstration :

  • @SuppressWarnings(“unchecked”)

    Tell the compiler to ignore unchecked warning information, such as warning information generated by using List, ArrayList, etc. without parameterization.

  • @SuppressWarnings(“serial”)

    Tell the compiler to ignore: The serializable class WmailCalendar does notdeclare a static final serialVersionUID field of type long information.

  • @SuppressWarnings(“deprecation”)

    Tell the compiler to ignore warning messages using the annotation @Deprecated

  • @SuppressWarnings(“unchecked”, “deprecation”)

    Tells the compiler to ignore both unchecked and deprecation warning messages.

2. Use of @SuppressWarings annotation

2.1. Suppress single type warnings

@SuppressWarnings("unchecked")
public void annotationDemo(String item){
    
    
  @SuppressWarnings("rawtypes")
   List items = new ArrayList();
   items.add(item);
}

2.2. Suppressing multiple types of warnings

@SuppressWarnings(value={
    
    "unchecked", "rawtypes"})
public void annotationDemo(String item){
    
    
   List items = new ArrayList();
   items.add(item);
}

2.3. Suppress all types of warnings

@SuppressWarnings(value={
    
    "all"})
public void annotationDemo(String item){
    
    
   List items = new ArrayList();
   items.add(item);
}

4. Annotation goals

It can be seen from the source code of @SuppressWarnings that its annotation targets are classes, fields, functions, function parameters, constructors and local variables of functions.

It is recommended that the annotation should be declared closest to where the warning occurs.

5. Keywords that suppress warnings

sequence Keywords illustrate
1 all to suppress all warnings (suppress all warnings)
2 boxing to suppress warnings relative to boxing/unboxing operations (suppress warnings when boxing and unboxing operations)
3 cast to suppress warnings relative to cast operations (suppress mapping-related warnings)
4 dep-ann to suppress warnings relative to deprecated annotation
5 deprecation to suppress warnings relative to deprecation (suppress expired method warnings)
6 fallthrough to suppress warnings relative to missing breaks in switch statements (suppress warnings about missing breaks in switch)
7 finally to suppress warnings relative to finally block that don't return (suppress warnings that the finally module does not return)
8 hiding to suppress warnings relative to locals that hide variable
9 incomplete-switch to suppress warnings relative to missing entries in a switch statement (enum case) (ignoring incomplete switch statements)
10 nls to suppress warnings relative to non-nls string literals (ignore characters in non-nls format)
11 null to suppress warnings relative to null analysis (ignore operations on null)
12 rawtypes to suppress warnings relative to un-specific types when using generics on class params (ignore the corresponding types when using generics)
13 restriction to suppress warnings relative to usage of discouraged or forbidden references
14 serial to suppress warnings relative to missing serialVersionUID field for a serializable class(ignore that the serialVersionUID variable is not declared in the serializable class
15 static-access to suppress warnings relative to incorrect static access (suppress incorrect static access warnings)
16 synthetic-access to suppress warnings relative to unoptimized access from inner classes (suppress warnings that subclasses do not access inner classes in the optimal way)
17 unchecked to suppress warnings relative to unchecked operations (suppress warnings that do not perform type-checked operations)
18 unqualified-field-access to suppress warnings relative to field access unqualified (suppress warnings for domains that do not have permission to access)
19 unused to suppress warnings relative to unused code (suppress warnings about unused code)

Guess you like

Origin blog.csdn.net/songjianlong/article/details/132487589