JVM是否会在运行时使用@deprecated代码发出警告?
如果没有,我可以使用@deprecated代码警告它吗?
解决方法:
Will JVM issue warning during runtime for using @deprecated codes?
没有.
If it doesn’t, can i make it warn for using @deprecated codes?
不是我所知道的任何方式.
OTOH可以在运行时找到代码,如果某个特定成员被弃用:
import java.awt.Panel;
import java.lang.reflect.Method;
import java.lang.annotation.Annotation;
class TestAnnotations {
@Deprecated
public Object getStuff() {
return null;
}
public static void showAnnotations(
Object obj,
String methodName) throws Exception {
Class cls = obj.getClass();
Method m = cls.getMethod(methodName);
System.out.println(m);
Annotation[] as = m.getDeclaredAnnotations();
for (Annotation a : as) {
System.out.println(a);
}
}
public static void main(String[] args) throws Exception {
Panel p = new Panel();
showAnnotations(p, "show");
TestAnnotations ta = new TestAnnotations();
showAnnotations(ta, "getStuff");
}
}
产量
public void java.awt.Component.show()
@java.lang.Deprecated()
public java.lang.Object TestAnnotations.getStuff()
@java.lang.Deprecated()
Press any key to continue . . .