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

Putting key on generic Map fails when using CompileStatic

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Open
    • Priority: Minor
    • Resolution: Unresolved
    • Affects Version/s: 3.0.0-alpha-2, 2.4.15, 2.5.0
    • Fix Version/s: None
    • Component/s: Static compilation
    • Labels:
    • Environment:
      Elementary OS Freya 0.3.2
      java version "1.8.0_121"
      Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
      Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

      Description

      I'm trying to use Groovy's short hand way to put a new key value to an encapsulated Map. But the compilation fails with:

       

      [Static type checking] - Cannot assign value of type java.lang.String to variable of type ?   
      

       

      Scenario to reproduce:

      import groovy.transform.CompileStatic
      
      @CompileStatic
      class MapHolder {
         private Map<?, ?> map = [:]
      
         Map<?, ?> getMap() {
            return map
         }
      }
      
      @CompileStatic
      static setMapValue() {
         MapHolder mapHolder = new MapHolder()
         mapHolder.map.key = 'value'
      }
      
      setMapValue()
      

      Using the put method works just fine:

      import groovy.transform.CompileStatic
      
      @CompileStatic
      class MapHolder {
         private Map<?, ?> map = [:]
      
         Map<?, ?> getMap() {
            return map
         }
      }
      
      @CompileStatic
      static setMapValue() {
         MapHolder mapHolder = new MapHolder()
         mapHolder.put('key', 'value')
      }
      
      setMapValue()
      

       

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              cleydsonjr Cleydson José de Figueiredo Júnior
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated: