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

Map-based constructor generated by @Immutable works only with HashMap

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.4.13
    • 2.5.0-beta-3
    • None
    • None

    Description

      If you call the map-based constructor of an @Immutable class with a non-HashMap, it will try to set the properties via setters, which causes a ReadOnlyPropertyException. Somehow it works with HashMap or subclasses like LinkedHashMap. Why?

      import groovy.transform.Immutable
      
      @Immutable
      class Point {
      	int x
      	int y
      }
      
      def coordinates = [x: 1, y: 2]
      assert coordinates instanceof LinkedHashMap
      
      def p1 = new Point(coordinates)
      assert p1.x == 1
      
      def p2 = new Point(new HashMap(coordinates))
      assert p2.x == 1
      
      try {
      	new Point(new TreeMap(coordinates))
      } catch (e) {
      	assert e instanceof ReadOnlyPropertyException
      }
      
      try {
      	new Point(coordinates.asImmutable())
      } catch (e) {
      	assert e instanceof ReadOnlyPropertyException
      }
      

      Attachments

        Issue Links

          Activity

            People

              paulk Paul King
              anma Andreas Mayer
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: