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 2021/10/05 22:24:10 UTC

[groovy] 02/02: GROOVY-6586: BaseScript: check for setter before adding value to binding

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

commit 0c5bc8be4db0878ad7ecd4d0dc148ac412d81775
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Tue Oct 5 17:09:49 2021 -0500

    GROOVY-6586: BaseScript: check for setter before adding value to binding
---
 src/main/java/groovy/lang/Script.java                | 12 +++++++-----
 .../groovy/transform/BaseScriptTransformTest.groovy  | 20 ++++++++++++++++++++
 2 files changed, 27 insertions(+), 5 deletions(-)

diff --git a/src/main/java/groovy/lang/Script.java b/src/main/java/groovy/lang/Script.java
index 4a18889..98ced44 100644
--- a/src/main/java/groovy/lang/Script.java
+++ b/src/main/java/groovy/lang/Script.java
@@ -76,11 +76,13 @@ public abstract class Script extends GroovyObjectSupport {
 
     private boolean hasSetterMethodFor(String property) {
         String setterName = GeneralUtils.getSetterName(property);
-        for (Method method : getClass().getDeclaredMethods()) {
-            if (method.getParameterCount() == 1
-                    // TODO: Test modifiers or return type?
-                    && method.getName().equals(setterName)) {
-                return true;
+        for (Class<?> c = getClass(); !c.equals(Script.class); c = c.getSuperclass()) {
+            for (Method method : c.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/BaseScriptTransformTest.groovy b/src/test/org/codehaus/groovy/transform/BaseScriptTransformTest.groovy
index 36f4385..cb38850 100644
--- a/src/test/org/codehaus/groovy/transform/BaseScriptTransformTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/BaseScriptTransformTest.groovy
@@ -309,6 +309,26 @@ final class BaseScriptTransformTest extends gls.CompilableTestSupport {
         assert result
     }
 
+    // GROOVY-6586
+    void testBaseScriptVsBinding() {
+        assertScript '''
+            abstract class Custom extends Script {
+                private int _something = 1
+                int getSomething() { _something }
+                void setSomething(int i) { _something = i }
+            }
+
+            @groovy.transform.BaseScript Custom self
+
+            assert binding.variables.size() == 0
+            assert something == 1
+            assert binding.variables.size() == 0
+            something = 2
+            assert binding.variables.size() == 0
+            assert something == 2
+        '''
+    }
+
     void testShouldNotAllowClassMemberIfUsedOnADeclaration() {
         shouldNotCompile '''import groovy.transform.BaseScript