显示相同类别方法的不推荐使用的警告 [英] Show deprecated warnings for methods of same class
问题描述
使用eclipse,只要我们使用的方法的任何标记为 @Deprecated
,我就想看到一个警告。如果是这样,方法调用被正确地删除,但如果方法源于同一个类,eclipse不会发出警告。请看下面的截图。
为了更好地再现,我还将提供textform中的代码:
/ * MainClass.java * /
public class MainClass {
public static void main(String [] args){
MainClass.foo();
MemberClass.foo();
OtherClass.foo();
}
@Deprecated
public static void foo(){
return;
}
private static class MemberClass {
@Deprecated
protected static void foo(){
return;
}
}
}
/ * OtherClass.java * /
public class OtherClass {
@Deprecated
public static void foo(){
return;
}
}
而只有 OtherClass.foo ()
会生成警告。为什么?如何解决这个问题?
-
启用项目特定设置
已停用 -
窗口
- >首选项
- >Java
- >编译器
- >错误/警告
- >已弃用和限制的API
如果完全设置为警告
,从上图可以看出。
注意: Eclipse没有显示不赞成的警告?是不相关的
code> @Deprecated 注释不在javadoc中,但实际上在 Java语言规范,第9.6.4.6节。
本质上说,使用不推荐使用的元素会产生警告
使用和声明都在同一最外层。
因为MainClass.foo()
和MemberClass.foo()$ c
$ c>都在 MainClass
中,您对它们的调用也位于 MainClass
中,不会生成警告。 / p>
对 OtherClass.foo()
的调用不在同一个最外层,所以一个生成一个警告。
我相信这个规则的原因是最外层或顶级类中的代码可以被推定为一起维护和发展。使一个不赞成的元素通常是一个信号,代码外面,可能由其他人维护,这个元素会发生什么。班级通常会有不同的规则。内部使用不赞成使用的元素(如私有元素)可能完全正常。
编译器或IDE发出额外的警告可能是合法的超出了JLS要求的范围,例如在同一个最外层中使用不赞成使用的元素。我不知道有什么系统可以这样做。
但是你使用的是Eclipse,我确定它和其他IDE可以轻松找到课堂内某些元素的所有用法。
Using eclipse, I want to see a warning as soon as any of my used methods is marked as @Deprecated
. Method calls are correctly crossed out if so, but eclipse does not give out a warning if the method origins from the same class. See the screenshot below.
For better reproducing, I'll also provide the code in textform:
/* MainClass.java */
public class MainClass {
public static void main(String[] args) {
MainClass.foo();
MemberClass.foo();
OtherClass.foo();
}
@Deprecated
public static void foo() {
return;
}
private static class MemberClass {
@Deprecated
protected static void foo() {
return;
}
}
}
/* OtherClass.java */
public class OtherClass {
@Deprecated
public static void foo() {
return;
}
}
Whereas only OtherClass.foo()
generates a warning. Why? How can I fix this?
Enable project specific settings
is deactivatedWindow
->Preferences
->Java
->Compiler
->Errors/Warnings
->Deprecated and restricted API
if fully set toWarning
, as can be seen from the picture above.
Note: Eclipse not showing deprecated warning? is not related
The exact definition of the @Deprecated
annotation is not in the javadoc, but is actually in the Java Language Specification, section 9.6.4.6.
Essentially it says that uses of a deprecated element will generate a warning unless
The use and declaration are both within the same outermost class.
Since MainClass.foo()
and MemberClass.foo()
are both within MainClass
, and your calls to them are also in MainClass
, no warnings are generated.
The call to OtherClass.foo()
is not within the same outermost class, so that one generates a warning.
I believe the reason for this rule is that the code within an outermost or top-level class can be presumed to be maintained and evolved together. Making an element deprecated is usually a signal to code "outside," possibly maintained by other people, that something is going to happen to that element. Often different rules apply within a class. Internal use of deprecated elements, like private elements, might be perfectly fine.
It's probably legal for a compiler or an IDE to issue additional warnings beyond those required by the JLS, such as for uses of a deprecated element within the same outermost class. I'm not aware of any system that does this, though.
But you're using Eclipse, and I'm sure that it, as well other IDEs, can easily find all usages of some element within a class.
这篇关于显示相同类别方法的不推荐使用的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!