groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pa...@apache.org
Subject groovy git commit: fix for: GROOVY-8430: @Field doesn't support final fields (closes #652)
Date Thu, 04 Jan 2018 00:36:20 GMT
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_5_X 2f2efcf1e -> 976d6f567


fix for: GROOVY-8430: @Field doesn't support final fields (closes #652)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/976d6f56
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/976d6f56
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/976d6f56

Branch: refs/heads/GROOVY_2_5_X
Commit: 976d6f5671a50cfb9758c9fc0c12fc6a19244686
Parents: 2f2efcf
Author: paulk <paulk@asert.com.au>
Authored: Wed Jan 3 08:55:35 2018 +1000
Committer: paulk <paulk@asert.com.au>
Committed: Thu Jan 4 10:36:07 2018 +1000

----------------------------------------------------------------------
 src/main/groovy/groovy/transform/Field.java         |  1 -
 .../groovy/transform/FieldASTTransformation.java    | 11 +++++++----
 .../groovy/transform/FieldTransformTest.groovy      | 16 ++++++++++++++++
 3 files changed, 23 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/976d6f56/src/main/groovy/groovy/transform/Field.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/groovy/transform/Field.java b/src/main/groovy/groovy/transform/Field.java
index bcc64e8..1dc628c 100644
--- a/src/main/groovy/groovy/transform/Field.java
+++ b/src/main/groovy/groovy/transform/Field.java
@@ -44,7 +44,6 @@ import java.lang.annotation.Target;
  * <code>awe</code> becomes a private List field in the script class and is
  * visible within the <code>awesum</code> method.
  *
- * @author Paul King
  * @since 1.8.0
  */
 @java.lang.annotation.Documented

http://git-wip-us.apache.org/repos/asf/groovy/blob/976d6f56/src/main/java/org/codehaus/groovy/transform/FieldASTTransformation.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/transform/FieldASTTransformation.java b/src/main/java/org/codehaus/groovy/transform/FieldASTTransformation.java
index 8e6b58e..4ccacd1 100644
--- a/src/main/java/org/codehaus/groovy/transform/FieldASTTransformation.java
+++ b/src/main/java/org/codehaus/groovy/transform/FieldASTTransformation.java
@@ -110,10 +110,13 @@ public class FieldASTTransformation extends ClassCodeExpressionTransformer
imple
             fieldNode = new FieldNode(variableName, ve.getModifiers(), ve.getType(), null,
de.getRightExpression());
             fieldNode.setSourcePosition(de);
             cNode.addField(fieldNode);
-            String setterName = "set" + MetaClassHelper.capitalize(variableName);
-            cNode.addMethod(setterName, ACC_PUBLIC | ACC_SYNTHETIC, ClassHelper.VOID_TYPE,
params(param(ve.getType(), variableName)), ClassNode.EMPTY_ARRAY, block(
-                    stmt(assignX(propX(varX("this"), variableName), varX(variableName)))
-            ));
+            // allow friendly setting of field via a setter unless final
+            if (!fieldNode.isFinal()) {
+                String setterName = "set" + MetaClassHelper.capitalize(variableName);
+                cNode.addMethod(setterName, ACC_PUBLIC | ACC_SYNTHETIC, ClassHelper.VOID_TYPE,
params(param(ve.getType(), variableName)), ClassNode.EMPTY_ARRAY, block(
+                        stmt(assignX(propX(varX("this"), variableName), varX(variableName)))
+                ));
+            }
 
             // GROOVY-4833 : annotations that are not Groovy transforms should be transferred
to the generated field
             // GROOVY-6112 : also copy acceptable Groovy transforms

http://git-wip-us.apache.org/repos/asf/groovy/blob/976d6f56/src/test/org/codehaus/groovy/transform/FieldTransformTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/transform/FieldTransformTest.groovy b/src/test/org/codehaus/groovy/transform/FieldTransformTest.groovy
index 6575720..34cace8 100644
--- a/src/test/org/codehaus/groovy/transform/FieldTransformTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/FieldTransformTest.groovy
@@ -259,4 +259,20 @@ class FieldTransformTest extends CompilableTestSupport {
             assert logger.toString() == 'abcdefgh'
         '''
     }
+
+    void testFieldTransformWithFinalField() {
+        // GROOVY-8430
+        assertScript '''
+            import groovy.transform.Field
+
+            @Field final foo = 14
+            @Field final bar = foo * 2
+            @Field baz = foo + bar
+
+            assert foo + bar == 42
+            assert baz == 42
+            def setters = getClass().methods.findAll{ it.name.startsWith('set') }.name
+            assert setters.intersect(['setBar', 'setFoo', 'setBaz']) == ['setBaz']
+        '''
+    }
 }


Mime
View raw message