3

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 {};
}
Hardy
  • 18,659
  • 3
  • 49
  • 65
Deca
  • 1,155
  • 1
  • 10
  • 19
  • Regarding the keyword issue, `default` has always been a keyword, as it is being used in `switch`. – RealSkeptic Oct 23 '15 at 18:17
  • ya default was always used in switch case. Something like, if it doesn't find any matching case, goes to default. But how does it work here. – Deca Oct 23 '15 at 18:20
  • "and other custom annotation"? `@SuppressWarnings` is not a *custom* annotation. It's from the `java.lang` package, so doesn't get more *core* than that. – Andreas Oct 23 '15 at 18:48

2 Answers2

1

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".

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Why isn't it designed as a Marker Interface. I mean, couldn't things have been like we declare it as marker and whatever warning is specified in the parameter, the compiler doesn't throw the warning. I mean we could get away with value(), can't we? – Deca Oct 23 '15 at 19:11
  • Well, for one thing, an interface can't be used to annotate fields, methods, etc. And the idea of SuppressWarnings is that you can pass whatever value you want, and the compiler is free to implement whatever suppressable checks it wants. I'm not sure I follow what you want. – JB Nizet Oct 23 '15 at 19:18
  • Ya, you got it right. One more question, if we see, the definition is something like, String[] value(); in the interface. This means something like, method value() returns a string array. But while using the annotation, we pass it as parameters, which is something should have been something like value(String[] strArray). This is not clear to me, we pass the parameters with the annotations, and that gets passed to the method value(). But value() doesn't accept any String[], instead returns String[]. – Deca Oct 23 '15 at 19:25
  • value() returns a String[] because when you programmatically get the annotations on a class, method or field and you get back a SuppressWarnings instance, you want toknow what the value is and you thus get back a String[] array. Parameters are not passed to annotations at runtime. They're embedded in the byte-code by the compiler. – JB Nizet Oct 23 '15 at 19:31
  • If you don't mind, would you please explain this part "you want to know what the value is and you thus get back a String[] array." I mean, I already know what value isn't it. Like, I already know that "unused" is to be suppressed. Then why to get back the same String[]. – Deca Oct 23 '15 at 19:41
  • You, as the developer who adds the annotation to the code, know what the value is. So you write it in the code, and (in case of a runtime annotation) the compiler stores it in the byte-code. The tool which inspects the classes, fields and methods at runtime doesn't have any idea of what the value is, and wants to know. So it gets the annotation instances dynamically from the Class, Method, Field, etc. and calls the annotations methods (like `value()`), to know what the value is, i.e. to know what you wrote in the `value` attribute. – JB Nizet Oct 23 '15 at 19:47
0

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"

Community
  • 1
  • 1
dkatzel
  • 31,188
  • 3
  • 63
  • 67
  • Thanks for your reply. But this is not clear to me, There is a mapping of string names to the javac warnings so it knows what warnings not to print out. – Deca Oct 23 '15 at 18:36