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 deprecatedthatClass.theOldWay(); ^1 warning
From the Java Language Specification:
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.