简体   繁体   中英

How does @SuppressWarnings and other custom annotation work internally?

I am under confusion how does @SuppressWarnings works internally. If we see the source code of it, it's something as below:

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

Now, if we see it getting implemented in code, it's something like,

@SuppressWarnings({"unused"})
public static void main(String args[]){
int i;
}

So questions are:-

1) As soon as we pass "unused" as a parameter, the eclipse stops throwing warning. Similarly, we can use "unchecked", "deprecation" , etc. So how does it work? I mean we have only a method named value() in the @interface with it's return type being String[]. So it does everything, how? And why the name of method is value()? Does this method has some special significance which performs something internally to catch the parameters like "unused"?

2) Sometime we can see that there is default as specificed below in some @interface. So what is default? From java8 we have a new concept of default methods. But this default is used in lower version of java too. How does this work and what is it? Is this a keyword in lower than java8 versions?

public @interface MyIntf{

    /**
     * The error message.
     */
    String message() default "My Default Message";

    /**
     * The group.
     */
    Class<?>[] groups() default {};

    /**
     * the payload.
     */
    Class<? extends Payload>[] payload() default {};
}

The annotation doesn't do anything. It's just there, in the source code.

Th Eclipse compiler, when it sees one on a method (or class, or constructor, etc.), just doesn't emit some of the warnings it would normally emit, depending on what is in the value attribute of the annotation.

why the name of method is value()

Because that's what the designer of the annotation chose as attribute name. It could be named anything. The advantage of using "value" as its name is that it allows writing

@SuppressWarnings("unused")

instead of having to write

@SuppressWarnings(value = "unused")

Regarding the default keyword:

String message() default "My Default Message";

This simply means that if you don't explicitely specify a value for the message attribute of the annotation, its value is "My Default Message" .

You are asking 2 separate questions. I will answer them separately.

As soon as we pass "unused" as a parameter, the eclipse stops throwing warning. Similarly, we can use "unchecked", "deprecation" , etc. So how does it work?

@SuppressWarning is a special annotation in that the java compiler knows about it. When the compile compiles the classes and finds warnings, it looks to see if there are any @SuppressWarning annotations on that code and if so what warnings it's supposed to suppress. There is a mapping of string names to the compiler warnings so it knows what warnings not to print out. Here is a list I found in another SO question:

What is the list of valid @SuppressWarnings warning names in Java?

Eclipse is constantly compiling your code on the fly so it will see your @SuppressWarning and start ignoring the warning when a recompile is triggered (usually on save)

Sometime we can see that there is default as specificed below in some @interface. So what is default?

Default is the default value to use if a user doesn't specify what the value is. So in your example you can specify a message like this:

@MyInf(message="my custom message")

If you didn't specify a message like @MyInf , then the message is set to "My Default Message"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM