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 2021/05/28 14:55:30 UTC
[groovy] branch master updated: GROOVY-5364: find static members
from static script methods
This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/master by this push:
new 7d21ac4 GROOVY-5364: find static members from static script methods
7d21ac4 is described below
commit 7d21ac466be191b7d2ce6f6c0a480cf94180c0a7
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Fri May 28 09:55:01 2021 -0500
GROOVY-5364: find static members from static script methods
---
.../groovy/classgen/VariableScopeVisitor.java | 6 +-
src/test/groovy/bugs/Groovy5364.groovy | 104 +++++++++++++++++++++
2 files changed, 105 insertions(+), 5 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java b/src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java
index 542c7b7..8fba09c 100644
--- a/src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java
+++ b/src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java
@@ -171,10 +171,6 @@ public class VariableScopeVisitor extends ClassCodeVisitorSupport {
final boolean abstractType = node.isAbstract();
for (ClassNode cn = node; cn != null && !isObjectType(cn); cn = cn.getSuperClass()) {
- if (cn.isScript()) {
- return new DynamicVariable(name, false);
- }
-
for (FieldNode fn : cn.getFields()) {
if (name.equals(fn.getName())) return fn;
}
@@ -243,7 +239,7 @@ public class VariableScopeVisitor extends ClassCodeVisitorSupport {
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)
- if (!(staticScope && !staticMember)) {
+ if (!(staticScope ? !staticMember : node.isScript())) {
variable = member;
}
}
diff --git a/src/test/groovy/bugs/Groovy5364.groovy b/src/test/groovy/bugs/Groovy5364.groovy
new file mode 100644
index 0000000..91300ff
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy5364.groovy
@@ -0,0 +1,104 @@
+/*
+ * 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 org.junit.Test
+
+import static groovy.test.GroovyAssert.assertScript
+import static groovy.test.GroovyAssert.shouldFail
+
+final class Groovy5364 {
+
+ @Test
+ void testStaticScriptMethodAsProperty1() {
+ assertScript '''
+ static getStaticProperty() {
+ 'x'
+ }
+
+ assert 'x' == getStaticProperty()
+ assert 'x' == staticProperty
+ '''
+ }
+
+ @Test
+ void testStaticScriptMethodAsProperty2() {
+ assertScript '''
+ static getSomething() { 'x' }
+ something = 'y' // script var
+
+ assert 'x' == getSomething()
+ assert 'y' == something
+ '''
+ }
+
+ @Test
+ void testStaticScriptMethodAsProperty3() {
+ assertScript new GroovyShell(new Binding(something: 'y')), '''
+ static getSomething() { 'x' }
+
+ assert 'x' == getSomething()
+ assert 'y' == something
+ '''
+ }
+
+ @Test
+ void testStaticScriptMethodAsProperty4() {
+ assertScript '''
+ static getStaticProperty() {
+ 'x'
+ }
+
+ void test() {
+ assert 'x' == getStaticProperty()
+ assert 'x' == staticProperty
+ }
+ test()
+ '''
+ }
+
+ @Test
+ void testStaticScriptMethodAsProperty5() {
+ assertScript '''
+ static getStaticProperty() {
+ 'x'
+ }
+
+ static void test() {
+ assert 'x' == getStaticProperty()
+ assert 'x' == staticProperty // Apparent variable 'staticProperty' was found in a static scope but doesn't refer to a local variable, static field or class
+ }
+ test()
+ '''
+ }
+
+ @Test
+ void testStaticScriptMethodAsProperty6() {
+ def err = shouldFail '''
+ def getNonStaticProperty() {
+ 'x'
+ }
+
+ static void test() {
+ nonStaticProperty
+ }
+ '''
+ assert err =~ /Apparent variable 'nonStaticProperty' was found in a static scope but doesn't refer to a local variable, static field or class/
+ }
+}