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

Generics: suboptimal error message when actual type arguments arity does not match

XMLWordPrintable

    • b144
    • generic
    • generic
    • Not verified

      FULL PRODUCT VERSION :
      java version "1.8.0_111"
      Java(TM) SE Runtime Environment (build 1.8.0_111-b14)
      Java HotSpot(TM) Client VM (build 25.111-b14, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      Not relevant.

      A DESCRIPTION OF THE PROBLEM :
      With specific Generics pattern, javac does not match method parameters although they do match and issues error:

      error: method properties in class GenericClass<S,E> cannot be applied to given types;

        required: String
        found: String
        reason: actual and formal argument lists differ in length

      Minimal test case is provided to illustrate behavior.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile attached test case.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Compilation OK.
      ACTUAL -
      Compilation fails.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      error: method properties in class GenericClass<S,E> cannot be applied to given types;
                  HashMap<String, List<String>> map = gt.<String, List<String>>properties( "John" );
                                                        ^
        required: String
        found: String
        reason: actual and formal argument lists differ in length
        where S,E are type-variables:
          S extends Object declared in class GenericClass
          E extends Object declared in class GenericClass

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.util.HashMap;
      import java.util.List;

      class GenericClass<S extends Object, E extends Object>
      {
          public <E2 extends Object> HashMap<S, ? extends List<E2>> properties( String key )
          {
              return null;
          }

          public static void main(String [] args)
          {
              GenericClass<String, Long> gt = new GenericClass<String, Long>();
              HashMap<String, List<String>> map = gt.<String, List<String>>properties( "John" );
          }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      One workaround is to use raw class HashMap:

           HashMap map = gt.properties( "John" );

      but that approach triggers a compilation warning (we're in the process of getting rid of them). Furthermore, with raw class, downstream use/access to the map triggers more warnings.


            mcimadamore Maurizio Cimadamore
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: