Java:如何避免派生接口中覆盖弃用成员的弃用警告?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1213362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: How to avoid deprecated warning in derived interfaces which override deprecated members?
提问by Daniel Fortunov
Consider the following simplified interface inheritence hierarchy:
考虑以下简化的接口继承层次结构:
// Starting point:
public interface Base {
void Foo();
}
public interface Derived extends Base {
}
It is intended to move the Foomethod from the Baseinterface to the Derivedinterface:
它旨在将Foo方法从Base接口移动到Derived接口:
// Desired end-point:
public interface Base {
}
public interface Derived extends Base {
void Foo();
}
In order to phase in this breaking change, it is desired to retain backwards compatibility of the Baseinterface for some time.
为了逐步进行这种重大更改,需要在Base一段时间内保留接口的向后兼容性。
This can be achieved by marking the method on the Baseinterface as @Deprecated:
这可以通过将Base接口上的方法标记为@Deprecated:
// Intermediate state:
public interface Base {
/**
* @deprecated This method is deprecated as of release X. Derived.Foo should be used instead.
*/
@Deprecated void Foo();
}
public interface Derived extends Base {
void Foo();
}
When I compile this code I receive a compiler warning for Derived:
当我编译这段代码时,我收到一个编译器警告Derived:
[deprecation] Foo() in interface Base has been deprecated
[弃用] 接口 Base 中的 Foo() 已弃用
Oddly, if I remove the @deprecatedfrom the documentation in Base(but leave the @Deprecated) this warning disappears.
奇怪的是,如果我@deprecated从文档中删除Base(但保留@Deprecated)这个警告就会消失。
Is it correct that I get this warning, and if so, how can I work around this?
我收到此警告是否正确,如果是,我该如何解决?
The warning seems to communicate that Derived.Foois "using" Base.Foo(which is deprecated). But the only capacity in which Derived.Foois "using" the deprecated Base.Foois to override it. That seems to say that you are not allowed to override deprecated interface methods in derived methods.
该警告似乎传达了Derived.Foo“使用” Base.Foo(已弃用)的信息。但是Derived.Foo“使用”已弃用的唯一能力Base.Foo是覆盖它。这似乎是说您不允许在派生方法中覆盖已弃用的接口方法。
If this is the case, should I then decorate Derivedwith @SuppressWarnings("deprecation")to suppress the warning?
如果是这样的话,我应该再装饰Derived用@SuppressWarnings("deprecation")相应的警告?
采纳答案by KLE
I believe your requirement is valid, I have no doubt that overriding the deprecated method is the correct way to go.
我相信您的要求是有效的,我毫不怀疑覆盖已弃用的方法是正确的方法。
I believe the difference between @deprecated and @Deprecated is mainly historical. @Deprecated is the official way in java 5, but is new, so we are expected to double it with @deprecated.
我相信@deprecated 和@Deprecated 之间的区别主要是历史性的。@Deprecated 是 java 5 中的官方方式,但它是新的,所以我们希望使用 @deprecated 将它加倍。
Also note that, sadly enough, @Deprecated doesn't let you specify information .. while information is usually needed, for example to tell what should be used as a replacement, or when the deprecated method is expected to be completely removed.
另请注意,遗憾的是,@Deprecated 不允许您指定信息 .. 而通常需要信息,例如告诉什么应该用作替代品,或者预计何时完全删除不推荐使用的方法。
Not knowing more, and knowing the problem will disappear as soon as you effectively remove the super method, I would use the @SuppressWarnings("deprecation"), possibly with a comment for your successors to understand ... (and another comment on the super method to tell them to remove all that when deleting the method). ;-)
不知道更多,并且知道一旦您有效地删除超级方法,问题就会消失,我会使用@SuppressWarnings("deprecation"),可能会附上一条评论,让您的继任者理解......(以及另一条评论super 方法告诉他们在删除方法时删除所有内容)。;-)
回答by MattC
if I understand correctly, you need a @SuppressWarnings("deprecation") at the beginning of your classes that implement the deprecated interface/function. Or am I way off base here?
如果我理解正确,您需要在实现已弃用接口/功能的类的开头使用 @SuppressWarnings("deprecation")。还是我离基地很远?
回答by Raven
If you add @Deprecated to your derived declaration of Foo() I believe the warning will go away.
如果您将 @Deprecated 添加到 Foo() 的派生声明中,我相信警告会消失。
public interface Derived extends Base {
@Deprecated void Foo();
}
回答by Noel Grandin
There is no way to accomplish what you want.
没有办法完成你想要的。
Deprecation is a relatively simply mechanism and does not support this use case.
弃用是一种相对简单的机制,不支持此用例。
The way that deprecation works is that anything that references a deprecated method or field generates a warning.
弃用的工作方式是任何引用弃用方法或字段的内容都会生成警告。
The only exception is if the code using the deprecated method/field is deprecated itself.
唯一的例外是使用已弃用方法/字段的代码本身是否已弃用。

