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

Warning issued despite @SafeVarargs annotation on constructor

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 9
    • 8u25
    • tools
    • b48
    • x86_64
    • linux
    • Verified

        FULL PRODUCT VERSION :
        java version "1.8.0_25"
        Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
        Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)


        ADDITIONAL OS VERSION INFORMATION :
        Linux example.com 3.18.1-desktop-3.mga5 #1 SMP Tue Dec 30 22:09:00 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

        A DESCRIPTION OF THE PROBLEM :
        Consider the following code:


        import java.util.Iterator;
        import java.util.LinkedList;

        public class Test {
                static class Foo<T> {
                        @SafeVarargs
                        public Foo(T... args) {
                        }
                }

                public static void main(String[] args) {
                        Foo<String> foo0 = new Foo<>("0", "1"); // Clear
                        Foo<String> foo1 = new Foo<String>("0", "1"); // Clear

                        Iterator<String> i = new LinkedList<String>().iterator();

                        Foo<Iterator<String>> foo2 = new Foo<>(i, i); // Warning
                        Foo<Iterator<String>> foo3 = new Foo<Iterator<String>>(i, i); // Clear
                }
        }


        ECJ compiles this code with no warnings. When compiled with Oracle JDK or OpenJDK, though, a single warning is issued:

        Test.java:19: warning: [unchecked] unchecked generic array creation for varargs parameter of type Iterator<String>[]
                        Foo<Iterator<String>> foo2 = new Foo<>(i, i); // Warning
                                                     ^
        1 warning


        There seems to be some relation to the use of the diamond operator. Also, the behavior w.r.t. the warning seems inconsistent when compared with the other three cases, especially with the `... foo0 = ...' case.


        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Compile the following code:

        import java.util.Iterator;
        import java.util.LinkedList;

        public class Test {
                static class Foo<T> {
                        @SafeVarargs
                        public Foo(T... args) {
                        }
                }

                public static void main(String[] args) {
                        Foo<String> foo0 = new Foo<>("0", "1"); // Clear
                        Foo<String> foo1 = new Foo<String>("0", "1"); // Clear

                        Iterator<String> i = new LinkedList<String>().iterator();

                        Foo<Iterator<String>> foo2 = new Foo<>(i, i); // Warning
                        Foo<Iterator<String>> foo3 = new Foo<Iterator<String>>(i, i); // Clear
                }
        }


        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        No warnings should be issued by the compiler
        ACTUAL -
        Test.java:19: warning: [unchecked] unchecked generic array creation for varargs parameter of type Iterator<String>[]
                        Foo<Iterator<String>> foo2 = new Foo<>(i, i); // Warning
                                                     ^
        1 warning


        ERROR MESSAGES/STACK TRACES THAT OCCUR :
        Test.java:19: warning: [unchecked] unchecked generic array creation for varargs parameter of type Iterator<String>[]
                        Foo<Iterator<String>> foo2 = new Foo<>(i, i); // Warning
                                                     ^
        1 warning


        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        import java.util.Iterator;
        import java.util.LinkedList;

        public class Test {
                static class Foo<T> {
                        @SafeVarargs
                        public Foo(T... args) {
                        }
                }

                public static void main(String[] args) {
                        Foo<String> foo0 = new Foo<>("0", "1"); // Clear
                        Foo<String> foo1 = new Foo<String>("0", "1"); // Clear

                        Iterator<String> i = new LinkedList<String>().iterator();

                        Foo<Iterator<String>> foo2 = new Foo<>(i, i); // Warning
                        Foo<Iterator<String>> foo3 = new Foo<Iterator<String>>(i, i); // Clear
                }
        }

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

              jlahoda Jan Lahoda
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

                Created:
                Updated:
                Resolved: