You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2020/06/23 02:59:17 UTC
[groovy] branch GROOVY-9598 updated (38d11b0 -> 68f8106)
This is an automated email from the ASF dual-hosted git repository.
emilles pushed a change to branch GROOVY-9598
in repository https://gitbox.apache.org/repos/asf/groovy.git.
discard 38d11b0 GROOVY-9598: account for static modifier when checking for outer members
new 68f8106 GROOVY-9598: account for static modifier when checking for outer members
This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version. This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:
* -- * -- B -- O -- O -- O (38d11b0)
\
N -- N -- N refs/heads/GROOVY-9598 (68f8106)
You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.
Any revisions marked "omit" are not gone; other references still
refer to them. Any revisions marked "discard" are gone forever.
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../groovy/classgen/VariableScopeVisitor.java | 7 +++-
src/test/gls/innerClass/InnerClassTest.groovy | 38 ++++++++++++++++++++++
.../stc/FieldsAndPropertiesSTCTest.groovy | 2 +-
3 files changed, 45 insertions(+), 2 deletions(-)
[groovy] 01/01: GROOVY-9598: account for static modifier when
checking for outer members
Posted by em...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch GROOVY-9598
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 68f81068d941df82e84bafb8a000d1eba0054698
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Mon Jun 22 19:56:48 2020 -0500
GROOVY-9598: account for static modifier when checking for outer members
---
.../groovy/classgen/VariableScopeVisitor.java | 31 +++++++-------
src/test/gls/innerClass/InnerClassTest.groovy | 38 +++++++++++++++++
.../stc/FieldsAndPropertiesSTCTest.groovy | 47 ++++++++++++++++++++++
3 files changed, 100 insertions(+), 16 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java b/src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java
index 12edb39..ef22321 100644
--- a/src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java
+++ b/src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java
@@ -56,6 +56,7 @@ import java.util.LinkedList;
import java.util.function.BiConsumer;
import static java.lang.reflect.Modifier.isFinal;
+import static java.lang.reflect.Modifier.isStatic;
import static org.apache.groovy.ast.tools.MethodNodeUtils.getPropertyName;
/**
@@ -191,10 +192,12 @@ public class VariableScopeVisitor extends ClassCodeVisitorSupport {
if (pn.getName().equals(name)) return pn;
}
- Variable ret = findClassMember(cn.getSuperClass(), name);
- if (ret != null) return ret;
- if (isAnonymous(cn)) return null;
- return findClassMember(cn.getOuterClass(), name);
+ for (ClassNode face : cn.getInterfaces()) {
+ FieldNode fn = face.getDeclaredField(name);
+ if (fn != null) return fn;
+ }
+
+ return findClassMember(cn.getSuperClass(), name);
}
private Variable findVariableDeclaration(final String name) {
@@ -225,9 +228,13 @@ public class VariableScopeVisitor extends ClassCodeVisitorSupport {
break;
}
- ClassNode classScope = scope.getClassScope();
- if (classScope != null) {
- Variable member = findClassMember(classScope, name);
+ ClassNode node = scope.getClassScope();
+ if (node != null) {
+ Variable member = findClassMember(node, name);
+ while (member == null && node.getOuterClass() != null && !isAnonymous(node)) {
+ crossingStaticContext = (crossingStaticContext || isStatic(node.getModifiers()));
+ member = findClassMember((node = node.getOuterClass()), name);
+ }
if (member != null) {
boolean staticScope = (crossingStaticContext || inSpecialConstructorCall), staticMember = member.isInStaticContext();
// prevent a static context (e.g. a static method) from accessing a non-static variable (e.g. a non-static field)
@@ -236,7 +243,7 @@ public class VariableScopeVisitor extends ClassCodeVisitorSupport {
}
}
// GROOVY-5961
- if (!isAnonymous(classScope)) break;
+ if (!isAnonymous(scope.getClassScope())) break;
}
scope = scope.getParent();
}
@@ -460,14 +467,6 @@ public class VariableScopeVisitor extends ClassCodeVisitorSupport {
popState();
}
- /**
- * Sets the current class node context.
- */
- public void prepareVisit(ClassNode node) {
- currentClass = node;
- currentScope.setClassScope(node);
- }
-
@Override
public void visitConstructorCallExpression(final ConstructorCallExpression expression) {
boolean oldInSpecialCtorFlag = inSpecialConstructorCall;
diff --git a/src/test/gls/innerClass/InnerClassTest.groovy b/src/test/gls/innerClass/InnerClassTest.groovy
index a9ba77a..5615e00 100644
--- a/src/test/gls/innerClass/InnerClassTest.groovy
+++ b/src/test/gls/innerClass/InnerClassTest.groovy
@@ -600,6 +600,26 @@ final class InnerClassTest {
}
@Test
+ void testUsageOfOuterField12() {
+ def err = shouldFail '''
+ class C {
+ int count
+ static def m() {
+ new LinkedList() {
+ def get(int i) {
+ count += 1
+ super.get(i)
+ }
+ }
+ }
+ }
+ C.m()
+ '''
+
+ assert err =~ /Apparent variable 'count' was found in a static scope but doesn't refer to a local variable, static field or class./
+ }
+
+ @Test
void testUsageOfOuterSuperField() {
assertScript '''
class InnerBase {
@@ -635,6 +655,24 @@ final class InnerClassTest {
}
@Test
+ void testUsageOfOuterSuperField2() {
+ assertScript '''
+ interface I {
+ String CONST = 'value'
+ }
+ class A implements I {
+ static class B {
+ def test() {
+ CONST
+ }
+ }
+ }
+ def x = new A.B().test()
+ assert x == 'value'
+ '''
+ }
+
+ @Test
void testUsageOfOuterField_WrongCallToSuper() {
shouldFail '''
class Outer {
diff --git a/src/test/groovy/transform/stc/FieldsAndPropertiesSTCTest.groovy b/src/test/groovy/transform/stc/FieldsAndPropertiesSTCTest.groovy
index 545cde5..3b39eff 100644
--- a/src/test/groovy/transform/stc/FieldsAndPropertiesSTCTest.groovy
+++ b/src/test/groovy/transform/stc/FieldsAndPropertiesSTCTest.groovy
@@ -387,6 +387,53 @@ class FieldsAndPropertiesSTCTest extends StaticTypeCheckingTestCase {
'''
}
+ void testOuterPropertyAccess1() {
+ assertScript '''
+ class Outer {
+ class Inner {
+ def m() {
+ p
+ }
+ }
+ def p = 1
+ }
+ def i = new Outer.Inner(new Outer())
+ def x = i.m()
+ assert x == 1
+ '''
+ }
+
+ // GROOVY-9598
+ void testOuterPropertyAccess2() {
+ shouldFailWithMessages '''
+ class Outer {
+ static class Inner {
+ def m() {
+ p
+ }
+ }
+ def p = 1
+ }
+ def i = new Outer.Inner()
+ def x = i.m()
+ ''', "Apparent variable 'p' was found in a static scope but doesn't refer to a local variable, static field or class."
+ }
+
+ void testOuterPropertyAccess3() {
+ shouldFailWithMessages '''
+ class Outer {
+ static class Inner {
+ def m() {
+ this.p
+ }
+ }
+ def p = 1
+ }
+ def i = new Outer.Inner()
+ def x = i.m()
+ ''', 'No such property: p for class: Outer$Inner'
+ }
+
void testPrivateFieldAccessInClosure() {
assertScript '''
class A {