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

HashMap subclass property access inconsistent

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 1.8.6
    • 5.0.0-alpha-1
    • groovy-runtime
    • None

    Description

      Creating a subclass of java.util.HashMap with properties defined results in inconsistent and confusing behavior.

      class MapSub extends HashMap {
        def foo
        
      }
      
      // Using dot notation to get the property results in a map key lookup.
      def m = new MapSub(foo: 'bar')
      println m.foo    // prints 'null'
      println m.getFoo() // prints 'bar'
      
      // Using same dot notation to set the property results in property setter.
      m.foo = 'baz'
      println m.foo // still prints 'null'
      println m.getFoo() // prints 'baz'
      

      This behavior results in subtle and hard to track bugs. I saw another Jira that stated that a design decision was made that Map dot property getters always resulted in a map key lookup. For consistency, I think either the same policy should be applied to Map property setters or the original design decision should be reconsidered to first check for property existence.

      Attachments

        Issue Links

          Activity

            People

              emilles Eric Milles
              mrfritz379 Michael Collado
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: