I am trying to use the @Deprecated annotation. The @Deprecated documentation says that: "Compilers warn when a deprecated program element is used or overridden in non-deprecated code". I would think this should trigger it, but it did not. javac version 1.7.0_09 and compiled using and not using -Xlint and -deprecation.
public class TestAnnotations {
public static void main(String[] args)
{
TestAnnotations theApp = new TestAnnotations();
theApp.thisIsDeprecated();
}
@Deprecated
public void thisIsDeprecated()
{
System.out.println("doing it the old way");
}
}
Edit: per the comment of gd1 below regarding it only working if the method is in another class, I added a second class. And it DOES WARN on the call to theOldWay():
public class TestAnnotations {
public static void main(String[] args)
{
TestAnnotations theApp = new TestAnnotations();
theApp.thisIsDeprecated();
OtherClass thatClass = new OtherClass();
thatClass.theOldWay();
}
@Deprecated
public void thisIsDeprecated()
{
System.out.println("doing it the old way");
}
}
class OtherClass {
@Deprecated
void theOldWay()
{
System.out.println("gone out of style");
}
}
The warning:
/home/java/TestAnnotations.java:10: warning: [deprecation] theOldWay() in OtherClass has been deprecated
thatClass.theOldWay(); ^
1 warning
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.
Annotation Types Used by the Java Language @Deprecated @Deprecated annotation indicates that the marked element is deprecated and should no longer be used. The compiler generates a warning whenever a program uses a method, class, or field with the @Deprecated annotation.
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.
Using the @Deprecated Annotation To use it, you simply precede the class, method, or member declaration with "@Deprecated." Using the @Deprecated annotation to deprecate a class, method, or field ensures that all compilers will issue warnings when code uses that program element.
From the Java Language Specification:
A Java compiler must produce a deprecation warning when a type, method, field, or constructor whose declaration is annotated with the annotation @Deprecated is used (i.e. overridden, invoked, or referenced by name), unless:
The use is within an entity that is itself annotated with the annotation @Deprecated; or
The use is within an entity that is annotated to suppress the warning with the annotation @SuppressWarnings("deprecation"); or
The use and declaration are both within the same outermost class.
Your example is an example of the last condition: you're only using the deprecated method from the same outermost class as the deprecated method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With