groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From emil...@apache.org
Subject [groovy] branch master updated: minor refactor
Date Thu, 10 Oct 2019 16:23:13 GMT
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 12dca39  minor refactor
12dca39 is described below

commit 12dca398e711ce6c703998b46c928f6bc87f0018
Author: Eric Milles <eric.milles@thomsonreuters.com>
AuthorDate: Thu Oct 10 11:12:33 2019 -0500

    minor refactor
---
 .../groovy/ast/ClassCodeExpressionTransformer.java | 118 +++++++++++----------
 .../groovy/ast/ClassCodeVisitorSupport.java        |  14 ++-
 .../codehaus/groovy/classgen/ExtendedVerifier.java |  29 ++---
 .../groovy/classgen/VariableScopeVisitor.java      |  33 +++---
 .../codehaus/groovy/control/ResolveVisitor.java    |  34 +++---
 5 files changed, 117 insertions(+), 111 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/ast/ClassCodeExpressionTransformer.java b/src/main/java/org/codehaus/groovy/ast/ClassCodeExpressionTransformer.java
index 65bbddd..5d00c82 100644
--- a/src/main/java/org/codehaus/groovy/ast/ClassCodeExpressionTransformer.java
+++ b/src/main/java/org/codehaus/groovy/ast/ClassCodeExpressionTransformer.java
@@ -34,15 +34,27 @@ import org.codehaus.groovy.ast.stmt.SynchronizedStatement;
 import org.codehaus.groovy.ast.stmt.ThrowStatement;
 import org.codehaus.groovy.ast.stmt.WhileStatement;
 
-import java.util.List;
 import java.util.Map;
 
 /**
- * Visitor to transform expressions in a whole class.
- * Transformed Expressions are usually not visited.
+ * Transforms expressions in a whole class. Transformed expressions are usually not visited.
  */
 public abstract class ClassCodeExpressionTransformer extends ClassCodeVisitorSupport implements
