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

Runtimer error when types down match with Map and Map.Entry when using @CompileStatic

    Details

      Description

      When running the follow code a runtime error occurs:

      @CompileStatic
      class Scratch
      {
      
          @CompileStatic
          public static void main(String[] args)
          {
              def Map<String, Set<String>> map = new HashMap<>()
              def set = new HashSet<String>()
              set.add("foo")
              map.put("bar", set)
      
              map.each {
                  println(it.getKey()) // Groovyc: [Static type checking] - Cannot find matching method java.lang.Object#getKey().
                  def List<String> list = it.getValue() // Groovyc: [Static type checking] - Cannot find matching method java.lang.Object#getKey().
              }
      
              // compiles fine and created runtime error
              // Cannot cast object '[foo]' with class 'java.util.HashSet' to class 'java.util.List' due to: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: java.util.List(java.lang.String)
              map.each { Map.Entry<String, List<String>> entry ->
                  println(entry.getKey())
                  def List<String> list = entry.getValue()
              }
          }
      }
      

      Reason is that the type parameters for Map<String, Set<String>> and Map.Entry<String, List<String>> don't match, but the compiler doesn't reject it, although @CompileStatic is defined

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                melix Cédric Champeau
                Reporter:
                oliver_plow Oliver Plow
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: