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 2019/11/30 16:55:29 UTC

[groovy] branch master updated: refactor dynamic variable handling

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


The following commit(s) were added to refs/heads/master by this push:
     new d2e8e00  refactor dynamic variable handling
d2e8e00 is described below

commit d2e8e00acd014477fb15f4642c286f6ea05fbbf9
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Sat Nov 30 10:55:24 2019 -0600

    refactor dynamic variable handling
---
 .../groovy/classgen/VariableScopeVisitor.java      | 37 +++++++++++-----------
 1 file changed, 18 insertions(+), 19 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java b/src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java
index 531e5f6..be90e1e 100644
--- a/src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java
+++ b/src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java
@@ -234,40 +234,39 @@ public class VariableScopeVisitor extends ClassCodeVisitorSupport {
     private Variable checkVariableNameForDeclaration(final String name, final Expression expression) {
         if ("super".equals(name) || "this".equals(name)) return null;
 
+        Variable variable = null;
         VariableScope scope = currentScope;
-        Variable var = new DynamicVariable(name, currentScope.isInStaticContext());
-        Variable orig = var;
-        // try to find a declaration of a variable
         boolean crossingStaticContext = false;
+        // try to find a declaration of a variable
         while (true) {
             crossingStaticContext = (crossingStaticContext || scope.isInStaticContext());
 
-            Variable var1 = scope.getDeclaredVariable(var.getName());
-            if (var1 != null) {
-                var = var1;
+            Variable var = scope.getDeclaredVariable(name);
+            if (var != null) {
+                variable = var;
                 break;
             }
 
-            var1 = scope.getReferencedLocalVariable(var.getName());
-            if (var1 != null) {
-                var = var1;
+            var = scope.getReferencedLocalVariable(name);
+            if (var != null) {
+                variable = var;
                 break;
             }
 
-            var1 = scope.getReferencedClassVariable(var.getName());
-            if (var1 != null) {
-                var = var1;
+            var = scope.getReferencedClassVariable(name);
+            if (var != null) {
+                variable = var;
                 break;
             }
 
             ClassNode classScope = scope.getClassScope();
             if (classScope != null) {
-                Variable member = findClassMember(classScope, var.getName());
+                Variable member = findClassMember(classScope, name);
                 if (member != null) {
                     boolean staticScope = (crossingStaticContext || isSpecialConstructorCall), staticMember = member.isInStaticContext();
                     // prevent a static context (e.g. a static method) from accessing a non-static variable (e.g. a non-static field)
                     if (!(staticScope && !staticMember)) {
-                        var = member;
+                        variable = member;
                     }
                 }
                 // GROOVY-5961
@@ -275,22 +274,22 @@ public class VariableScopeVisitor extends ClassCodeVisitorSupport {
             }
             scope = scope.getParent();
         }
-        if (var == orig && crossingStaticContext) {
-            var = new DynamicVariable(var.getName(), true);
+        if (variable == null) {
+            variable = new DynamicVariable(name, crossingStaticContext);
         }
 
         VariableScope end = scope;
         scope = currentScope;
         while (scope != end) {
             if (end.isClassScope() || (end.isReferencedClassVariable(name) && end.getDeclaredVariable(name) == null)) {
-                scope.putReferencedClassVariable(var);
+                scope.putReferencedClassVariable(variable);
             } else {
-                scope.putReferencedLocalVariable(var);
+                scope.putReferencedLocalVariable(variable);
             }
             scope = scope.getParent();
         }
 
-        return var;
+        return variable;
     }
 
     /**