Java: How to avoid deprecated warning in derived interfaces which override deprecated members?
-
06-07-2019 - |
Question
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 Foo
method from the Base
interface to the Derived
interface:
// 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 Base
interface for some time.
This can be achieved by marking the method on the Base
interface as @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
:
[deprecation] Foo() in interface Base has been deprecated
Oddly, if I remove the @deprecated
from the documentation in Base
(but leave the @Deprecated) this warning disappears.
Is it correct that I get this warning, and if so, how can I work around this?
The warning seems to communicate that Derived.Foo
is "using" Base.Foo
(which is deprecated). But the only capacity in which Derived.Foo
is "using" the deprecated Base.Foo
is to override it. That seems to say that you are not allowed to override deprecated interface methods in derived methods.
If this is the case, should I then decorate Derived
with @SuppressWarnings("deprecation")
to suppress the warning?
Solution
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.
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.
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). ;-)
OTHER TIPS
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?
If you add @Deprecated to your derived declaration of Foo() I believe the warning will go away.
public interface Derived extends Base {
@Deprecated void Foo();
}
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.