ExpressionTransformer {
 
+    @Override
+    public Expression transform(Expression expr) {
+        if (expr == null) return null;
+        return expr.transformExpression(this);
+    }
+
+    @Override
+    protected void visitAnnotation(AnnotationNode node) {
+        for (Map.Entry<String, Expression> entry : node.getMembers().entrySet()) {
+            entry.setValue(transform(entry.getValue()));
+        }
+    }
+
+    @Override
     protected void visitConstructorOrMethod(MethodNode node, boolean isConstructor) {
         for (Parameter p : node.getParameters()) {
             if (p.hasInitialExpression()) {
@@ -53,21 +65,14 @@ public abstract class ClassCodeExpressionTransformer extends ClassCodeVisitorSup
         super.visitConstructorOrMethod(node, isConstructor);
     }
 
-    public void visitSwitch(SwitchStatement statement) {
-        Expression exp = statement.getExpression();
-        statement.setExpression(transform(exp));
-        for (CaseStatement caseStatement : statement.getCaseStatements()) {
-            caseStatement.visit(this);
-        }
-        statement.getDefaultStatement().visit(this);
-    }
-
+    @Override
     public void visitField(FieldNode node) {
         visitAnnotations(node);
         Expression init = node.getInitialExpression();
         node.setInitialValueExpression(transform(init));
     }
 
+    @Override
     public void visitProperty(PropertyNode node) {
         visitAnnotations(node);
         Statement statement = node.getGetterBlock();
@@ -77,68 +82,73 @@ public abstract class ClassCodeExpressionTransformer extends ClassCodeVisitorSup
         visitClassCodeContainer(statement);
     }
 
-    public void visitIfElse(IfStatement ifElse) {
-        ifElse.setBooleanExpression((BooleanExpression) (transform(ifElse.getBooleanExpression())));
-        ifElse.getIfBlock().visit(this);
-        ifElse.getElseBlock().visit(this);
-    }
+    // statements:
 
-    public Expression transform(Expression exp) {
-        if (exp == null) return null;
-        return exp.transformExpression(this);
+    @Override
+    public void visitAssertStatement(AssertStatement stmt) {
+        stmt.setBooleanExpression((BooleanExpression) transform(stmt.getBooleanExpression()));
+        stmt.setMessageExpression(transform(stmt.getMessageExpression()));
     }
 
-    public void visitAnnotations(AnnotatedNode node) {
-        List<AnnotationNode> annotations = node.getAnnotations();
-        if (annotations.isEmpty()) return;
-        for (AnnotationNode an : annotations) {
-            // skip built-in properties
-            if (an.isBuiltIn()) continue;
-            for (Map.Entry<String, Expression> member : an.getMembers().entrySet())
{
-                member.setValue(transform(member.getValue()));
-            }
-        }
+    @Override
+    public void visitCaseStatement(CaseStatement stmt) {
+        stmt.setExpression(transform(stmt.getExpression()));
+        stmt.getCode().visit(this);
     }
 
-    public void visitReturnStatement(ReturnStatement statement) {
-        statement.setExpression(transform(statement.getExpression()));
+    @Override
+    public void visitDoWhileLoop(DoWhileStatement stmt) {
+        stmt.setBooleanExpression((BooleanExpression) transform(stmt.getBooleanExpression()));
+        super.visitDoWhileLoop(stmt);
     }
 
-    public void visitAssertStatement(AssertStatement as) {
-        as.setBooleanExpression((BooleanExpression) (transform(as.getBooleanExpression())));
-        as.setMessageExpression(transform(as.getMessageExpression()));
+    @Override
+    public void visitExpressionStatement(ExpressionStatement stmt) {
+        stmt.setExpression(transform(stmt.getExpression()));
     }
 
-    public void visitCaseStatement(CaseStatement statement) {
-        statement.setExpression(transform(statement.getExpression()));
-        statement.getCode().visit(this);
+    @Override
+    public void visitForLoop(ForStatement stmt) {
+        stmt.setCollectionExpression(transform(stmt.getCollectionExpression()));
+        super.visitForLoop(stmt);
     }
 
-    public void visitDoWhileLoop(DoWhileStatement loop) {
-        loop.setBooleanExpression((BooleanExpression) (transform(loop.getBooleanExpression())));
-        super.visitDoWhileLoop(loop);
+    @Override
+    public void visitIfElse(IfStatement stmt) {
+        stmt.setBooleanExpression((BooleanExpression) transform(stmt.getBooleanExpression()));
+        stmt.getIfBlock().visit(this);
+        stmt.getElseBlock().visit(this);
     }
 
-    public void visitForLoop(ForStatement forLoop) {
-        forLoop.setCollectionExpression(transform(forLoop.getCollectionExpression()));
-        super.visitForLoop(forLoop);
+    @Override
+    public void visitReturnStatement(ReturnStatement stmt) {
+        stmt.setExpression(transform(stmt.getExpression()));
     }
 
-    public void visitSynchronizedStatement(SynchronizedStatement sync) {
-        sync.setExpression(transform(sync.getExpression()));
-        super.visitSynchronizedStatement(sync);
+    @Override
+    public void visitSwitch(SwitchStatement stmt) {
+        Expression exp = stmt.getExpression();
+        stmt.setExpression(transform(exp));
+        for (CaseStatement caseStatement : stmt.getCaseStatements()) {
+            caseStatement.visit(this);
+        }
+        stmt.getDefaultStatement().visit(this);
     }
 
-    public void visitThrowStatement(ThrowStatement ts) {
-        ts.setExpression(transform(ts.getExpression()));
+    @Override
+    public void visitSynchronizedStatement(SynchronizedStatement stmt) {
+        stmt.setExpression(transform(stmt.getExpression()));
+        super.visitSynchronizedStatement(stmt);
     }
 
-    public void visitWhileLoop(WhileStatement loop) {
-        loop.setBooleanExpression((BooleanExpression) transform(loop.getBooleanExpression()));
-        super.visitWhileLoop(loop);
+    @Override
+    public void visitThrowStatement(ThrowStatement stmt) {
+        stmt.setExpression(transform(stmt.getExpression()));
     }
 
-    public void visitExpressionStatement(ExpressionStatement es) {
-        es.setExpression(transform(es.getExpression()));
+    @Override
+    public void visitWhileLoop(WhileStatement stmt) {
+        stmt.setBooleanExpression((BooleanExpression) transform(stmt.getBooleanExpression()));
+        super.visitWhileLoop(stmt);
     }
 }
diff --git a/src/main/java/org/codehaus/groovy/ast/ClassCodeVisitorSupport.java b/src/main/java/org/codehaus/groovy/ast/ClassCodeVisitorSupport.java
index 15dfa9e..13e476d 100644
--- a/src/main/java/org/codehaus/groovy/ast/ClassCodeVisitorSupport.java
+++ b/src/main/java/org/codehaus/groovy/ast/ClassCodeVisitorSupport.java
@@ -42,10 +42,9 @@ import org.codehaus.groovy.control.messages.SyntaxErrorMessage;
 import org.codehaus.groovy.syntax.SyntaxException;
 import org.codehaus.groovy.transform.ErrorCollecting;
 
-import java.util.Map;
-
 public abstract class ClassCodeVisitorSupport extends CodeVisitorSupport implements ErrorCollecting,
GroovyClassVisitor {
 
+    @Override
     public void visitClass(ClassNode node) {
         visitAnnotations(node);
         visitPackage(node.getPackage());
@@ -57,13 +56,18 @@ public abstract class ClassCodeVisitorSupport extends CodeVisitorSupport
impleme
     public void visitAnnotations(AnnotatedNode node) {
         for (AnnotationNode annotation : node.getAnnotations()) {
             // skip built-in properties
-            if (annotation.isBuiltIn()) continue;
-            for (Map.Entry<String, Expression> member : annotation.getMembers().entrySet())
{
-                member.getValue().visit(this);
+            if (!annotation.isBuiltIn()) {
+                visitAnnotation(annotation);
             }
         }
     }
 
+    protected void visitAnnotation(AnnotationNode node) {
+        for (Expression expr : node.getMembers().values()) {
+            expr.visit(this);
+        }
+    }
+
     public void visitPackage(PackageNode node) {
         if (node != null) {
             visitAnnotations(node);
diff --git a/src/main/java/org/codehaus/groovy/classgen/ExtendedVerifier.java b/src/main/java/org/codehaus/groovy/classgen/ExtendedVerifier.java
index 16a1591..520ef4f 100644
--- a/src/main/java/org/codehaus/groovy/classgen/ExtendedVerifier.java
+++ b/src/main/java/org/codehaus/groovy/classgen/ExtendedVerifier.java
@@ -158,9 +158,16 @@ public class ExtendedVerifier extends ClassCodeVisitorSupport {
             addError("Annotations are not supported in the current runtime. " + JVM_ERROR_MESSAGE,
node);
             return;
         }
-        Map<String, List<AnnotationNode>> nonSourceAnnotations = new LinkedHashMap<String,
List<AnnotationNode>>();
+        Map<String, List<AnnotationNode>> nonSourceAnnotations = new LinkedHashMap<>();
         for (AnnotationNode unvisited : node.getAnnotations()) {
-            AnnotationNode visited = visitAnnotation(unvisited);
+            AnnotationNode visited;
+            {
+                ErrorCollector errorCollector = new ErrorCollector(source.getConfiguration());
+                AnnotationVisitor visitor = new AnnotationVisitor(source, errorCollector);
+                visited = visitor.visit(unvisited);
+                source.getErrorCollector().addCollectorContents(errorCollector);
+            }
+
             String name = visited.getClassNode().getName();
             if (!visited.hasSourceRetention()) {
                 List<AnnotationNode> seen = nonSourceAnnotations.get(name);
@@ -206,8 +213,8 @@ public class ExtendedVerifier extends ClassCodeVisitorSupport {
                 if (repeatable != null) {
                     AnnotationNode collector = new AnnotationNode(repeatable);
                     if (repeatable.isResolved()) {
-                        Class repeatableType = repeatable.getTypeClass();
-                        Retention retAnn = (Retention) repeatableType.getAnnotation(Retention.class);
+                        Class<?> repeatableType = repeatable.getTypeClass();
+                        Retention retAnn = repeatableType.getAnnotation(Retention.class);
                         collector.setRuntimeRetention(retAnn != null && retAnn.value().equals(RetentionPolicy.RUNTIME));
                     } else if (repeatable.redirect() != null) {
                         for (AnnotationNode annotationNode : repeatable.redirect().getAnnotations())
{
@@ -318,20 +325,6 @@ public class ExtendedVerifier extends ClassCodeVisitorSupport {
     }
 
     /**
-     * Resolve metadata and details of the annotation.
-     *
-     * @param unvisited the node to visit
-     * @return the visited node
-     */
-    private AnnotationNode visitAnnotation(AnnotationNode unvisited) {
-        ErrorCollector errorCollector = new ErrorCollector(this.source.getConfiguration());
-        AnnotationVisitor visitor = new AnnotationVisitor(this.source, errorCollector);
-        AnnotationNode visited = visitor.visit(unvisited);
-        this.source.getErrorCollector().addCollectorContents(errorCollector);
-        return visited;
-    }
-
-    /**
      * Check if the current runtime allows Annotation usage.
      *
      * @return true if running on a 1.5+ runtime
diff --git a/src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java b/src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java
index 7c6799d..12ae1ed 100644
--- a/src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java
+++ b/src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java
@@ -20,8 +20,6 @@ package org.codehaus.groovy.classgen;
 
 import org.codehaus.groovy.GroovyBugError;
 import org.codehaus.groovy.ast.ASTNode;
-import org.codehaus.groovy.ast.AnnotatedNode;
-import org.codehaus.groovy.ast.AnnotationNode;
 import org.codehaus.groovy.ast.ClassCodeVisitorSupport;
 import org.codehaus.groovy.ast.ClassHelper;
 import org.codehaus.groovy.ast.ClassNode;
@@ -54,8 +52,6 @@ import org.codehaus.groovy.syntax.Types;
 
 import java.util.Iterator;
 import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
 
 import static java.lang.reflect.Modifier.isFinal;
 import static org.apache.groovy.ast.tools.MethodNodeUtils.getPropertyName;
@@ -172,6 +168,7 @@ public class VariableScopeVisitor extends ClassCodeVisitorSupport {
         currentScope.putDeclaredVariable(var);
     }
 
+    @Override
     protected SourceUnit getSourceUnit() {
         return source;
     }
@@ -319,6 +316,7 @@ public class VariableScopeVisitor extends ClassCodeVisitorSupport {
     // code visit
     // ------------------------------
 
+    @Override
     public void visitBlockStatement(BlockStatement block) {
         pushState();
         block.setVariableScope(currentScope);
@@ -326,6 +324,7 @@ public class VariableScopeVisitor extends ClassCodeVisitorSupport {
         popState();
     }
 
+    @Override
     public void visitForLoop(ForStatement forLoop) {
         pushState();
         forLoop.setVariableScope(currentScope);
@@ -336,6 +335,7 @@ public class VariableScopeVisitor extends ClassCodeVisitorSupport {
         popState();
     }
 
+    @Override
     public void visitIfElse(IfStatement ifElse) {
         ifElse.getBooleanExpression().visit(this);
         pushState();
@@ -346,6 +346,7 @@ public class VariableScopeVisitor extends ClassCodeVisitorSupport {
         popState();
     }
 
+    @Override
     public void visitDeclarationExpression(DeclarationExpression expression) {
         visitAnnotations(expression);
         // visit right side first to avoid the usage of a
@@ -396,6 +397,7 @@ public class VariableScopeVisitor extends ClassCodeVisitorSupport {
         }
     }
 
+    @Override
     public void visitVariableExpression(VariableExpression expression) {
         String name = expression.getName();
         Variable v = checkVariableNameForDeclaration(name, expression);
@@ -404,12 +406,14 @@ public class VariableScopeVisitor extends ClassCodeVisitorSupport {
         checkVariableContextAccess(v, expression);
     }
 
+    @Override
     public void visitPropertyExpression(PropertyExpression expression) {
         expression.getObjectExpression().visit(this);
         expression.getProperty().visit(this);
         checkPropertyOnExplicitThis(expression);
     }
 
+    @Override
     public void visitClosureExpression(ClosureExpression expression) {
         pushState();
 
@@ -442,6 +446,7 @@ public class VariableScopeVisitor extends ClassCodeVisitorSupport {
         }
     }
 
+    @Override
     public void visitCatchStatement(CatchStatement statement) {
         pushState();
         Parameter p = statement.getVariable();
@@ -451,6 +456,7 @@ public class VariableScopeVisitor extends ClassCodeVisitorSupport {
         popState();
     }
 
+    @Override
     public void visitFieldExpression(FieldExpression expression) {
         String name = expression.getFieldName();
         //TODO: change that to get the correct scope
@@ -462,6 +468,7 @@ public class VariableScopeVisitor extends ClassCodeVisitorSupport {
     // class visit
     // ------------------------------
 
+    @Override
     public void visitClass(ClassNode node) {
         // AIC are already done, doing them here again will lead to wrong scopes
         if (isAnonymous(node)) return;
@@ -488,6 +495,7 @@ public class VariableScopeVisitor extends ClassCodeVisitorSupport {
         currentScope.setClassScope(node);
     }
 
+    @Override
     protected void visitConstructorOrMethod(MethodNode node, boolean isConstructor) {
         pushState(node.isStatic());
         inConstructor = isConstructor;
@@ -506,6 +514,7 @@ public class VariableScopeVisitor extends ClassCodeVisitorSupport {
         popState();
     }
 
+    @Override
     public void visitMethodCallExpression(MethodCallExpression call) {
         if (call.isImplicitThis() && call.getMethod() instanceof ConstantExpression)
{
             ConstantExpression methodNameConstant = (ConstantExpression) call.getMethod();
@@ -535,6 +544,7 @@ public class VariableScopeVisitor extends ClassCodeVisitorSupport {
         super.visitMethodCallExpression(call);
     }
 
+    @Override
     public void visitConstructorCallExpression(ConstructorCallExpression call) {
         isSpecialConstructorCall = call.isSpecialCall();
         super.visitConstructorCallExpression(call);
@@ -577,28 +587,17 @@ public class VariableScopeVisitor extends ClassCodeVisitorSupport {
         popState();
     }
 
+    @Override
     public void visitProperty(PropertyNode node) {
         pushState(node.isStatic());
         super.visitProperty(node);
         popState();
     }
 
+    @Override
     public void visitField(FieldNode node) {
         pushState(node.isStatic());
         super.visitField(node);
         popState();
     }
-
-    public void visitAnnotations(AnnotatedNode node) {
-        List<AnnotationNode> annotations = node.getAnnotations();
-        if (annotations.isEmpty()) return;
-        for (AnnotationNode an : annotations) {
-            // skip built-in properties
-            if (an.isBuiltIn()) continue;
-            for (Map.Entry<String, Expression> member : an.getMembers().entrySet())
{
-                Expression annMemberValue = member.getValue();
-                annMemberValue.visit(this);
-            }
-        }
-    }
 }
diff --git a/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java b/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
index 0e4fced..b3a9fa6 100644
--- a/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
+++ b/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
@@ -507,7 +507,7 @@ public class ResolveVisitor extends ClassCodeExpressionTransformer {
     private boolean resolveNestedClass(ClassNode type) {
         if (type instanceof ConstructedNestedClass || type instanceof ConstructedClassWithPackage)
return false;
         // we have for example a class name A, are in class X
-        // and there is a nested class A$X. we want to be able 
+        // and there is a nested class A$X. we want to be able
         // to access that class directly, so A becomes a valid
         // name in X.
         // GROOVY-4043: Do this check up the hierarchy, if needed
@@ -527,19 +527,19 @@ public class ResolveVisitor extends ClassCodeExpressionTransformer {
         // nested class A$B$C and want to access B without
         // qualifying it by A.B. A alone will work, since that
         // is the qualified (minus package) name of that class
-        // anyway. 
-        
+        // anyway.
+
         // That means if the current class is not an InnerClassNode
         // there is nothing to be done.
         if (!(currentClass instanceof InnerClassNode)) return false;
-        
-        // since we have B and want to get A we start with the most 
+
+        // since we have B and want to get A we start with the most
         // outer class, put them together and then see if that does
-        // already exist. In case of B from within A$B we are done 
+        // already exist. In case of B from within A$B we are done
         // after the first step already. In case of for example
-        // A.B.C.D.E.F and accessing E from F we test A$E=failed, 
+        // A.B.C.D.E.F and accessing E from F we test A$E=failed,
         // A$B$E=failed, A$B$C$E=fail, A$B$C$D$E=success
-        
+
         LinkedList<ClassNode> outerClasses = new LinkedList<ClassNode>();
         ClassNode outer = currentClass.getOuterClass();
         while (outer!=null) {
@@ -1127,11 +1127,11 @@ public class ResolveVisitor extends ClassCodeExpressionTransformer
{
     protected Expression transformVariableExpression(VariableExpression ve) {
         visitAnnotations(ve);
         Variable v = ve.getAccessedVariable();
-        
+
         if(!(v instanceof DynamicVariable) && !checkingVariableTypeInDeclaration)
{
             /*
-             *  GROOVY-4009: when a normal variable is simply being used, there is no need
to try to 
-             *  resolve its type. Variable type resolve should proceed only if the variable
is being declared. 
+             *  GROOVY-4009: when a normal variable is simply being used, there is no need
to try to
+             *  resolve its type. Variable type resolve should proceed only if the variable
is being declared.
              */
             return ve;
         }
@@ -1312,14 +1312,14 @@ public class ResolveVisitor extends ClassCodeExpressionTransformer
{
     private static String getDescription(ClassNode node) {
         return (node.isInterface() ? "interface" : "class") + " '" + node.getName() + "'";
     }
-    
+
     protected Expression transformMethodCallExpression(MethodCallExpression mce) {
         Expression args = transform(mce.getArguments());
         Expression method = transform(mce.getMethod());
         Expression object = transform(mce.getObjectExpression());
 
         resolveGenericsTypes(mce.getGenericsTypes());
-        
+
         MethodCallExpression result = new MethodCallExpression(object, method, args);
         result.setSafe(mce.isSafe());
         result.setImplicitThis(mce.isImplicitThis());
@@ -1329,7 +1329,7 @@ public class ResolveVisitor extends ClassCodeExpressionTransformer {
         result.setMethodTarget(mce.getMethodTarget());
         return result;
     }
-    
+
     protected Expression transformDeclarationExpression(DeclarationExpression de) {
         visitAnnotations(de);
         Expression oldLeft = de.getLeftExpression();
@@ -1388,8 +1388,8 @@ public class ResolveVisitor extends ClassCodeExpressionTransformer {
                 checkAnnotationMemberValue(adjusted);
             }
             if (annType.isResolved()) {
-                Class annTypeClass = annType.getTypeClass();
-                Retention retAnn = (Retention) annTypeClass.getAnnotation(Retention.class);
+                Class<?> annTypeClass = annType.getTypeClass();
+                Retention retAnn = annTypeClass.getAnnotation(Retention.class);
                 if (retAnn != null && !retAnn.value().equals(RetentionPolicy.SOURCE)
&& !isRepeatable(annTypeClass)) {
                     // remember non-source/non-repeatable annos (auto collecting of Repeatable
annotations is handled elsewhere)
                     AnnotationNode anyPrevAnnNode = tmpAnnotations.put(annTypeClass.getName(),
an);
@@ -1401,7 +1401,7 @@ public class ResolveVisitor extends ClassCodeExpressionTransformer {
         }
     }
 
-    private boolean isRepeatable(Class annTypeClass) {
+    private boolean isRepeatable(Class<?> annTypeClass) {
         Annotation[] annTypeAnnotations = annTypeClass.getAnnotations();
         for (Annotation annTypeAnnotation : annTypeAnnotations) {
             if (annTypeAnnotation.annotationType().getName().equals("java.lang.annotation.Repeatable"))
{


Mime
View raw message