groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject groovy git commit: Minor refactoring: Extract common method for `ClassNode`
Date Fri, 09 Mar 2018 00:04:09 GMT
Repository: groovy
Updated Branches:
  refs/heads/master 3f41484e5 -> 3bbd5a75f


Minor refactoring: Extract common method for `ClassNode`


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/3bbd5a75
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/3bbd5a75
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/3bbd5a75

Branch: refs/heads/master
Commit: 3bbd5a75f6ae48604fe95067d21e55fc2206a380
Parents: 3f41484
Author: sunlan <sunlan@apache.org>
Authored: Fri Mar 9 08:04:00 2018 +0800
Committer: sunlan <sunlan@apache.org>
Committed: Fri Mar 9 08:04:00 2018 +0800

----------------------------------------------------------------------
 .../java/org/codehaus/groovy/ast/ClassNode.java | 31 ++++++++++++++++++++
 .../codehaus/groovy/classgen/asm/MopWriter.java |  2 +-
 .../asm/sc/StaticTypesWriterController.java     |  9 +++---
 src/test/groovy/transform/stc/LambdaTest.groovy | 16 ++++++++++
 4 files changed, 53 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/3bbd5a75/src/main/java/org/codehaus/groovy/ast/ClassNode.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/ast/ClassNode.java b/src/main/java/org/codehaus/groovy/ast/ClassNode.java
index 4593b12..fe8a490 100644
--- a/src/main/java/org/codehaus/groovy/ast/ClassNode.java
+++ b/src/main/java/org/codehaus/groovy/ast/ClassNode.java
@@ -964,6 +964,21 @@ public class ClassNode extends AnnotatedNode implements Opcodes, GroovydocHolder
     }
 
     /**
+     *
+     * @param classNodes the class nodes for the interfaces
+     * @return true if this class or any base class implements any of the given interfaces
+     */
+    public boolean implementsAnyInterfaces(ClassNode... classNodes) {
+        for (ClassNode classNode : classNodes) {
+            if (implementsInterface(classNode)) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+    /**
      * @param classNode the class node for the interface
      * @return true if this class or any base class implements the given interface
      */
@@ -980,6 +995,22 @@ public class ClassNode extends AnnotatedNode implements Opcodes, GroovydocHolder
     }
 
     /**
+     *
+     * @param classNodes the class nodes for the interfaces
+     * @return true if this class declares that it implements any of the given interfaces
+     * or if one of its interfaces extends directly or indirectly any of the given interfaces
+     */
+    public boolean declaresAnyInterfaces(ClassNode... classNodes) {
+        for (ClassNode classNode : classNodes) {
+            if (declaresInterface(classNode)) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+    /**
      * @param classNode the class node for the interface
      * @return true if this class declares that it implements the given interface
      * or if one of its interfaces extends directly or indirectly the interface

http://git-wip-us.apache.org/repos/asf/groovy/blob/3bbd5a75/src/main/java/org/codehaus/groovy/classgen/asm/MopWriter.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/classgen/asm/MopWriter.java b/src/main/java/org/codehaus/groovy/classgen/asm/MopWriter.java
index fff65d1..6d06742 100644
--- a/src/main/java/org/codehaus/groovy/classgen/asm/MopWriter.java
+++ b/src/main/java/org/codehaus/groovy/classgen/asm/MopWriter.java
@@ -86,7 +86,7 @@ public class MopWriter {
     
     public void createMopMethods() {
         ClassNode classNode = controller.getClassNode();
-        if (classNode.declaresInterface(ClassHelper.GENERATED_CLOSURE_Type) || classNode.declaresInterface(ClassHelper.GENERATED_LAMBDA_TYPE))
{
+        if (classNode.declaresAnyInterfaces(ClassHelper.GENERATED_CLOSURE_Type, ClassHelper.GENERATED_LAMBDA_TYPE))
{
             return;
         }
         Set<MopKey> currentClassSignatures = buildCurrentClassSignatureSet(classNode.getMethods());

http://git-wip-us.apache.org/repos/asf/groovy/blob/3bbd5a75/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesWriterController.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesWriterController.java
b/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesWriterController.java
index 5ce784a..7891d03 100644
--- a/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesWriterController.java
+++ b/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesWriterController.java
@@ -94,14 +94,15 @@ public class StaticTypesWriterController extends DelegatingController
{
     private void updateStaticCompileFlag(final MethodNode mn) {
         ClassNode classNode = getClassNode();
         AnnotatedNode node = mn;
-        boolean implementsGeneratedClosureOrGeneratedLambdaInterface = classNode.implementsInterface(ClassHelper.GENERATED_CLOSURE_Type)
|| classNode.implementsInterface(ClassHelper.GENERATED_LAMBDA_TYPE);
+        boolean implementsGeneratedClosureOrGeneratedLambdaInterface = classNode.implementsAnyInterfaces(ClassHelper.GENERATED_CLOSURE_Type,
ClassHelper.GENERATED_LAMBDA_TYPE);
         if (implementsGeneratedClosureOrGeneratedLambdaInterface) {
             node = classNode.getOuterClass();
         }
 
-        isInStaticallyCheckedMethod = mn != null && (
-                StaticCompilationVisitor.isStaticallyCompiled(node)
-                        || implementsGeneratedClosureOrGeneratedLambdaInterface &&
classNode.getNodeMetaData(StaticCompilationMetadataKeys.STATIC_COMPILE_NODE) != null);
+        boolean isStaticCompileNode = classNode.getNodeMetaData(StaticCompilationMetadataKeys.STATIC_COMPILE_NODE)
!= null;
+        isInStaticallyCheckedMethod =
+                mn != null && (StaticCompilationVisitor.isStaticallyCompiled(node)
+                                || implementsGeneratedClosureOrGeneratedLambdaInterface &&
isStaticCompileNode);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/groovy/blob/3bbd5a75/src/test/groovy/transform/stc/LambdaTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/transform/stc/LambdaTest.groovy b/src/test/groovy/transform/stc/LambdaTest.groovy
index 6ffd42e..1654781 100644
--- a/src/test/groovy/transform/stc/LambdaTest.groovy
+++ b/src/test/groovy/transform/stc/LambdaTest.groovy
@@ -60,6 +60,7 @@ class LambdaTest extends GroovyTestCase {
         '''
     }
 
+
     void testFunctionScript() {
         assertScript '''
         import groovy.transform.CompileStatic
@@ -75,6 +76,21 @@ class LambdaTest extends GroovyTestCase {
         '''
     }
 
+    void testFunctionScript2() {
+        assertScript '''
+        import groovy.transform.CompileStatic
+        import java.util.stream.Collectors
+        import java.util.stream.Stream
+        
+        @CompileStatic
+        void p() {
+            assert [2, 3, 4] == [1, 2, 3].stream().map(e -> e.plus 1).collect(Collectors.toList());
+        }
+        
+        p()
+        '''
+    }
+
     void testBinaryOperator() {
         assertScript '''
         import groovy.transform.CompileStatic


Mime
View raw message