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/12/24 00:12:28 UTC

[groovy] 01/04: add missing pops and other minor edits

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

sunlan pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 6984ec4bfd861a36404e77b0e8fd161f03271bf7
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Mon Dec 23 12:02:05 2019 -0600

    add missing pops and other minor edits
    
    (cherry picked from commit 6ee13a0aeca344c092f8028cba7c5b0d125c8774)
---
 .../transform/stc/StaticTypeCheckingVisitor.java   | 88 +++++++++++-----------
 1 file changed, 45 insertions(+), 43 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 659597a..30c389b 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -394,55 +394,50 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
     @Override
     public void visitClass(final ClassNode node) {
         if (shouldSkipClassNode(node)) return;
-        if (extension.beforeVisitClass(node)) {
-            extension.afterVisitClass(node);
-            return;
-        }
-        Object type = node.getNodeMetaData(INFERRED_TYPE);
-        if (type != null) {
-            // transformation has already been run on this class node
-            // so we'll use a silent collector in order not to duplicate errors
-            typeCheckingContext.pushErrorCollector();
-        }
-        typeCheckingContext.pushEnclosingClassNode(node);
-        Set<MethodNode> oldVisitedMethod = typeCheckingContext.alreadyVisitedMethods;
-        typeCheckingContext.alreadyVisitedMethods = new LinkedHashSet<>();
-        super.visitClass(node);
-        Iterator<InnerClassNode> innerClasses = node.getInnerClasses();
-        while (innerClasses.hasNext()) {
-            InnerClassNode innerClassNode = innerClasses.next();
-            visitClass(innerClassNode);
-        }
-        typeCheckingContext.alreadyVisitedMethods = oldVisitedMethod;
-        node.putNodeMetaData(INFERRED_TYPE, node);
-        // mark all methods as visited. We can't do this in visitMethod because the type checker
-        // works in a two pass sequence and we don't want to skip the second pass
-        for (MethodNode methodNode : node.getMethods()) {
-            methodNode.putNodeMetaData(StaticTypeCheckingVisitor.class, Boolean.TRUE);
-        }
-        for (ConstructorNode constructorNode : node.getDeclaredConstructors()) {
-            constructorNode.putNodeMetaData(StaticTypeCheckingVisitor.class, Boolean.TRUE);
+        if (!extension.beforeVisitClass(node)) {
+            Object type = node.getNodeMetaData(INFERRED_TYPE);
+            if (type != null) {
+                // transformation has already been run on this class node
+                // so use a silent collector in order not to duplicate errors
+                typeCheckingContext.pushErrorCollector();
+            }
+            typeCheckingContext.pushEnclosingClassNode(node);
+            Set<MethodNode> oldSet = typeCheckingContext.alreadyVisitedMethods;
+            typeCheckingContext.alreadyVisitedMethods = new LinkedHashSet<>();
+
+            super.visitClass(node);
+            node.getInnerClasses().forEachRemaining(this::visitClass);
+
+            typeCheckingContext.alreadyVisitedMethods = oldSet;
+            typeCheckingContext.popEnclosingClassNode();
+            if (type != null) {
+                typeCheckingContext.popErrorCollector();
+            }
+
+            node.putNodeMetaData(INFERRED_TYPE, node);
+            // mark all methods as visited. We can't do this in visitMethod because the type checker
+            // works in a two pass sequence and we don't want to skip the second pass
+            node.getMethods().forEach(n -> n.putNodeMetaData(StaticTypeCheckingVisitor.class, Boolean.TRUE));
+            node.getDeclaredConstructors().forEach(n -> n.putNodeMetaData(StaticTypeCheckingVisitor.class, Boolean.TRUE));
         }
         extension.afterVisitClass(node);
     }
 
-    protected boolean shouldSkipClassNode(final ClassNode node) {
-        return isSkipMode(node);
-    }
-
     /**
-     * Returns the list of type checking annotations class nodes. Subclasses may override this method
-     * in order to provide additional classes which must be looked up when checking if a method or
-     * a class node should be skipped.
+     * Returns array of type checking annotations. Subclasses may override this
+     * method in order to provide additional types which must be looked up when
+     * checking if a method or a class node should be skipped.
      * <p>
      * The default implementation returns {@link TypeChecked}.
-     *
-     * @return array of class nodes
      */
     protected ClassNode[] getTypeCheckingAnnotations() {
         return TYPECHECKING_ANNOTATIONS;
     }
 
+    protected boolean shouldSkipClassNode(final ClassNode node) {
+        return isSkipMode(node);
+    }
+
     public boolean isSkipMode(final AnnotatedNode node) {
         if (node == null) return false;
         for (ClassNode tca : getTypeCheckingAnnotations()) {
@@ -465,19 +460,25 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
         if (node instanceof MethodNode) {
             return isSkipMode(node.getDeclaringClass());
         }
-        if (isSkippedInnerClass(node)) return true;
-        return false;
+        return isSkippedInnerClass(node);
     }
 
     /**
-     * Test if a node is an inner class node, and if it is, then checks if the enclosing method is skipped.
+     * Tests if a node is an inner class node, and if it is, then checks if the enclosing method is skipped.
      *
      * @return true if the inner class node should be skipped
      */
     protected boolean isSkippedInnerClass(final AnnotatedNode node) {
-        if (!(node instanceof InnerClassNode)) return false;
-        MethodNode enclosingMethod = ((InnerClassNode) node).getEnclosingMethod();
-        return enclosingMethod != null && isSkipMode(enclosingMethod);
+        if (node instanceof ClassNode) {
+            ClassNode type = (ClassNode) node;
+            if (type.getOuterClass() != null) {
+                MethodNode enclosingMethod = type.getEnclosingMethod();
+                if (enclosingMethod != null && isSkipMode(enclosingMethod)) {
+                    return true;
+                }
+            }
+        }
+        return false;
     }
 
     @Override
@@ -2210,6 +2211,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
             }
             if (node != null) storeTargetMethod(call, node);
         }
+
         extension.afterMethodCall(call);
     }