Java @SuppressWarnings Annotations

Source: Internet
Author: User
Tags finally block

First, preface

When coding, we always find a warning that the following variables are not being used:

The code above is compiled and can be run, but the exclamation point in front of each line seriously hinders the breakpoint we have to determine if the line is set. At this point we can add @SuppressWarnings ("unused") before the method to remove these "exclamation marks".

Second, @SuppressWarings annotations

Role: Used to suppress compiler-generated warning messages.

Example to suppress single-type warnings:

@SuppressWarnings ("unchecked") public void Additems (String item) {  @SuppressWarnings ("Rawtypes")   List items = New ArrayList ();   Items.Add (item);}

Example 2--suppresses multiple types of warnings:

@SuppressWarnings (value={"Unchecked", "rawtypes"}) public void Additems (String item) {   List items = new ArrayList ();   Items.Add (item);}

Example 3--suppresses all types of warnings:

@SuppressWarnings ("all") public void Additems (String item) {   List items = new ArrayList ();   Items.Add (item);}

Iii. Annotated target

by @SuppressWarnings Source, the annotated target is a local variable of class, field, function, function parameter, constructor and function.

The home recommendation note should be declared at the location closest to where the warning occurred.

Iv. keywords to suppress warnings

Key words Use
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-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 if 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 o 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

V. Java LINT Options

1. Meaning of Lint

Used to perform additional checks in more detail during the process of compiling the program.

2. Standard and non-standard options for Javac

standard option: refers to the options that are supported in the current and future versions, such as -CP and- D.

Non-standard options: refers to options that are supported by the current version, but are not necessarily supported in the future. View the non-standard options supported by the current version through Javac-x.

3. View the warning message

By default, execution Javac only displays the main message of the warning, but also prevents the compilation process. To see the details of the warning, you need to execute javac-xlint:keyword to compile the source code.

Vi. Summary

Now no longer afraid don't know set breakpoints No!

Java @SuppressWarnings Annotations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.