为同一个类的方法显示已弃用的警告 [英] Show deprecated warnings for methods of same class

查看:24
本文介绍了为同一个类的方法显示已弃用的警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Eclipse,我希望在我使用的方法的任何 标记为 @Deprecated 时立即看到警告.如果是这样,方法调用会被正确划掉,但如果方法来自同一类,eclipse 不会发出警告.请参阅下面的屏幕截图.

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;
    }
}

而只有 OtherClass.foo() 会生成警告.为什么?我该如何解决这个问题?

Whereas only OtherClass.foo() generates a warning. Why? How can I fix this?

  • 启用项目特定设置 已停用
  • Window->Preferences->Java->Compiler->错误/警告->Deprecated and Restricted API 如果完全设置为 Warning,如上图所示.
  • Enable project specific settings is deactivated
  • Window->Preferences->Java->Compiler->Errors/Warnings->Deprecated and restricted API if fully set to Warning, as can be seen from the picture above.

注意:Eclipse 未显示弃用警告?不是强>相关

推荐答案

@Deprecated 注释的确切定义不在 javadoc 中,但实际上在 Java 语言规范,第 9.6.4.6 节.

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.

由于 MainClass.foo()MemberClass.foo() 都在 MainClass 中,您对它们的调用也在 MainClass 中code>MainClass,不会产生警告.

Since MainClass.foo() and MemberClass.foo() are both within MainClass, and your calls to them are also in MainClass, no warnings are generated.

OtherClass.foo() 的调用不在同一个最外层类中,因此会产生警告.

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.

编译器或 IDE 发出超出 JLS 要求的附加警告可能是合法的,例如在同一最外层类中使用已弃用的元素.不过,我不知道有任何系统可以做到这一点.

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.

但是您使用的是 Eclipse,我确信它以及其他 IDE 可以轻松找到类中某个元素的所有用法.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