You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2026/02/25 16:29:21 UTC
(groovy) branch master updated: GROOVY-11862: add typecast to static field value
This is an automated email from the ASF dual-hosted git repository.
emilles 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 96cca630de GROOVY-11862: add typecast to static field value
96cca630de is described below
commit 96cca630de57467d7af13e4b069ba4382062c40c
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Wed Feb 25 10:18:13 2026 -0600
GROOVY-11862: add typecast to static field value
---
.../transform/trait/TraitASTTransformation.java | 49 +++++++++----------
.../groovy/transform/traitx/Groovy_7217.groovy | 57 ----------------------
.../traitx/TraitASTTransformationTest.groovy | 32 ++++++++++++
3 files changed, 56 insertions(+), 82 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/transform/trait/TraitASTTransformation.java b/src/main/java/org/codehaus/groovy/transform/trait/TraitASTTransformation.java
index 60829e083a..0edc14659e 100644
--- a/src/main/java/org/codehaus/groovy/transform/trait/TraitASTTransformation.java
+++ b/src/main/java/org/codehaus/groovy/transform/trait/TraitASTTransformation.java
@@ -507,13 +507,14 @@ public class TraitASTTransformation extends AbstractASTTransformation implements
return;
}
- Expression initialExpression = field.getInitialExpression();
+ Expression initExpression = field.getInitialExpression();
MethodNode selectedMethod = field.isStatic() ? staticInitializer : initializer;
ClassNode target = field.isStatic() && staticFieldHelper != null ? staticFieldHelper : fieldHelper;
- if (initialExpression != null) {
+ if (initExpression != null) {
VariableExpression thisObject = varX(selectedMethod.getParameters()[0]);
- ExpressionStatement initCode = new ExpressionStatement(initialExpression);
- processBody(thisObject, initCode, trait, helper, fieldHelper, knownFields);
+ ExpressionStatement initValue = new ExpressionStatement(initExpression);
+ processBody(thisObject, initValue, trait, helper, fieldHelper, knownFields);
+ initExpression = initValue.getExpression(); // extract transformed expression
if (field.isFinal()) {
String baseName = field.isStatic() ? Traits.STATIC_INIT_METHOD : Traits.INIT_METHOD;
MethodNode fieldInitializer = new MethodNode(
@@ -522,37 +523,35 @@ public class TraitASTTransformation extends AbstractASTTransformation implements
field.getOriginType(),
new Parameter[]{createSelfParameter(trait, field.isStatic())},
ClassNode.EMPTY_ARRAY,
- returnS(initCode.getExpression())
+ returnS(initExpression)
);
helper.addMethod(fieldInitializer);
} else {
- BlockStatement code = (BlockStatement) selectedMethod.getCode();
- MethodCallExpression mce;
+ initExpression = castX(field.getOriginType(), initExpression); // GROOVY-7217, GROOVY-11862
+
+ Expression receiver;
+ String methodTarget;
+ Expression callArgs;
if (field.isStatic()) {
- if (staticFieldHelper != null) {
- target = staticFieldHelper;
- }
- mce = callX(
- classX(InvokerHelper.class),
- "invokeStaticMethod",
- args(
- thisObject,
- constX(Traits.helperSetterName(field)),
- initCode.getExpression()
- )
+ receiver = classX(InvokerHelper.class);
+ methodTarget = "invokeStaticMethod";
+ callArgs = args(
+ thisObject,
+ constX(Traits.helperSetterName(field)),
+ initExpression
);
} else {
- mce = callX(
- castX(createReceiverType(field.isStatic(), fieldHelper), thisObject),
- Traits.helperSetterName(field),
- castX(field.getOriginType(), initCode.getExpression())
- );
+ receiver = castX(fieldHelper, thisObject);
+ methodTarget = Traits.helperSetterName(field);
+ callArgs = initExpression;
}
+ MethodCallExpression mce = callX(receiver, methodTarget, callArgs);
mce.setImplicitThis(false);
- mce.setSourcePosition(initialExpression);
- code.addStatement(stmt(mce));
+ mce.setSourcePosition(field.getInitialExpression());
+ ((BlockStatement)selectedMethod.getCode()).addStatement(stmt(mce));
}
}
+
// define setter/getter helper methods (setter added even for final fields for legacy compatibility)
addGeneratedMethod(target,
Traits.helperSetterName(field),
diff --git a/src/test/groovy/org/codehaus/groovy/transform/traitx/Groovy_7217.groovy b/src/test/groovy/org/codehaus/groovy/transform/traitx/Groovy_7217.groovy
deleted file mode 100644
index 7543ef24fc..0000000000
--- a/src/test/groovy/org/codehaus/groovy/transform/traitx/Groovy_7217.groovy
+++ /dev/null
@@ -1,57 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.codehaus.groovy.transform.traitx
-
-import org.junit.jupiter.api.Test
-
-import static groovy.test.GroovyAssert.assertScript
-
-final class Groovy_7217 {
-
- @Test
- void testNumberInitializationInTrait() {
- assertScript '''
- trait Version {
- Long version = 1
- }
- class HasVersion implements Version {
- }
-
- def v = new HasVersion()
- assert v.version == 1'''
- }
-
- @Test
- void testAnyInitializerInTrait() {
- assertScript '''
- class SomeA {}
- trait DummyInit {
- SomeA a = init()
- }
- class Dummy implements DummyInit {
- def init() {
- new SomeA()
- }
- }
-
- def d = new Dummy()
- assert d.a instanceof SomeA
- '''
- }
-}
diff --git a/src/test/groovy/org/codehaus/groovy/transform/traitx/TraitASTTransformationTest.groovy b/src/test/groovy/org/codehaus/groovy/transform/traitx/TraitASTTransformationTest.groovy
index e3151c340d..ab45703cf3 100644
--- a/src/test/groovy/org/codehaus/groovy/transform/traitx/TraitASTTransformationTest.groovy
+++ b/src/test/groovy/org/codehaus/groovy/transform/traitx/TraitASTTransformationTest.groovy
@@ -333,6 +333,38 @@ final class TraitASTTransformationTest {
'''
}
+ // GROOVY-7217
+ @ParameterizedTest
+ @ValueSource(strings=['byte','short','int','long','float','double','Byte','Short','Integer','Long','Float','Double','Number'])
+ void testTraitWithProperty6(String type) {
+ assertScript shell, """
+ trait T {
+ $type n = 42
+ }
+ class C implements T {
+ }
+
+ def c = new C()
+ assert c.getN() == 42
+ """
+ }
+
+ // GROOVY-11862
+ @ParameterizedTest
+ @ValueSource(strings=['byte','short','int','long','float','double','Byte','Short','Integer','Long','Float','Double','Number'])
+ void testTraitWithProperty7(String type) {
+ assertScript shell, """
+ trait T {
+ static $type n = 42
+ }
+ class C implements T {
+ }
+
+ def c = new C()
+ assert c.getN() == 42
+ """
+ }
+
@Test
void testUpdatePropertyFromSelf() {
assertScript shell, '''