为什么@Deprecated注释不会触发有关方法的编译器警告?

Sco*_*ter 5 java annotations

我正在尝试使用@Deprecated注释.@Deprecated文档说:"编译器在未弃用的代码中使用或覆盖已弃用的程序元素时发出警告".我认为这应该触发它,但事实并非如此.javac版本1.7.0_09并使用而不是使用-Xlint和-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");
   }
}
Run Code Online (Sandbox Code Playgroud)

编辑:根据下面的gd1的评论关于它只在该方法在另一个类中时才工作,我添加了第二个类.并且它在调用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");
   }


}
Run Code Online (Sandbox Code Playgroud)

警告:

/home/java/TestAnnotations.java:10:警告:[弃用]其他类中的OldWay()已被弃用

    thatClass.theOldWay();
             ^
Run Code Online (Sandbox Code Playgroud)

1警告

JB *_*zet 5

Java语言规范:

当使用注释@Deprecated注释声明的类型,方法,字段或构造函数(即重写,调用或通过名称引用)时,Java编译器必须生成弃用警告,除非:

  • 使用在一个实体内,该实体本身用注释@Deprecated注释; 要么

  • 该用法位于注释为使用注释@SuppressWarnings("deprecation")抑制警告的实体内; 要么

  • 使用和声明都在同一个最外层.

您的示例是最后一个条件的示例:您只使用与弃用方法相同的最外层类中的弃用方法.