Search code examples
javawarningsdeprecatedeclipse-jdt

Deprecate class inheritance only


I would like to deprecate only the extension of a given class, not all the methods and fields contained within a class, using the @Deprecated annotation.

That is, a warning will occur if you extend a given class - but references to methods or fields will not trigger a warning. There are already several classes that extend this class, and I want to have the deprecation warnings target these clients - I can't break them yet (but they can be recompiled - ABI compatibility not needed).

Is it possible in Java 1.6 (JDT compiler)?


Solution

  • Two considerations

    1) The class may already be extended, so don't mark it final or you could break backwards compatibility.

    2) You don't want the class extended, so it should be marked final.

    I think what you should do is extend the old class with a new class, mark the old class deprecated, and declare the new class final. In the new class, you can add the @SuppressWarning tag to quiet the deprecated message, then you should sitll get a clean compile.

    Code using the old class will get a @Deprecated Warning, but will still compile.. Code using the new class will compile cleanly. Kind of a "strong suggestion" to your users instead of a backward compatible break, and pretty easy for them to fix since the API is 100% compatible.