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/11/17 07:35:25 UTC

[groovy] branch master updated: GROOVY-8579, GROOVY-8930: check static interface method for direct calls

This is an automated email from the ASF dual-hosted git repository.

sunlan 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 fedc791  GROOVY-8579, GROOVY-8930: check static interface method for direct calls
fedc791 is described below

commit fedc791efd562ed1fe74e3887340c38cabfcbb19
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Fri Nov 15 13:09:44 2019 -0600

    GROOVY-8579, GROOVY-8930: check static interface method for direct calls
    
    - calling a static interface method requires Java 1.8+ bytecode
---
 .../transform/stc/StaticTypeCheckingVisitor.java   | 13 ++++++---
 .../{Groovy8579Bug.groovy => Groovy8579.groovy}    | 32 ++++++++++++++++------
 2 files changed, 33 insertions(+), 12 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 0725829..44140c0 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -3492,10 +3492,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
 
                             storeType(call, returnType);
                             storeTargetMethod(call, directMethodCallCandidate);
-                            ClassNode declaringClass = directMethodCallCandidate.getDeclaringClass();
-                            if (declaringClass.isInterface() && directMethodCallCandidate.isStatic() && !(directMethodCallCandidate instanceof ExtensionMethodNode)) {
-                                typeCheckingContext.getEnclosingClassNode().putNodeMetaData(MINIMUM_BYTECODE_VERSION, Opcodes.V1_8);
-                            }
+
                             String data = chosenReceiver.getData();
                             if (data != null) {
                                 // the method which has been chosen is supposed to be a call on delegate or owner
@@ -3768,6 +3765,14 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
 
     protected void storeTargetMethod(final Expression call, final MethodNode directMethodCallCandidate) {
         call.putNodeMetaData(DIRECT_METHOD_CALL_TARGET, directMethodCallCandidate);
+
+        if (directMethodCallCandidate != null
+                && directMethodCallCandidate.isStatic()
+                && directMethodCallCandidate.getDeclaringClass().isInterface()
+                && !(directMethodCallCandidate instanceof ExtensionMethodNode)) {
+            typeCheckingContext.getEnclosingClassNode().putNodeMetaData(MINIMUM_BYTECODE_VERSION, Opcodes.V1_8);
+        }
+
         checkOrMarkPrivateAccess(call, directMethodCallCandidate);
         checkSuperCallFromClosure(call, directMethodCallCandidate);
         extension.onMethodSelection(call, directMethodCallCandidate);
diff --git a/subprojects/tests-vm8/src/test/groovy/groovy/bugs/vm8/Groovy8579Bug.groovy b/subprojects/tests-vm8/src/test/groovy/groovy/bugs/vm8/Groovy8579.groovy
similarity index 60%
rename from subprojects/tests-vm8/src/test/groovy/groovy/bugs/vm8/Groovy8579Bug.groovy
rename to subprojects/tests-vm8/src/test/groovy/groovy/bugs/vm8/Groovy8579.groovy
index 153f33f..8105cea 100644
--- a/subprojects/tests-vm8/src/test/groovy/groovy/bugs/vm8/Groovy8579Bug.groovy
+++ b/subprojects/tests-vm8/src/test/groovy/groovy/bugs/vm8/Groovy8579.groovy
@@ -18,19 +18,35 @@
  */
 package groovy.bugs.vm8
 
-import groovy.test.GroovyTestCase
+import org.junit.Test
 
-class Groovy8579Bug extends GroovyTestCase {
-    void testCallToStaticInterfaceMethod() {
-        assertScript '''
-            import groovy.transform.CompileStatic
+import static groovy.test.GroovyAssert.assertScript
+
+final class Groovy8579 {
 
-            @CompileStatic
-            Comparator myMethod() {
+    @Test
+    void testCallToStaticInterfaceMethod1() {
+        assertScript '''
+            @groovy.transform.CompileStatic
+            Comparator test() {
                 Map.Entry.comparingByKey()
             }
 
-            assert myMethod() instanceof Comparator
+            assert test() instanceof Comparator
+        '''
+    }
+
+    @Test
+    void testCallToStaticInterfaceMethod2() {
+        assertScript '''
+            import static java.util.Map.Entry.comparingByKey
+
+            @groovy.transform.CompileStatic
+            Comparator test() {
+                comparingByKey()
+            }
+
+            assert test() instanceof Comparator
         '''
     }
 }