Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-8638

CompileStatic fails with Guava Multimap#asMap

    XMLWordPrintableJSON

Details

    Description

      When using CompileStatic and groovy 2.5.0, type checking appears wrong with Guava's Multimap.asMap method.  Return type from that method is Map<K, Collection<V>>, but attempting to assign to a variable of that type results in the error message:

      Incompatible generic argument types. Cannot assign java.util.Map <K, V> to: java.util.Map <K, Collection>
      

      This previously worked fine with Groovy 2.4.15. Adding an explicit cast to the asMap() call appears to work around the error.

      Here is a small script demonstrating the problem:

      @Grab('com.google.guava:guava:25.1-jre')
      
      import com.google.common.collect.ArrayListMultimap
      import com.google.common.collect.ListMultimap
      import groovy.transform.CompileStatic
      
      @CompileStatic
      class Test {
      
          void test() {
              ListMultimap<String, Integer> mmap = ArrayListMultimap.create()
              // This extra cast fixes all the errors...
              //ListMultimap<String, Integer> mmap = ArrayListMultimap.create() as ListMultimap<String, Integer>
              Map<String, Collection<Integer>> map = mmap.asMap()
              
              Set<Map.Entry<String, Collection<Integer>>> entrySet = map.entrySet()
              Iterator<Map.Entry<String, Collection<Integer>>> iter = entrySet.iterator()
              
              while (iter.hasNext()) {
                  Map.Entry<String, Collection<Integer>> group = iter.next()
                  Collection<Integer> values = group.value
              }
          }
          
          static void main(String... args) {
              Test test = new Test()
              test.test()
          }
          
      }
      

      And the full error output:

      5 compilation errors:
      
      [Static type checking] - Incompatible generic argument types. Cannot assign java.util.Map <java.lang.String, java.lang.Integer> to: java.util.Map <String, Collection>
       at line: 14, column: 48
      
      [Static type checking] - Incompatible generic argument types. Cannot assign java.util.Set <java.util.Map$Entry> to: java.util.Set <Map.Entry>
       at line: 16, column: 64
      
      [Static type checking] - Incompatible generic argument types. Cannot assign java.util.Iterator <java.util.Map$Entry> to: java.util.Iterator <Map.Entry>
       at line: 17, column: 65
      
      [Static type checking] - Incompatible generic argument types. Cannot assign java.util.Map$Entry <java.lang.String, java.lang.Integer> to: java.util.Map$Entry <String, Collection>
       at line: 20, column: 60
      
      [Static type checking] - Cannot assign value of type java.lang.Integer to variable of type java.util.Collection <Integer>
       at line: 21, column: 42
      

      Attachments

        Issue Links

          Activity

            People

              emilles Eric Milles
              zman0900 Dan Ziemba
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: