Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8257037

No javac warning when calling deprecated constructor with diamond

    XMLWordPrintable

Details

    • b21
    • x86_64
    • generic
    • Not verified

    Backports

      Description

        A DESCRIPTION OF THE PROBLEM :
        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.

        I have confirmed this issue is present in Java 15, and has been present since at least Java 8 (I have not tested on Java 7), so it is not a new issue.

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Create a generic class with a deprecated constructor, and another class that calls that constructor using the diamond syntax for the new instance creation. Compile using javac. The diamond constructor usage will not show up as a deprecated usage.

        If the above steps are followed using an explicit type argument or raw generics, a deprecation warning will be emitted as expected.

        new GenericClass<Object>(); // Warning
        new GenericClass<>(); // No warning
        new GenericClass(); // Warning

        Include the provided Example & GenericClass source code in separate Example.java & GenericClass.java files in the same directory. Run javac against it with the "-Xlint:deprecation" option. The diamond usage will not be included as a warning.

        javac -Xlint:deprecation Example.java

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        The diamond constructor should show up as a deprecation warning.
        ACTUAL -
        No deprecation warning is emitted for the diamond constructor, but is shown for the non-diamond usages of the same constructor. The deprecation is not shown regardless of whether the "-Xlint:deprecation" command line option is used.

        Example.java:3: warning: [deprecation] GenericClass() in GenericClass has been deprecated
                GenericClass<Object> o1 = new GenericClass<Object>(); // Warning
                                          ^
        Example.java:5: warning: [deprecation] GenericClass() in GenericClass has been deprecated
                GenericClass<Object> o3 = new GenericClass(); // Warning
                                          ^
        Note: Example.java uses unchecked or unsafe operations.
        Note: Recompile with -Xlint:unchecked for details.
        2 warnings

        ---------- BEGIN SOURCE ----------
        public class Example {
            public static void main(String[] args) {
                GenericClass<Object> o1 = new GenericClass<Object>(); // Warning
                GenericClass<Object> o2 = new GenericClass<>(); // No warning
                GenericClass<Object> o3 = new GenericClass(); // Warning
            }
        }

        public class GenericClass<T> {
            @Deprecated
            public GenericClass() {}
        }

        ---------- END SOURCE ----------

        FREQUENCY : always


        Attachments

          Issue Links

            Activity

              People

                gli Guoxiong Li
                webbuggrp Webbug Group
                Votes:
                0 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved: