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 2016/09/20 06:53:39 UTC
groovy git commit: GROOVY-6831: Access to static fields/properties
from within nested classes is inconsistent (closes #425)
Repository: groovy
Updated Branches:
refs/heads/master 5b06149b9 -> afa59389f
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/afa59389
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/afa59389
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/afa59389
Branch: refs/heads/master
Commit: afa59389f3f362ed8bf37b80c1793b0f864a8721
Parents: 5b06149
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:52:47 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/afa59389/src/main/groovy/lang/MetaClassImpl.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/lang/MetaClassImpl.java b/src/main/groovy/lang/MetaClassImpl.java
index 41a91d9..ac9d767 100644
--- a/src/main/groovy/lang/MetaClassImpl.java
+++ b/src/main/groovy/lang/MetaClassImpl.java
@@ -2751,7 +2751,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/afa59389/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/afa59389/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 '''