SuppressWarnings annotations are annotations provided by jSE. The effect is to block out some unimportant warnings. Enable developers to see some warnings that they really care about. To improve the efficiency of developers

Brief introduction:

Java. Lang. SuppressWarnings is one of the standard of the Annotation to the J2SE 5.0. You can annotate classes, fields, methods, parameters, constructors, and local variables. Effect: Tells the compiler to ignore the specified warning.

Use:

@ SuppressWarnings (” “)

@SuppressWarnings({})

@SuppressWarnings(value={})

According to sun’s official documentation:

Value – The set of warnings that will be undisplayed in the element of the comment by the compiler. Duplicate names are allowed. Ignore the second and subsequent names. The presence of unrecognized warning names is not an error: the compiler must ignore all unrecognized warning names. But 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 types. Vendors are encouraged to cooperate with each other to ensure that the same name is used across multiple compilers.

Example:

  • @SuppressWarnings(“unchecked”)

Tells the compiler to ignore unchecked warnings such as using List, ArrayList, and others that are not parameterized.

  • @SuppressWarnings(“serial”)

If the compiler displays this warning: The Serializable class WmailCalendar does not declare a static final serialVersionUID field of type long The serializable class WmailCalendar does not declare a static final serialVersionUID field of type long.

  • @SuppressWarnings(“deprecation”)

If you use a method using the @deprecated annotation, the compiler will display a warning message. Use this comment to remove the warning message.

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

Tell the compiler to ignore both the warning message for unchecked and deprecation.

  • @SuppressWarnings(value={“unchecked”, “deprecation”})

Equivalent to @SuppressWarnings(“unchecked”, “deprecation”)

@SuppressWarnings What the annotation does

The last annotation provided by J2SE is @SuppressWarnings. The effect of this annotation is to give the compiler an instruction to be silent about certain warnings within the annotated code element. The @SuppressWarnings annotation allows you to selectively unwarn a particular code segment (that is, a class or method). The idea is that when you see a warning, you’ll investigate it, and if you’re sure it’s not a problem, you can add an @SuppressWarnings annotation so that you don’t see the warning again. While it sounds like it will screen out potential errors, it will actually improve code security because it will prevent you from being indifferent to warnings — every warning you see will be worth noticing.

The problem I often encounter is that I don’t know when to use the @supressWarnings notation, so I make the following arrangement

Use:

@ SuppressWarnings (” “)

@SuppressWarnings({})

@SuppressWarnings(value={})

A. @ SuppressWarings annotation

Purpose: Prevents the compiler from generating warning messages.

Example 1 — Suppress a single type of warning:

@SuppressWarnings(“unchecked”)

public void addItems(String item){

@SuppressWarnings(“rawtypes”)

List items = new ArrayList();

items.add(item);

}

Example 2 — Suppress 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. Annotating objectives

According to the @SuppressWarnings source code, the annotation targets are classes, fields, functions, function inputs, constructors, and local variables of functions. It is recommended that annotations be declared in the location closest to where the warning occurs.

Suppress warning keywords

Suppress the warning keyword

  • 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 related to Deprecated annotations
  • Deprecation to Suppress Warnings relative to deprecation
  • Fallthrough to suppress warnings about 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) 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 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(ignored at — – — – Serializable class does not declare the serialVersionUID variable.
  • Static-access to suppress Warnings relative to incorrect static access
  • Synthetic -access to suppress warnings that subclasses do not optimally access inner classes
  • Unchecked to suppress warnings that are not type checking operations
  • Undamp-field-access to suppress Warnings relative to field access undiminished
  • Unused to suppress warnings relative to unused code