We have a Java project. We enable -Xlint
(enable warnings) and -Werror
(treat warning as error) flags for javac
, to make sure our code is warning-free. Recently we decide to deprecate a class. The problem is in some cases @SuppressWarnings("deprecation")
will not suppress the deprecation warning at all, resulting in build failure. Below is a list of use cases that I ran into:
- Imported in other non-deprecated classes.
- Imported in other deprecated classes.
- Parent class.
Type parameter. For example
@SuppressWarnings("deprecation") public class Foo extends Bar<DeprecatedClass> { ... }
However, this one has no warning even without suppress:
@Deprecated public class DeprecatedClass extends Bar<DeprecatedClass> { ... }
AFAIK, there is no syntax for annotating imports, so for case 1 and 2 our solution is to either import * or avoid importing. For case 3 and 4, both Java 6 and 7 do not suppress the warning. Java 8 will correctly suppress it (maybe a bug is fixed). So far no solution for this.
Unfortunately, we have to support Java 6, 7 and 8 at this point. Is there way to deal with the problem? It is a road block for our Java API evolution.
ADDENDUM
Many people ask why do we still use the deprecated class in our own codebase. The reason is that the project is a library, supporting many different clients. When introducing new replacement API, we have to first deprecate our old API, keep it in our codebase, wait for all clients to migrate then remove it. There are three common use cases:
- We deprecate class
Foo
andBar
, whereFoo
extendsBar
. This is the case 2 and 3 in my question. - We deprecate class
Foo
andBar
, whereFoo
extendsCollection<Bar>
. This is the case 2 and 4. - We must keep all test code for class
Foo
andBar
. The test code imports these classes. This is the case 1.
Why keep the test? Don't forget that if a serious bug (e.g. memory leak, security issue) is discovered, and the clients can't easily migrate to the new version, we still need to provide bug fix to the old API. And all changes must be tested.
I feel our situation should be fairly common in software library development and API evolution. Surprisingly it took Java such long time (until Java 8) to fix the bug.