background

When my colleague got acute appendicitis and was bedridden, I helped him develop the project and found that he always added @SuppressWarnings(“all”) on the class. At first glance, it looks familiar, and is often seen in Spring and other open source frameworks, but the most obvious sign of this is the absence of yellow wavy lines for misspelled words in code. With doubt, I decided to learn more about…

Cognitive processes

SuppressWarning: Suppress Oh, as the name suggests, suppression warnings. Read the source code notes, talked about how to use. This annotation can be used almost anywhere, class, method, field, parameter, etc. The source code is as follows:

@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.SOURCE)
public @interface SuppressWarnings{
    String[] value();
}
Copy the code

Advise where to report warning, where to use it, not just on the outermost class. In that sense, my colleague was slacking off. The outermost @SuppressWARNING annotation and the inner @SuppressWarning annotation together scope the target of the inner @SuppressWarning annotation. The source code comments are as follows:

/**
 * 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.
 **/
Copy the code

For example: Handle all warnings: @suppressWarnings (“all”); Or the method to handle expiration @SuppressWarnings(“deprecation”); Or code that handles non-checking exceptions @SuppressWarnings(“unchecked”). Examples of code:

public class SuppressWarningDemo {
    @SuppressWarnings("unchecked")
    public void addItem(String item) {
        List items = new ArrayList();
        items.add(item);
        @SuppressWarnings("unused") int i = 0; }}Copy the code

You can also write:

public class SuppressWarningDemo {
    @SuppressWarnings(value = {"unchecked"."unused"}) public void addItem(String item) { List items = new ArrayList(); items.add(item); int i = 0; }}Copy the code

Of course, if you still want to be lazy, do what my colleague did:

@SuppressWarnings("all") public class SuppressWarningDemo { public void addItem(String item) { List items = new ArrayList(); items.add(item); int i = 0; }}Copy the code

I’m curious where the value of value is defined. I can’t find it in the source code, nor in the JDK documentation. If you happen to know it, please leave a message and let me know. Here are some common values:

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 inSwitch statements finally to Suppress warnings relative to finally block that don'treturn
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 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 fora serializable class static-access to 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 codeCopy the code

conclusion

Write a daily summary on the first day. Well, it’s terrible, but it’s a good start. Stick to the output, deepen the impression of knowledge points, and connect the world. Welcome to pass by friends show a few.