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/11/13 18:09:11 UTC

[groovy] 02/02: fix error message

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

commit e5097dd45b69158d0a1e4857478492ac221ab861
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Sat Nov 13 11:56:04 2021 -0600

    fix error message
---
 .../transform/stc/StaticTypeCheckingVisitor.java   | 23 ++++++++-------------
 .../{Groovy7987Bug.groovy => Groovy7987.groovy}    | 24 +++++++++++++---------
 .../groovy/transform/stc/MethodCallsSTCTest.groovy |  8 ++++----
 3 files changed, 26 insertions(+), 29 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 f4a51b2..c97324d 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -3447,10 +3447,8 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
                         }
                         mn = accessibleMethods;
                         if (accessibleMethods.isEmpty()) {
-                            // choose an arbitrary method to display an error message
-                            MethodNode node = inaccessibleMethods.get(0);
-                            ClassNode owner = node.getDeclaringClass();
-                            addStaticTypeError("Non static method " + owner.getName() + "#" + node.getName() + " cannot be called from static context", call);
+                            MethodNode node = inaccessibleMethods.get(0); // choose an arbitrary method to display an error message
+                            addStaticTypeError("Non-static method " + prettyPrintTypeName(node.getDeclaringClass()) + "#" + node.getName() + " cannot be called from static context", call);
                         }
                     }
 
@@ -3480,11 +3478,9 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
                     if (mn.size() == 1) {
                         MethodNode directMethodCallCandidate = mn.get(0);
                         ClassNode declaringClass = directMethodCallCandidate.getDeclaringClass();
-                        if (call.getNodeMetaData(DYNAMIC_RESOLUTION) == null
-                                && objectExpression instanceof ClassExpression
-                                && !directMethodCallCandidate.isStatic()
-                                && !isClassType(declaringClass)) {
-                            addStaticTypeError("Non static method " + declaringClass.getName() + "#" + directMethodCallCandidate.getName() + " cannot be called from static context", call);
+                        if (!directMethodCallCandidate.isStatic() && !isClassType(declaringClass)
+                                && objectExpression instanceof ClassExpression && call.getNodeMetaData(DYNAMIC_RESOLUTION) == null) {
+                            addStaticTypeError("Non-static method " + prettyPrintTypeName(declaringClass) + "#" + directMethodCallCandidate.getName() + " cannot be called from static context", call);
                         }
                         if (chosenReceiver == null) {
                             chosenReceiver = Receiver.make(declaringClass);
@@ -3550,17 +3546,14 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
                     }
                 }
             }
-            // adjust typing for explicit math methods which have special handling - operator variants handled elsewhere
-            if (NUMBER_OPS.containsKey(name) && isNumberType(receiver) && argumentList.getExpressions().size() == 1
-                    && isNumberType(getType(argumentList.getExpression(0)))) {
-                ClassNode right = getType(argumentList.getExpression(0));
-                ClassNode resultType = getMathResultType(NUMBER_OPS.get(name), receiver, right, name);
+            // adjust typing for explicit math methods which have special handling; operator variants handled elsewhere
+            if (args.length == 1 && isNumberType(args[0]) && isNumberType(receiver) && NUMBER_OPS.containsKey(name)) {
+                ClassNode resultType = getMathResultType(NUMBER_OPS.get(name), receiver, args[0], name);
                 if (resultType != null) {
                     storeType(call, resultType);
                 }
             }
 
-            // now that a method has been chosen, we are allowed to visit the closures
             MethodNode target = call.getNodeMetaData(DIRECT_METHOD_CALL_TARGET);
             if (!callArgsVisited) {
                 visitMethodCallArguments(receiver, argumentList, true, target);
diff --git a/src/test/groovy/bugs/Groovy7987Bug.groovy b/src/test/groovy/bugs/Groovy7987.groovy
similarity index 69%
rename from src/test/groovy/bugs/Groovy7987Bug.groovy
rename to src/test/groovy/bugs/Groovy7987.groovy
index 6d03466..7f15e08 100644
--- a/src/test/groovy/bugs/Groovy7987Bug.groovy
+++ b/src/test/groovy/bugs/Groovy7987.groovy
@@ -18,23 +18,27 @@
  */
 package groovy.bugs
 
-import gls.CompilableTestSupport
+import org.junit.Test
 
-class Groovy7987Bug extends CompilableTestSupport {
-    void testBindablePropertySettersHaveValidModifiersForMethod() {
-        def message = shouldNotCompile """
-            @groovy.transform.TypeChecked
+import static groovy.test.GroovyAssert.shouldFail
+
+final class Groovy7987 {
+
+    @Test
+    void testNonStaticMethodViaStaticReceiver() {
+        def err = shouldFail '''
             class Foo {
-                def bar() { }
+                def bar() {
+                }
             }
 
             @groovy.transform.TypeChecked
-            def method() {
+            void test() {
                 Foo.bar()
             }
 
-            method()
-        """
-        assert message.contains('Non static method Foo#bar cannot be called from static context')
+            test()
+        '''
+        assert err =~ 'Non-static method Foo#bar cannot be called from static context'
     }
 }
diff --git a/src/test/groovy/transform/stc/MethodCallsSTCTest.groovy b/src/test/groovy/transform/stc/MethodCallsSTCTest.groovy
index c083e82..517a55d 100644
--- a/src/test/groovy/transform/stc/MethodCallsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/MethodCallsSTCTest.groovy
@@ -901,7 +901,7 @@ class MethodCallsSTCTest extends StaticTypeCheckingTestCase {
                 }
             }
             A.staticMethod()
-        ''', 'Non static method A#instanceMethod cannot be called from static context'
+        ''', 'Non-static method A#instanceMethod cannot be called from static context'
     }
 
     void testShouldNotAllowMethodCallFromStaticConstructor() {
@@ -913,7 +913,7 @@ class MethodCallsSTCTest extends StaticTypeCheckingTestCase {
                 }
             }
             new A()
-        ''', 'Non static method A#instanceMethod cannot be called from static context'
+        ''', 'Non-static method A#instanceMethod cannot be called from static context'
     }
 
     void testShouldNotAllowMethodCallFromStaticField() {
@@ -923,7 +923,7 @@ class MethodCallsSTCTest extends StaticTypeCheckingTestCase {
                 static FOO = instanceMethod()
             }
             new A()
-        ''', 'Non static method A#instanceMethod cannot be called from static context'
+        ''', 'Non-static method A#instanceMethod cannot be called from static context'
     }
 
     // GROOVY-5495
@@ -1529,7 +1529,7 @@ class MethodCallsSTCTest extends StaticTypeCheckingTestCase {
                 }
             }
             null
-        ''', 'Non static method Foo#bar cannot be called from static context'
+        ''', 'Non-static method Foo#bar cannot be called from static context'
     }
 
     void testStaticOuterMethodCanBeCalledFromStaticClass() {