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

Misleading recommendation from diamond finder.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 9
    • None
    • tools
    • b96
    • generic
    • generic
    • Verified

      When the following program is compiled with -XDfind=diamond:

      // ----- 8< -------
      public class X<T> {
          T field1;
          public X(T param){
              field1 = param;
          }
          public static void main(String[] args) {
              X.testFunction(new X<Object>("hello").getField()); //prints 2
              X.testFunction(new X<>("hello").getField()); // prints 1
          }
          public static void testFunction(String param){
              System.out.println(1);
          }
          public static void testFunction(Object param){
              System.out.println(2);
          }
          public T getField(){
              return field1;
          }
      }

      javac issues a recommendation:

      /home/srikanth/tmp/X.java:7: warning: Redundant type arguments in new expression (use diamond operator instead).
              X.testFunction(new X<Object>("hello").getField()); //prints 2
                                  ^
        explicit: X<Object>
        inferred: X<String>
      1 warning

      The type argument <Object> is actually not redundant. Removing it and using the diamond
      operator instead per javac recommendation would actually change the meaning of the program.
      (instead of printing 2 followed by 1, it would actually print 1 followed by 1).

            sadayapalam Srikanth Adayapalam (Inactive)
            sadayapalam Srikanth Adayapalam (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: