Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_4_X 90549729a -> ed85b205c


GROOVY-6831: Access to static fields/properties from within nested classes is 
inconsistent (closes #425)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/ed85b205
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/ed85b205
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/ed85b205

Branch: refs/heads/GROOVY_2_4_X
Commit: ed85b205cb269fa74837c5469433de60c302d9ba
Parents: 9054972
Author: paulk <pa...@asert.com.au>
Authored: Mon Sep 19 12:53:15 2016 +1000
Committer: paulk <pa...@asert.com.au>
Committed: Tue Sep 20 16:54:30 2016 +1000

----------------------------------------------------------------------
 src/main/groovy/lang/MetaClassImpl.java         |  5 ++-
 .../classgen/InnerClassCompletionVisitor.java   | 40 +++++++++++++++++++-
 src/test/gls/innerClass/InnerClassTest.groovy   | 23 +++++++++++
 3 files changed, 65 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/ed85b205/src/main/groovy/lang/MetaClassImpl.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/lang/MetaClassImpl.java 
b/src/main/groovy/lang/MetaClassImpl.java
index a311a25..f0f63cb 100644
--- a/src/main/groovy/lang/MetaClassImpl.java
+++ b/src/main/groovy/lang/MetaClassImpl.java
@@ -2744,7 +2744,10 @@ public class MetaClassImpl implements MetaClass, 
MutableMetaClass {
             throw new ReadOnlyPropertyException(name, theClass);
         }
 
-        invokeMissingProperty(object, name, newValue, false);
+        if ((isStatic || object instanceof Class) && !"metaClass".equals(name))
+            invokeStaticMissingProperty(object, name, newValue, false);
+        else
+            invokeMissingProperty(object, name, newValue, false);
     }
 
     private static boolean isPrivateOrPkgPrivate(int mod) {

http://git-wip-us.apache.org/repos/asf/groovy/blob/ed85b205/src/main/org/codehaus/groovy/classgen/InnerClassCompletionVisitor.java
----------------------------------------------------------------------
diff --git 
a/src/main/org/codehaus/groovy/classgen/InnerClassCompletionVisitor.java 
b/src/main/org/codehaus/groovy/classgen/InnerClassCompletionVisitor.java
index de81bdd..b03d6fd 100644
--- a/src/main/org/codehaus/groovy/classgen/InnerClassCompletionVisitor.java
+++ b/src/main/org/codehaus/groovy/classgen/InnerClassCompletionVisitor.java
@@ -253,7 +253,7 @@ public class InnerClassCompletionVisitor extends 
InnerClassVisitorHelper impleme
 
         block = new BlockStatement();
         if (isStatic) {
-            setPropertySetterDispatcher(block, new 
ClassExpression(node.getOuterClass()), parameters);
+            setPropertySetterDispatcher(block, new 
ClassExpression(outerClass), parameters);
         } else {
             block.addStatement(
                     new BytecodeSequence(new BytecodeInstruction() {
@@ -269,6 +269,24 @@ public class InnerClassCompletionVisitor extends 
InnerClassVisitorHelper impleme
         }
         method.setCode(block);
 
+        // add static property missing setter dispatcher
+        methodName = "$static_propertyMissing";
+        if (isStatic)
+            addCompilationErrorOnCustomMethodNode(node, methodName, 
parameters);
+
+        method = node.addSyntheticMethod(
+                methodName,
+                Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC,
+                ClassHelper.VOID_TYPE,
+                parameters,
+                ClassNode.EMPTY_ARRAY,
+                null
+        );
+
+        block = new BlockStatement();
+        setPropertySetterDispatcher(block, new ClassExpression(outerClass), 
parameters);
+        method.setCode(block);
+
         // add property getter dispatcher
         parameters = new Parameter[]{
                 new Parameter(ClassHelper.STRING_TYPE, "name")
@@ -289,7 +307,7 @@ public class InnerClassCompletionVisitor extends 
InnerClassVisitorHelper impleme
 
         block = new BlockStatement();
         if (isStatic) {
-            setPropertyGetterDispatcher(block, new 
ClassExpression(node.getOuterClass()), parameters);
+            setPropertyGetterDispatcher(block, new 
ClassExpression(outerClass), parameters);
         } else {
             block.addStatement(
                     new BytecodeSequence(new BytecodeInstruction() {
@@ -303,6 +321,24 @@ public class InnerClassCompletionVisitor extends 
InnerClassVisitorHelper impleme
             );
         }
         method.setCode(block);
+
+        // add static property missing getter dispatcher
+        methodName = "$static_propertyMissing";
+        if (isStatic)
+            addCompilationErrorOnCustomMethodNode(node, methodName, 
parameters);
+
+        method = node.addSyntheticMethod(
+                methodName,
+                Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC,
+                ClassHelper.OBJECT_TYPE,
+                parameters,
+                ClassNode.EMPTY_ARRAY,
+                null
+        );
+
+        block = new BlockStatement();
+        setPropertyGetterDispatcher(block, new ClassExpression(outerClass), 
parameters);
+        method.setCode(block);
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/groovy/blob/ed85b205/src/test/gls/innerClass/InnerClassTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/gls/innerClass/InnerClassTest.groovy 
b/src/test/gls/innerClass/InnerClassTest.groovy
index cd27406..811f148 100644
--- a/src/test/gls/innerClass/InnerClassTest.groovy
+++ b/src/test/gls/innerClass/InnerClassTest.groovy
@@ -675,6 +675,29 @@ import org.codehaus.groovy.classgen.Verifier
         '''
     }
 
+    void testNestedPropertyHandling() {
+        // GROOVY-6831
+        assertScript '''
+            class Outer {
+                private static List items = []
+                void add() { items.add('Outer') }
+                static class Nested {
+                    void add() { items.add('Nested') }
+                    static class NestedNested {
+                        void add() { items.add('NestedNested') }
+                        void set() { items = ['Overridden'] }
+                    }
+                }
+            }
+            new Outer().add()
+            new Outer.Nested().add()
+            new Outer.Nested.NestedNested().add()
+            assert Outer.items == ["Outer", "Nested", "NestedNested"]
+            new Outer.Nested.NestedNested().set()
+            assert Outer.items == ["Overridden"]
+        '''
+    }
+
     void testInnerClassOfInterfaceIsStatic() {
         //GROOVY-7312
         assertScript '''

Reply via email to