Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities.

Each time the code is compiled, there are often many warning messages that affect the filtering of the code error message. Using the @SuppressWarnings annotation, you can eliminate the specified compiler warning problem

1. Introduction to @SuppressWarnings

The @SuppressWarnings function is to keep the compiler from generating warnings. As you can see from the source code, this annotation has been used since JDK1.5. It supports classes, properties, methods, parameters, constructs, and local variables. There is only one variable in the annotation, value, which is a string array type. Multiple types of warnings are supported.

/**
 * Indicates that the named compiler warnings should be suppressed in the
 * annotated element (and in all program elements contained in the annotated
 * element).  Note that the set of warnings suppressed in a given element is
 * a superset of the warnings suppressed in all containing elements.  For
 * example, if you annotate a class to suppress one warning and annotate a
 * method to suppress another, both warnings will be suppressed in the method.
 *
 * <p>As a matter of style, programmers should always use this annotation
 * on the most deeply nested element where it is effective.  If you want to
 * suppress a warning in a particular method, you should annotate that
 * method rather than its class.
 *
 * @author Josh Bloch
 * @since 1.5
 * @jls 4.8 Raw Types
 * @jls 4.12.2 Variables of Reference Type
 * @jls 5.1.9 Unchecked Conversion
 * @jls 5.5.2 Checked Casts and Unchecked Casts
 * @jls 9.6.3.5 @SuppressWarnings* /
// Class, field, method, function input parameter, construct, local variable
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.SOURCE)
public @interface SuppressWarnings {
    /**
     * The set of warnings that are to be suppressed by the compiler in the
     * annotated element.  Duplicate names are permitted.  The second and
     * successive occurrences of a name are ignored.  The presence of
     * unrecognized warning names is <i>not</i> an error: Compilers must
     * ignore any warning names they do not recognize.  They are, however,
     * free to emit a warning if an annotation contains an unrecognized
     * warning name.
     *
     * <p> The string {@code "unchecked"} is used to suppress
     * unchecked warnings. Compiler vendors should document the
     * additional warning names they support in conjunction with this
     * annotation type. They are encouraged to cooperate to ensure
     * that the same names work across multiple compilers.
     * @return the set of warnings to be suppressed
     */
    String[] value();
}
Copy the code

Common types used:

  • Single-type warnings such as: @suppresswarnings (“unchecked”) SuppressWarnings that are not being type checked
  • Multi-type warnings such as: @suppresswarnings (“unchecked”,”unused”) SuppressWarnings for codes that have not undergone type checking or have not been used before
  • All warnings such as @SuppressWarnings(“all”) suppress all warnings

List of warning keywords:

The keyword instructions
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 that enable annotations
deprecation To suppress Warnings relative to deprecation
fallthrough To suppress warnings about missing breaks in switch statements
finally To suppress warnings that the finally module does not return
hiding To suppress warnings of local variables relative to hidden variables
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 ( Ignore using generics without specifying the corresponding type, i.e., without passing parameters with 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 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 type checking operations are not being performed
unqualified-field-access To suppress Warnings relative to Field Access undiminished
unused To suppress warnings relative to unused code

2@SuppressWarnings use

    // Suppress unused warnings
    @SuppressWarnings("unused")
    public void test1(a){
        try {
            // No warning is used
            String s = "";
        } catch(Exception e) { e.printStackTrace(); }}// Suppress not type checking operation Warning Not specifying the corresponding type warning Not using the warning
    @SuppressWarnings({"unchecked","rawtypes","unused"})
    public void test2(a){
        try {
            // No warning is used
            String s = "";
            // No type check operation warning No corresponding type warning is specified
            List items = new ArrayList();
            items.add("Li bai");
        } catch(Exception e) { e.printStackTrace(); }}// Suppress all warnings
    @SuppressWarnings({"all"})
    public void test3(a){
        try {
            String s = "";
            List items = new ArrayList();
            items.add("Li bai");
        } catch(Exception e) { e.printStackTrace(); }}Copy the code

Reference article:

www.cnblogs.com/EasonJim/p/…

My.oschina.net/u/2433960/b…

www.jianshu.com/p/b41277be4…