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/12/07 03:57:38 UTC

[groovy] branch GROOVY_2_4_X updated: GROOVY-7549, GROOVY-9847: getOuterClass, getOuterClasses, getOuterField must check redirect (port to 2_4_X)

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

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


The following commit(s) were added to refs/heads/GROOVY_2_4_X by this push:
     new a977e90  GROOVY-7549, GROOVY-9847: getOuterClass, getOuterClasses, getOuterField must check redirect (port to 2_4_X)
a977e90 is described below

commit a977e90013fbb02933d853f85728cf154910f964
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Fri Dec 4 12:52:21 2020 -0600

    GROOVY-7549, GROOVY-9847: getOuterClass, getOuterClasses, getOuterField must check redirect (port to 2_4_X)
---
 src/main/org/codehaus/groovy/ast/ClassNode.java    | 14 +++++----
 .../transform/stc/TypeInferenceSTCTest.groovy      | 33 ++++++++++++++++++++++
 2 files changed, 41 insertions(+), 6 deletions(-)

diff --git a/src/main/org/codehaus/groovy/ast/ClassNode.java b/src/main/org/codehaus/groovy/ast/ClassNode.java
index 9838d58..fd93101 100644
--- a/src/main/org/codehaus/groovy/ast/ClassNode.java
+++ b/src/main/org/codehaus/groovy/ast/ClassNode.java
@@ -768,17 +768,19 @@ public class ClassNode extends AnnotatedNode implements Opcodes {
     }
 
     /**
-     * @return the field node on the outer class or null if this is not an
-     *         inner class
+     * @return outer class field or {@code null} if not found or this is not an inner class
      */
-    public FieldNode getOuterField(String name) {
+    public FieldNode getOuterField(final String name) {
+        if (redirect != null) {
+            return redirect.getOuterField(name);
+        }
         return null;
     }
 
-    /**
-     * Helper method to avoid casting to inner class
-     */
     public ClassNode getOuterClass() {
+        if (redirect != null) {
+            return redirect.getOuterClass();
+        }
         return null;
     }
 
diff --git a/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy b/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy
index afe34b8..2eea117 100644
--- a/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy
+++ b/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy
@@ -825,6 +825,39 @@ Thing.run()
         }
     }
 
+    // GROOVY-9847
+    void testShouldKeepInferredTypeWhenPrivateInnerClass() {
+        assertScript '''
+            class Test {
+                @groovy.transform.ToString(excludes='children')
+                private static class TreeAttr {
+                    String name
+                    Integer val = 0
+                    List<TreeAttr> children = []
+                }
+
+                static main(args) {
+                    new Test().test(1)
+                }
+
+                void test(Integer count) {
+                    TreeAttr root = new TreeAttr(name:'foo')
+                    List<TreeAttr> collector = root.children
+
+                    for (name in ['bar','baz']) { // tokens in a path
+                        def item = collector.find { it.name == name }
+                        if (!item) {
+                            item = new TreeAttr(name: name)
+                            collector.add(item)
+                        }
+                        collector = item.children
+                        if (count) item.val += count
+                    }
+                }
+            }
+        '''
+    }
+
     // GROOVY-6574
     void testShouldInferPrimitiveBoolean() {
         assertScript '''