Search code examples
javajavacjava-11

Why doesn't javac produce a warning when using a deprecated constructor of a parameterized type?


Say you have a parameterized class with a deprecated constructor, and a User class that calls this constructor. In the example below, using the diamond operator, javac (version 11.0.11) does not produce a deprecation warning:

class DepTester<T> {
    T t;

    @Deprecated
    public DepTester(T t) {
        this.t = t;
    }
}

class User {
    DepTester<String> tester = new DepTester<>("This does not produce a warning!");
}

However, with an explicit type argument in the constructor call, the warning is issued:

class User {
    DepTester<String> tester = new DepTester<String>("This produces a warning.");
}

[WARNING] DepTester.java:[17,40] DepTester(T) in com.foo.DepTester has been deprecated

The explicit type argument is normally not included, which IntelliJ inspection also warns about:

Explicit type argument String can be replaced with <>

I can't find an explanation for this behaviour in the Java language spec. I tried with javac 17, and it produces a warning in both cases, so is this a bug in javac 11?

EDIT: My bug report to Oracle was assigned an ID in the Java bug database: JDK-8281107


Solution

  • You encountered bug JDK-8257037, “No javac warning when calling deprecated constructor with diamond”:

    No deprecation warning is emitted when compiling a class that calls a deprecated constructor when using the diamond syntax to specify the generic types. A deprecation warning is emitted when calling the same constructor using an explicit type argument or a raw type.

    It has been fixed with JDK 17.
    There’s also a reported backport to JDK 16, but none for earlier versions.