From abff6c7bf8a953f8f41d4f5080004c436757cb7e Mon Sep 17 00:00:00 2001 From: Shil Sinha Date: Sun, 7 May 2017 14:47:19 -0400 Subject: [PATCH] GROOVY-8074: Statically compiled dot property accesses instance property instead of Map property --- .../asm/sc/StaticTypesCallSiteWriter.java | 26 +++++++++---------- ...raysAndCollectionsStaticCompileTest.groovy | 12 +++++++++ 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/main/org/codehaus/groovy/classgen/asm/sc/StaticTypesCallSiteWriter.java b/src/main/org/codehaus/groovy/classgen/asm/sc/StaticTypesCallSiteWriter.java index d77316a44b8..d558f9d84c1 100644 --- a/src/main/org/codehaus/groovy/classgen/asm/sc/StaticTypesCallSiteWriter.java +++ b/src/main/org/codehaus/groovy/classgen/asm/sc/StaticTypesCallSiteWriter.java @@ -137,6 +137,15 @@ public void makeGetPropertySite(Expression receiver, final String methodName, fi expr.visit(controller.getAcg()); return; } + + boolean isStaticProperty = receiver instanceof ClassExpression + && (receiverType.isDerivedFrom(receiver.getType()) || receiverType.implementsInterface(receiver.getType())); + + if (!isStaticProperty && (receiverType.implementsInterface(MAP_TYPE) || MAP_TYPE.equals(receiverType))) { + // for maps, replace map.foo with map.get('foo') + writeMapDotProperty(receiver, methodName, mv, safe); + return; + } if (makeGetPropertyWithGetter(receiver, receiverType, methodName, safe, implicitThis)) return; if (makeGetField(receiver, receiverType, methodName, safe, implicitThis, samePackages(receiverType.getPackageName(), classNode.getPackageName()))) return; if (receiverType.isEnum()) { @@ -211,22 +220,11 @@ public void makeGetPropertySite(Expression receiver, final String methodName, fi } } - boolean isStaticProperty = receiver instanceof ClassExpression - && (receiverType.isDerivedFrom(receiver.getType()) || receiverType.implementsInterface(receiver.getType())); - - if (!isStaticProperty) { - if (receiverType.implementsInterface(MAP_TYPE) || MAP_TYPE.equals(receiverType)) { - // for maps, replace map.foo with map.get('foo') - writeMapDotProperty(receiver, methodName, mv, safe); - return; - } - if (receiverType.implementsInterface(LIST_TYPE) || LIST_TYPE.equals(receiverType)) { - writeListDotProperty(receiver, methodName, mv, safe); - return; - } + if (!isStaticProperty && (receiverType.implementsInterface(LIST_TYPE) || LIST_TYPE.equals(receiverType))) { + writeListDotProperty(receiver, methodName, mv, safe); + return; } - controller.getSourceUnit().addError( new SyntaxException("Access to "+ (receiver instanceof ClassExpression ?receiver.getType():receiverType).toString(false) diff --git a/src/test/org/codehaus/groovy/classgen/asm/sc/ArraysAndCollectionsStaticCompileTest.groovy b/src/test/org/codehaus/groovy/classgen/asm/sc/ArraysAndCollectionsStaticCompileTest.groovy index fbb4f3ce03e..3e6f27829aa 100644 --- a/src/test/org/codehaus/groovy/classgen/asm/sc/ArraysAndCollectionsStaticCompileTest.groovy +++ b/src/test/org/codehaus/groovy/classgen/asm/sc/ArraysAndCollectionsStaticCompileTest.groovy @@ -128,6 +128,18 @@ class ArraysAndCollectionsStaticCompileTest extends ArraysAndCollectionsSTCTest } } + void testMapSubclassPropertyStyleAccess() { + assertScript ''' + class MyMap extends LinkedHashMap { + def foo = 1 + } + + def map = new MyMap() + map.put('foo', 42) + assert map.foo == 42 + ''' + } + @Override void testForInLoop() { try {