Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Fixed
-
2.5.0, 2.5.14, 3.0.8
-
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
- relates to
-
GROOVY-8416 Map-based constructor generated by @Immutable works only with HashMap
-
- Closed
-