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

Tuple constructor fails for an @Immutable class having a single Map property

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.5.0, 2.5.14, 3.0.8
    • 4.0.7
    • None
    • None
    • N/A

    Description

      For an @Immutable class having a single Map property, trying to use the generated tuple constructor generally results in an exception being thrown from ImmutableASTTransformation.checkPropNames(). This did not happen in Groovy 2.4.

      For example, given a class like this:

      @Immutable
      class LookupTable {
          Map<Long, String> data
      }
      

      And this usage of that class:

      Map<Long, String> data = [(1L): 'foo', (2L): 'bar']
      new LookupTable(data)
      

      The constructor invocation throws:

      java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String
      	at org.codehaus.groovy.transform.ImmutableASTTransformation.checkPropNames(ImmutableASTTransformation.java:423)
      

      The documentation for @Immutable does mention there are limitations when there is a single property of type Map, but the wording there doesn't sound like it's talking about the tuple constructor:

      Groovy's normal map-style naming conventions will not be available if the first property has type LinkedHashMap or if there is a single Map, AbstractMap or HashMap property.

      In fact that wording makes it sound as if it's the map-based constructor that won't be available, when in fact using the map-based constructor does work in this case (and thus seems to be a viable workaround).

      See https://github.com/douglyuckling/groovy-2.5-immutable-with-map-bug for a minimal reproduction of the problem.

      Attachments

        Issue Links

          Activity

            People

              emilles Eric Milles
              douglyuckling Douglas Paul
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: