You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2020/06/23 11:27:29 UTC

[groovy] branch GROOVY_2_5_X updated (4ffcd57 -> 24eb540)

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

paulk pushed a change to branch GROOVY_2_5_X
in repository https://gitbox.apache.org/repos/asf/groovy.git.


    from 4ffcd57  GROOVY-9603: don't apply map literal as constructor shorthand to Object (closes #1288)
     new 4d9b9c7  GROOVY-9554: Script: before adding to binding, check for property setter (closes #1280)
     new 24eb540  GROOVY-9554: Script: before adding to binding, check for property setter (tweak doco)

The 2 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.


Summary of changes:
 src/main/groovy/groovy/lang/Script.java            | 27 ++++++++++++++++++----
 src/spec/doc/core-program-structure.adoc           |  8 +++++--
 .../groovy/transform/FieldTransformTest.groovy     | 16 +++++++++++++
 3 files changed, 45 insertions(+), 6 deletions(-)


[groovy] 02/02: GROOVY-9554: Script: before adding to binding, check for property setter (tweak doco)

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

paulk pushed a commit to branch GROOVY_2_5_X
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 24eb540e16820f298caec16f73b283728c4c77d4
Author: Paul King <pa...@asert.com.au>
AuthorDate: Tue Jun 23 21:25:47 2020 +1000

    GROOVY-9554: Script: before adding to binding, check for property setter (tweak doco)
---
 src/spec/doc/core-program-structure.adoc | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/spec/doc/core-program-structure.adoc b/src/spec/doc/core-program-structure.adoc
index 1f5d4a8..7fa0e2b 100644
--- a/src/spec/doc/core-program-structure.adoc
+++ b/src/spec/doc/core-program-structure.adoc
@@ -253,7 +253,7 @@ will behave the same as:
 include::{projectdir}/src/spec/test/ScriptsAndClassesSpecTest.groovy[tags=script_with_untyped_variables,indent=0]
 ----
 
-However there is a semantic difference between the two:
+However, there is a semantic difference between the two:
 
 * if the variable is declared as in the first example, it is a _local variable_. It will be declared in the `run`
 method that the compiler will generate and will *not* be visible outside of the script main body. In particular, such
@@ -263,5 +263,9 @@ visible from the methods, and is especially important if you use a script to int
 share data between the script and the application. Readers might refer to the 
 <<guide-integrating.adoc#_integrating_groovy_in_a_java_application,integration guide>> for more information.
 
-TIP: If you want a variable to become a field of the class without going into the `Binding`, you can use the
+TIP: Another approach to making a variable visible to all methods, is to use the
 <<core-metaprogramming.adoc#xform-Field,@Field annotation>>.
+A variable annotated this way will become a field of the generated script class and,
+as for local variables, access won't involve the script `Binding`.
+While not recommended, if you have a local variable or script field with the same name as a binding variable,
+you can use `binding.varName` to access the binding variable.


[groovy] 01/02: GROOVY-9554: Script: before adding to binding, check for property setter (closes #1280)

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

paulk pushed a commit to branch GROOVY_2_5_X
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 4d9b9c7242433d39930f863b4cb39d4663350354
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 (closes #1280)
---
 src/main/groovy/groovy/lang/Script.java            | 27 ++++++++++++++++++----
 .../groovy/transform/FieldTransformTest.groovy     | 16 +++++++++++++
 2 files changed, 39 insertions(+), 4 deletions(-)

diff --git a/src/main/groovy/groovy/lang/Script.java b/src/main/groovy/groovy/lang/Script.java
index b9bda4b..de29c9b 100644
--- a/src/main/groovy/groovy/lang/Script.java
+++ b/src/main/groovy/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 '''