This is an automated email from the ASF dual-hosted git repository. paulk pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/master by this push: new 7e57187b94 GROOVY-11329: Class using trait and @Immutable and allProperties gives BUG! exception in phase 'canonicalization' NPE 7e57187b94 is described below commit 7e57187b942b594eb38be1de55f95837227897c3 Author: Paul King <pa...@asert.com.au> AuthorDate: Tue Mar 5 13:09:18 2024 +1000 GROOVY-11329: Class using trait and @Immutable and allProperties gives BUG! exception in phase 'canonicalization' NPE --- .../transform/options/ImmutablePropertyHandler.java | 5 ++++- .../groovy/transform/ImmutableTransformTest.groovy | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/main/java/groovy/transform/options/ImmutablePropertyHandler.java b/src/main/java/groovy/transform/options/ImmutablePropertyHandler.java index c8bcabe59f..935eaf6f4a 100644 --- a/src/main/java/groovy/transform/options/ImmutablePropertyHandler.java +++ b/src/main/java/groovy/transform/options/ImmutablePropertyHandler.java @@ -234,7 +234,10 @@ public class ImmutablePropertyHandler extends PropertyHandler { get.setImplicitThis(false); MethodCallExpression containsKey = callX(varX(map), "containsKey", nameArg); containsKey.setImplicitThis(false); - fNode.getDeclaringClass().getField(fNode.getName()).setInitialValueExpression(null); // to avoid default initialization + // avoid NPE if no declaring class - can be the case for traits + if (fNode.getDeclaringClass() != null) { + fNode.getDeclaringClass().getField(fNode.getName()).setInitialValueExpression(null); // to avoid default initialization + } return ifElseS(containsKey, assignStmt, assignInit); } diff --git a/src/test/org/codehaus/groovy/transform/ImmutableTransformTest.groovy b/src/test/org/codehaus/groovy/transform/ImmutableTransformTest.groovy index 32cb6929d5..703c1b8eeb 100644 --- a/src/test/org/codehaus/groovy/transform/ImmutableTransformTest.groovy +++ b/src/test/org/codehaus/groovy/transform/ImmutableTransformTest.groovy @@ -1074,4 +1074,23 @@ final class ImmutableTransformTest { assert obj['data'][2L] == 'bar' ''' } + + // GROOVY-11329 + @Test + void testAllPropertiesAndTrait() { + assertScript shell, ''' + import groovy.transform.Immutable + + trait T { + String foo + } + + @Immutable(allProperties=true) + class C implements T { + String bar + } + + assert new C('bar', 'foo').toString() == 'C(bar, foo)' + ''' + } }