Detailed explanation of @ SuppressWarnings annotation usage in java

  • 2021-08-12 02:50:47
  • OfStack

Directory introduction:
According to the official document description of sun:
Example: The role of @ SuppressWarnings annotations
1. @ SuppressWarings annotations
Example 1-Suppressing single-type warnings:
Example 2-Suppressing multiple types of warnings:
Example 3-Suppress all types of warnings:
2. Annotate the goal
3. Suppress keywords for warnings

SuppressWarnings annotations are those provided by jse. The function is to shield 1 insignificant warnings. Enable developers to see 1 warning that they really care about. Thereby improving the efficiency of developers

Introduction:

java. lang. SuppressWarnings is one of the standard Annotation in J2SE 5.0. Can be labeled on classes, fields, methods, parameters, constructors, and local variables. Function: Tells the compiler to ignore the specified warning, and no warning message will appear after compilation is completed.

Use:

@ SuppressWarnings ("") @SuppressWarnings({}) @SuppressWarnings(value={})

According to the official document description of sun:

value-The set of warnings that will be undisplayed in the annotated element by the compiler. Duplicate names are allowed. Ignore the second and subsequent names. An unrecognized warning name is not an error: The compiler must ignore all unrecognized warning names. However, if a comment contains an unrecognized warning name, the compiler can issue a warning at will.

Compiler vendors should record the warning names they support along with the annotation type 1. Encourage vendors to cooperate with each other to ensure that the same name is used in multiple compilers.

Example:

@SuppressWarnings("unchecked")

Tells the compiler to ignore unchecked warnings, such as those that are not parameterized with List, ArrayList, etc.

@SuppressWarnings("serial")

If a warning message appears in the compiler: The serializable class WmailCalendar does not declare a static final serialVersionUID of type long, use this annotation to remove the warning message.

@SuppressWarnings("deprecation")

If you use a method annotated with @ Deprecated, the compiler will have a warning message. Use this comment to remove the warning message.

@SuppressWarnings("unchecked", "deprecation")

Tells the compiler to ignore both unchecked and deprecation warnings.

@SuppressWarnings(value={"unchecked", "deprecation"})

Equivalent to @ SuppressWarnings ("unchecked", "deprecation")

The role of @ SuppressWarnings annotations

The last annotation provided by J2SE is @ SuppressWarnings. The annotation gives the compiler an instruction telling it to be silent about certain warnings inside the annotated code element.
The @ SuppressWarnings annotation allows you to optionally cancel warnings in specific code snippets (that is, classes or methods). The idea is that when you see a warning, you will investigate it, and if you are sure it is not a problem,
You can add a @ SuppressWarnings annotation so that you won't see any more warnings. Although it sounds like it shields potential errors, it will actually improve code security because it will prevent
You are indifferent to warnings-every warning you see will be worth noting.

The problem I often encounter is that I don't know when to use what annotation of @ SupressWarnings, so I have done the following arrangement

Use:

@ SuppressWarnings ("") @SuppressWarnings({}) @SuppressWarnings(value={})

1. @ SuppressWarings notes

Function: Used to suppress the compiler from generating warning information.

Example 1-Suppressing single-type warnings:


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

Example 2-Suppressing multiple types of warnings:


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

Example 3-Suppress all types of warnings:


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

2. Annotate the goal

Through the source code of @ SuppressWarnings, we can see that its annotation targets are classes, fields, functions, function parameters, constructors and local variables of functions. It is suggested that the annotation should be stated at the position closest to the warning.

3. Suppress keywords for warnings

Keywords for suppressing warnings

all to suppress all warnings (Suppress all warnings) boxing to suppress warnings relative to boxing/unboxing operations cast to suppress warnings relative to cast operations (Suppress Mapping Related Warning) dep-ann to suppress warnings relative to deprecated annotation (Suppress Annotation Enabled Warning) deprecation to suppress warnings relative to deprecation (Suppress expired method warnings) fallthrough to suppress warnings relative to missing breaks in switch statements (Suppress the warning that breaks is missing in switch) finally to suppress warnings relative to finally block that don 't return (Suppress no warning returned by finally module) 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) (Ignore that there is no complete switch statement) nls to suppress warnings relative to non-nls string literals (Ignore characters in non-nls format) null to suppress warnings relative to null analysis (Ignore operation on null) rawtypes to suppress warnings relative to un-specific types when using generics on class params (No corresponding type specified when using generics) 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 (Ignore that the serialVersionUID variable is not declared in the-----serializable class) static-access to suppress warnings relative to incorrect static access (Suppress incorrect static access warnings) synthetic-access to suppress warnings relative to unoptimized access from inner classes unchecked to suppress warnings relative to unchecked operations (Suppress warning of no type check operation) unqualified-field-access to suppress warnings relative to field access unqualified (Suppress warnings for unauthorized domains) unused to suppress warnings relative to unused code (Warning to suppress unused code)

Related articles: