groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pa...@apache.org
Subject groovy git commit: GROOVY-8430: @Field doesn't support final fields (add better error message for final field with @Option)
Date Thu, 04 Jan 2018 03:34:14 GMT
Repository: groovy
Updated Branches:
  refs/heads/master 0b7088778 -> 45233b673


GROOVY-8430: @Field doesn't support final fields (add better error message for final field
with @Option)


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

Branch: refs/heads/master
Commit: 45233b673076405b1d73cccec99675559a6a8639
Parents: 0b70887
Author: paulk <paulk@asert.com.au>
Authored: Thu Jan 4 13:34:03 2018 +1000
Committer: paulk <paulk@asert.com.au>
Committed: Thu Jan 4 13:34:03 2018 +1000

----------------------------------------------------------------------
 .../codehaus/groovy/transform/FieldASTTransformation.java | 10 ++++++++--
 .../codehaus/groovy/transform/FieldTransformTest.groovy   |  8 ++++++++
 2 files changed, 16 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/45233b67/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 4ccacd1..bb52c6f 100644
--- a/src/main/java/org/codehaus/groovy/transform/FieldASTTransformation.java
+++ b/src/main/java/org/codehaus/groovy/transform/FieldASTTransformation.java
@@ -18,6 +18,7 @@
  */
 package org.codehaus.groovy.transform;
 
+import groovy.cli.Option;
 import groovy.lang.Lazy;
 import groovy.transform.Field;
 import org.codehaus.groovy.GroovyBugError;
@@ -73,6 +74,7 @@ public class FieldASTTransformation extends ClassCodeExpressionTransformer
imple
     private static final ClassNode LAZY_TYPE = make(Lazy.class);
     private static final String MY_TYPE_NAME = "@" + MY_TYPE.getNameWithoutPackage();
     private static final ClassNode ASTTRANSFORMCLASS_TYPE = make(GroovyASTTransformationClass.class);
+    private static final ClassNode OPTION_TYPE = make(Option.class);
     private SourceUnit sourceUnit;
     private DeclarationExpression candidate;
     private boolean insideScriptBody;
@@ -110,8 +112,12 @@ public class FieldASTTransformation extends ClassCodeExpressionTransformer
imple
             fieldNode = new FieldNode(variableName, ve.getModifiers(), ve.getType(), null,
de.getRightExpression());
             fieldNode.setSourcePosition(de);
             cNode.addField(fieldNode);
-            // allow friendly setting of field via a setter unless final
-            if (!fieldNode.isFinal()) {
+            // provide setter for CLI Builder purposes unless final
+            if (fieldNode.isFinal()) {
+                if (!de.getAnnotations(OPTION_TYPE).isEmpty()) {
+                    addError("Can't have a final field also annotated with @" + OPTION_TYPE.getNameWithoutPackage(),
de);
+                }
+            } else {
                 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)))

http://git-wip-us.apache.org/repos/asf/groovy/blob/45233b67/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 34cace8..a7a07e3 100644
--- a/src/test/org/codehaus/groovy/transform/FieldTransformTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/FieldTransformTest.groovy
@@ -275,4 +275,12 @@ class FieldTransformTest extends CompilableTestSupport {
             assert setters.intersect(['setBar', 'setFoo', 'setBaz']) == ['setBaz']
         '''
     }
+
+    void testFieldTransformWithFinalFieldAndOption() {
+        // GROOVY-8430 in conjunction with @Option
+        shouldNotCompile '''
+            import groovy.cli.OptionField
+            @OptionField final String first
+        '''
+    }
 }


Mime
View raw message