You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2019/10/07 03:54:23 UTC
[groovy] branch GROOVY_3_0_X updated: GROOVY-9265: do not set
"owner" receiver for parameter or local variable
This is an automated email from the ASF dual-hosted git repository.
sunlan pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push:
new 2a5494c GROOVY-9265: do not set "owner" receiver for parameter or local variable
2a5494c is described below
commit 2a5494c43f1086880cfed67b2691533c58c03518
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Wed Oct 2 15:52:26 2019 -0500
GROOVY-9265: do not set "owner" receiver for parameter or local variable
(cherry picked from commit 9b8999558d6aee7b6c66e591112031be97ca42c3)
---
.../transform/stc/StaticTypeCheckingVisitor.java | 18 +++++---
src/test/groovy/bugs/Groovy9265.groovy | 49 ++++++++++++++++++++++
2 files changed, 61 insertions(+), 6 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
index 878cbd0..0e54e69 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -532,16 +532,22 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
*/
private String checkOrMarkInnerPropertyOwnerAccess(PropertyExpression source, boolean lhsOfAssignment, String delegationData) {
// check for reference to method, closure, for loop, try with, or catch block parameter from a non-nested closure
- if (typeCheckingContext.getEnclosingClosureStack().size() == 1 && !"this".equals(source.getPropertyAsString())) {
- if (!(source.getObjectExpression() instanceof VariableExpression &&
- ((VariableExpression) source.getObjectExpression()).getAccessedVariable() instanceof Parameter)) {
- delegationData = "owner";
- source.getObjectExpression().putNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER, delegationData);
- }
+ if (typeCheckingContext.getEnclosingClosureStack().size() == 1 && !"this".equals(source.getPropertyAsString()) &&
+ !isParameterOrVariableReference(source.getObjectExpression())) {
+ delegationData = "owner";
+ source.getObjectExpression().putNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER, delegationData);
}
return delegationData;
}
+ private static boolean isParameterOrVariableReference(Expression objectExpr) {
+ if (objectExpr instanceof VariableExpression) {
+ Variable accessedVar = ((VariableExpression) objectExpr).getAccessedVariable();
+ return (accessedVar instanceof Parameter || accessedVar instanceof VariableExpression);
+ }
+ return false;
+ }
+
private MethodNode findValidGetter(ClassNode classNode, String name) {
MethodNode getterMethod = classNode.getGetterMethod(name);
if (getterMethod != null && (getterMethod.isPublic() || getterMethod.isProtected())) {
diff --git a/src/test/groovy/bugs/Groovy9265.groovy b/src/test/groovy/bugs/Groovy9265.groovy
new file mode 100644
index 0000000..ba9b36d
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy9265.groovy
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package groovy.bugs
+
+import groovy.transform.CompileStatic
+import org.junit.Test
+
+import static groovy.test.GroovyAssert.assertScript
+
+@CompileStatic
+final class Groovy9265 {
+
+ @Test
+ void testAccessInnerClassFieldFromClosure() {
+ assertScript '''
+ class Outer {
+ static class Inner {
+ public String field = 'works'
+ }
+ }
+
+ @groovy.transform.CompileStatic
+ def test() {
+ return { ->
+ def var = new Outer.Inner()
+ var.field
+ }()
+ }
+
+ assert test() == 'works'
+ '''
+ }
+}