Próbuję użyć @Deprecated annotation. Dokumentacja @Deprecated mówi, że: "Kompilatory ostrzegają, gdy nieaktualny element programu jest używany lub przesłonięty w nie-przestarzałym kodzie". Myślę, że to powinno go uruchomić, ale tak się nie stało. javac w wersji 1.7.0_09 i skompilowany przy użyciu i nie używając -Xlint i -deprecation.Dlaczego adnotacja @Deprecated nie uruchamia ostrzeżenia kompilatora dotyczącego metody?
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: za komentarzu GD1 poniżej traktując ją tylko działać, jeśli metoda jest w innej klasie, dodałem drugą klasę. I robi WARN na wezwanie do 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");
}
}
ostrzeżenie:
/home/java/TestAnnotations.java:10: ostrzeżenie: [deprecation] theOldWay() w OtherClass została zaniechana
thatClass.theOldWay(); ^
1 ostrzeżenie
OSTRZEŻENIE: Po prostu zgaduję. Być może nie ma przestarzałego ostrzeżenia, chyba że wywołasz nieaktualną metodę z innej klasy. Losowe zgadywanie. – gd1
@ gd1 no. Wycofanie nie wyzwala ostrzeżenia.Musisz zarządzać logiką Deprecation w swojej strukturze/aplikacji –