Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No compilation warning with @Deprecated annotation?

Tags:

According to Java tutorial on Oracle, if deprecated method marked with @Deprecated annotation is used, compiler should be giving warning on compilation. But with following code sample, I am not getting any warning in the console.

Java version used: 1.8.0_112

Please let me know what could be missing here. Thanks.

public class PreDefinedAnnotationTypesTest {

/**
 * This method is deprecated.
 * @deprecated
 */
@Deprecated
public void m1(){

}

public static void main(String[] args) {
    PreDefinedAnnotationTypesTest obj = new PreDefinedAnnotationTypesTest();
    obj.m1();
}

}

like image 728
Omkar Shetkar Avatar asked Nov 17 '16 04:11

Omkar Shetkar


People also ask

What does @deprecated annotation do?

The @Deprecated annotation tells the compiler that a method, class, or field is deprecated and that it should generate a warning if someone tries to use it. That's what a deprecated class or method is.

When using @deprecated annotation what other annotation should be used?

Using the @Deprecated Annotation J2SE 5.0 introduces a new language feature called annotations (also called metadata). One of the Java language's built-in annotations is the @Deprecated annotation. To use it, you simply precede the class, method, or member declaration with "@Deprecated."

What does @deprecated mean in Java?

A program element annotated @Deprecated is one that programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists.

Which annotation can cancel out a warning on a method using the @deprecated API at compile time?

You can use the @SuppressWarnings annotation to suppress warnings whenever that code is compiled. Place the @SuppressWarnings annotation at the declaration of the class, method, field, or local variable that uses a deprecated API.


1 Answers

From docs

The compiler suppresses deprecation warnings if a deprecated item is used within an entity which itself is deprecated or is used within the same outermost class or is used in an entity that is annotated to suppress the warning.

so your function is being used within the same class in which it is declared simply try to use in some other class.

In the below image the wontShowWarning function will not generate any warning although show() funtion will, which is from another class.

The API design can have different rules for itself because it is presumed that the outermost classes will be modified according to new design so this is just a indication to other classes

enter image description here

like image 163
Pavneet_Singh Avatar answered Sep 23 '22 16:09

Pavneet_Singh