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 2020/06/14 18:38:08 UTC

[groovy] branch GROOVY-9554 created (now e9684b8)

This is an automated email from the ASF dual-hosted git repository.

emilles pushed a change to branch GROOVY-9554
in repository https://gitbox.apache.org/repos/asf/groovy.git.


      at e9684b8  GROOVY-9554: Script: before adding to binding, check for property setter

This branch includes the following new commits:

     new e9684b8  GROOVY-9554: Script: before adding to binding, check for property setter

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[groovy] 01/01: GROOVY-9554: Script: before adding to binding, check for property setter

Posted by em...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

emilles pushed a commit to branch GROOVY-9554
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit e9684b8fe4fc7d9d3b4adf9190a7f2776b8cdb04
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Sun Jun 14 13:37:46 2020 -0500

    GROOVY-9554: Script: before adding to binding, check for property setter
---
 src/main/java/groovy/lang/Script.java              | 27 ++++++++++++++++++----
 .../groovy/transform/FieldTransformTest.groovy     | 16 +++++++++++++
 2 files changed, 39 insertions(+), 4 deletions(-)

diff --git a/src/main/java/groovy/lang/Script.java b/src/main/java/groovy/lang/Script.java
index b9bda4b..de29c9b 100644
--- a/src/main/java/groovy/lang/Script.java
+++ b/src/main/java/groovy/lang/Script.java
@@ -18,6 +18,7 @@
  */
 package groovy.lang;
 
+import org.apache.groovy.util.BeanUtils;
 import org.codehaus.groovy.ast.expr.ArgumentListExpression;
 import org.codehaus.groovy.control.CompilationFailedException;
 import org.codehaus.groovy.runtime.DefaultGroovyMethods;
@@ -25,6 +26,7 @@ import org.codehaus.groovy.runtime.InvokerHelper;
 
 import java.io.File;
 import java.io.IOException;
+import java.lang.reflect.Method;
 
 /**
  * This object represents a Groovy script
@@ -57,12 +59,29 @@ public abstract class Script extends GroovyObjectSupport {
     }
 
     public void setProperty(String property, Object newValue) {
-        if ("binding".equals(property))
+        if ("binding".equals(property)) {
             setBinding((Binding) newValue);
-        else if("metaClass".equals(property))
-            setMetaClass((MetaClass)newValue);
-        else
+        } else if ("metaClass".equals(property)) {
+            setMetaClass((MetaClass) newValue);
+        } else if (!binding.hasVariable(property)
+                // GROOVY-9554: @Field adds setter
+                && hasSetterMethodFor(property)) {
+            super.setProperty(property, newValue);
+        } else {
             binding.setVariable(property, newValue);
+        }
+    }
+
+    private boolean hasSetterMethodFor(String property) {
+        String setterName = "set" + BeanUtils.capitalize(property);
+        for (Method method : getClass().getDeclaredMethods()) {
+            if (method.getParameterCount() == 1
+                    // TODO: Test modifiers or return type?
+                    && method.getName().equals(setterName)) {
+                return true;
+            }
+        }
+        return false;
     }
 
     /**
diff --git a/src/test/org/codehaus/groovy/transform/FieldTransformTest.groovy b/src/test/org/codehaus/groovy/transform/FieldTransformTest.groovy
index 469ca33..9146c6f 100644
--- a/src/test/org/codehaus/groovy/transform/FieldTransformTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/FieldTransformTest.groovy
@@ -260,6 +260,22 @@ class FieldTransformTest extends CompilableTestSupport {
         '''
     }
 
+    void testClosureReferencesToField() {
+        // GROOVY-9554
+        assertScript '''
+            @groovy.transform.Field String abc
+            binding.variables.clear()
+            abc = 'abc'
+            assert !binding.hasVariable('abc')
+            ['D','E','F'].each {
+                abc += it
+            }
+            assert !binding.hasVariable('abc')
+            assert this.@abc == 'abcDEF'
+            assert abc == 'abcDEF'
+        '''
+    }
+
     void testFieldTransformWithFinalField() {
         // GROOVY-8430
         assertScript '''