groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pa...@apache.org
Subject groovy git commit: refactor some duplicated code
Date Wed, 31 Oct 2018 06:53:19 GMT
Repository: groovy
Updated Branches:
  refs/heads/master d87966b1b -> 7170aa505


refactor some duplicated code


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

Branch: refs/heads/master
Commit: 7170aa505c64a73a3b2e4c770167e905b34ac5e8
Parents: d87966b
Author: Paul King <paulk@asert.com.au>
Authored: Wed Oct 31 16:52:45 2018 +1000
Committer: Paul King <paulk@asert.com.au>
Committed: Wed Oct 31 16:53:07 2018 +1000

----------------------------------------------------------------------
 .../apache/groovy/ast/tools/ClassNodeUtils.java | 27 ++++++++
 .../groovy/ast/tools/ExpressionUtils.java       | 73 ++++++++++++++++++++
 .../codehaus/groovy/control/ResolveVisitor.java | 38 +++-------
 .../groovy/control/StaticImportVisitor.java     | 56 +--------------
 4 files changed, 112 insertions(+), 82 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/7170aa50/src/main/java/org/apache/groovy/ast/tools/ClassNodeUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/groovy/ast/tools/ClassNodeUtils.java b/src/main/java/org/apache/groovy/ast/tools/ClassNodeUtils.java
index 7ba0548..814b127 100644
--- a/src/main/java/org/apache/groovy/ast/tools/ClassNodeUtils.java
+++ b/src/main/java/org/apache/groovy/ast/tools/ClassNodeUtils.java
@@ -22,6 +22,7 @@ import groovy.transform.Generated;
 import org.codehaus.groovy.ast.ClassHelper;
 import org.codehaus.groovy.ast.ClassNode;
 import org.codehaus.groovy.ast.ConstructorNode;
+import org.codehaus.groovy.ast.FieldNode;
 import org.codehaus.groovy.ast.MethodNode;
 import org.codehaus.groovy.ast.Parameter;
 import org.codehaus.groovy.ast.PropertyNode;
@@ -35,9 +36,11 @@ import org.codehaus.groovy.transform.AbstractASTTransformation;
 import java.lang.reflect.Modifier;
 import java.util.Arrays;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
+import java.util.Set;
 
 import static org.apache.groovy.ast.tools.AnnotatedNodeUtils.hasAnnotation;
 import static org.apache.groovy.ast.tools.AnnotatedNodeUtils.markAsGenerated;
@@ -373,4 +376,28 @@ public class ClassNodeUtils {
         return Objects.equals(first.getPackageName(), second.getPackageName());
     }
 
+    /**
+     * Return the (potentially inherited) field of the classnode.
+     *
+     * @param classNode the classnode
+     * @param fieldName the name of the field
+     * @return the field or null if not found
+     */
+    public static FieldNode getField(ClassNode classNode, String fieldName) {
+        ClassNode node = classNode;
+        Set<String> visited = new HashSet<String>();
+        while (node != null) {
+            FieldNode fn = node.getDeclaredField(fieldName);
+            if (fn != null) return fn;
+            ClassNode[] interfaces = node.getInterfaces();
+            for (ClassNode iNode : interfaces) {
+                if (visited.contains(iNode.getName())) continue;
+                FieldNode ifn = getField(iNode, fieldName);
+                visited.add(iNode.getName());
+                if (ifn != null) return ifn;
+            }
+            node = node.getSuperClass();
+        }
+        return null;
+    }
 }

http://git-wip-us.apache.org/repos/asf/groovy/blob/7170aa50/src/main/java/org/apache/groovy/ast/tools/ExpressionUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/groovy/ast/tools/ExpressionUtils.java b/src/main/java/org/apache/groovy/ast/tools/ExpressionUtils.java
new file mode 100644
index 0000000..c3d9737
--- /dev/null
+++ b/src/main/java/org/apache/groovy/ast/tools/ExpressionUtils.java
@@ -0,0 +1,73 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.groovy.ast.tools;
+
+import org.codehaus.groovy.ast.ClassNode;
+import org.codehaus.groovy.ast.FieldNode;
+import org.codehaus.groovy.ast.expr.ClassExpression;
+import org.codehaus.groovy.ast.expr.ConstantExpression;
+import org.codehaus.groovy.ast.expr.Expression;
+import org.codehaus.groovy.ast.expr.ListExpression;
+import org.codehaus.groovy.ast.expr.PropertyExpression;
+
+public class ExpressionUtils {
+    private ExpressionUtils() {
+
+    }
+
+    // resolve constant-looking expressions statically (do here as gets transformed away
later)
+    public static Expression transformInlineConstants(final Expression exp) {
+        if (exp instanceof PropertyExpression) {
+            PropertyExpression pe = (PropertyExpression) exp;
+            if (pe.getObjectExpression() instanceof ClassExpression) {
+                ClassExpression ce = (ClassExpression) pe.getObjectExpression();
+                ClassNode type = ce.getType();
+                if (type.isEnum()) return exp;
+                Expression constant = findConstant(ClassNodeUtils.getField(type, pe.getPropertyAsString()));
+                if (constant != null) return constant;
+            }
+        } else if (exp instanceof ListExpression) {
+            ListExpression origList = (ListExpression) exp;
+            ListExpression newList = new ListExpression();
+            boolean changed = false;
+            for (Expression e : origList.getExpressions()) {
+                Expression transformed = transformInlineConstants(e);
+                newList.addExpression(transformed);
+                if (transformed != e) changed = true;
+            }
+            if (changed) {
+                newList.setSourcePosition(origList);
+                return newList;
+            }
+            return origList;
+        }
+
+        return exp;
+    }
+
+    private static Expression findConstant(FieldNode fn) {
+        if (fn != null && !fn.isEnum() && fn.isStatic() && fn.isFinal())
{
+            if (fn.getInitialValueExpression() instanceof ConstantExpression) {
+                return fn.getInitialValueExpression();
+            }
+        }
+        return null;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/groovy/blob/7170aa50/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java b/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
index db0e4e4..5492d1a 100644
--- a/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
+++ b/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
@@ -19,6 +19,7 @@
 package org.codehaus.groovy.control;
 
 import groovy.lang.Tuple2;
+import org.apache.groovy.ast.tools.ExpressionUtils;
 import org.codehaus.groovy.GroovyBugError;
 import org.codehaus.groovy.ast.ASTNode;
 import org.codehaus.groovy.ast.AnnotatedNode;
@@ -80,6 +81,7 @@ import java.util.Set;
 import static org.codehaus.groovy.ast.GenericsType.GenericsTypeName;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.inSamePackage;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.isDefaultVisibility;
+
 /**
  * Visitor to resolve Types and convert VariableExpression to
  * ClassExpressions if needed. The ResolveVisitor will try to
@@ -1253,9 +1255,9 @@ public class ResolveVisitor extends ClassCodeExpressionTransformer {
             resolveOrFail(annType, ",  unable to find class for annotation", an);
             for (Map.Entry<String, Expression> member : an.getMembers().entrySet())
{
                 Expression newValue = transform(member.getValue());
-                newValue = transformInlineConstants(newValue);
-                member.setValue(newValue);
-                checkAnnotationMemberValue(newValue);
+                Expression adjusted = transformInlineConstants(newValue);
+                member.setValue(adjusted);
+                checkAnnotationMemberValue(adjusted);
             }
             if (annType.isResolved()) {
                 Class annTypeClass = annType.getTypeClass();
@@ -1281,31 +1283,9 @@ public class ResolveVisitor extends ClassCodeExpressionTransformer
{
         return false;
     }
 
-    // resolve constant-looking expressions statically (do here as gets transformed away
later)
-    private Expression transformInlineConstants(Expression exp) {
-        if (exp instanceof PropertyExpression) {
-            PropertyExpression pe = (PropertyExpression) exp;
-            if (pe.getObjectExpression() instanceof ClassExpression) {
-                ClassExpression ce = (ClassExpression) pe.getObjectExpression();
-                ClassNode type = ce.getType();
-                if (type.isEnum())
-                    return exp;
-
-                FieldNode fn = type.getField(pe.getPropertyAsString());
-                if (fn != null && !fn.isEnum() && fn.isStatic() &&
fn.isFinal()) {
-                    if (fn.getInitialValueExpression() instanceof ConstantExpression) {
-                        return fn.getInitialValueExpression();
-                    }
-                }
-            }
-        } else if (exp instanceof ListExpression) {
-            ListExpression le = (ListExpression) exp;
-            ListExpression result = new ListExpression();
-            for (Expression e : le.getExpressions()) {
-                result.addExpression(transformInlineConstants(e));
-            }
-            return result;
-        } else if (exp instanceof AnnotationConstantExpression) {
+    // resolve constant-looking expressions statically (do here as they get transformed away
later)
+    private static Expression transformInlineConstants(final Expression exp) {
+        if (exp instanceof AnnotationConstantExpression) {
             ConstantExpression ce = (ConstantExpression) exp;
             if (ce.getValue() instanceof AnnotationNode) {
                 // replicate a little bit of AnnotationVisitor here
@@ -1316,6 +1296,8 @@ public class ResolveVisitor extends ClassCodeExpressionTransformer {
                 }
 
             }
+        } else {
+            return ExpressionUtils.transformInlineConstants(exp);
         }
         return exp;
     }

http://git-wip-us.apache.org/repos/asf/groovy/blob/7170aa50/src/main/java/org/codehaus/groovy/control/StaticImportVisitor.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/control/StaticImportVisitor.java b/src/main/java/org/codehaus/groovy/control/StaticImportVisitor.java
index 9adda6e..3283d2b 100644
--- a/src/main/java/org/codehaus/groovy/control/StaticImportVisitor.java
+++ b/src/main/java/org/codehaus/groovy/control/StaticImportVisitor.java
@@ -38,7 +38,6 @@ import org.codehaus.groovy.ast.expr.ConstantExpression;
 import org.codehaus.groovy.ast.expr.ConstructorCallExpression;
 import org.codehaus.groovy.ast.expr.EmptyExpression;
 import org.codehaus.groovy.ast.expr.Expression;
-import org.codehaus.groovy.ast.expr.ListExpression;
 import org.codehaus.groovy.ast.expr.MapEntryExpression;
 import org.codehaus.groovy.ast.expr.MethodCallExpression;
 import org.codehaus.groovy.ast.expr.NamedArgumentListExpression;
@@ -49,10 +48,8 @@ import org.codehaus.groovy.ast.expr.VariableExpression;
 import org.codehaus.groovy.ast.stmt.Statement;
 import org.codehaus.groovy.syntax.Types;
 
-import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
-import java.util.Set;
 
 import static org.apache.groovy.ast.tools.ClassNodeUtils.getPropNameForAccessor;
 import static org.apache.groovy.ast.tools.ClassNodeUtils.hasPossibleStaticMethod;
@@ -60,6 +57,8 @@ import static org.apache.groovy.ast.tools.ClassNodeUtils.hasPossibleStaticProper
 import static org.apache.groovy.ast.tools.ClassNodeUtils.hasStaticProperty;
 import static org.apache.groovy.ast.tools.ClassNodeUtils.isInnerClass;
 import static org.apache.groovy.ast.tools.ClassNodeUtils.isValidAccessorName;
+import static org.apache.groovy.ast.tools.ClassNodeUtils.getField;
+import static org.apache.groovy.ast.tools.ExpressionUtils.transformInlineConstants;
 import static org.apache.groovy.util.BeanUtils.capitalize;
 
 /**
@@ -236,39 +235,6 @@ public class StaticImportVisitor extends ClassCodeExpressionTransformer
{
         }
     }
 
-    // resolve constant-looking expressions statically (do here as gets transformed away
later)
-
-    private Expression transformInlineConstants(Expression exp) {
-        if (exp instanceof PropertyExpression) {
-            PropertyExpression pe = (PropertyExpression) exp;
-            if (pe.getObjectExpression() instanceof ClassExpression) {
-                ClassExpression ce = (ClassExpression) pe.getObjectExpression();
-                ClassNode type = ce.getType();
-                if (type.isEnum()) return exp;
-                Expression constant = findConstant(getField(type, pe.getPropertyAsString()));
-                if (constant != null) return constant;
-            }
-        } else if (exp instanceof ListExpression) {
-            ListExpression le = (ListExpression) exp;
-            ListExpression result = new ListExpression();
-            for (Expression e : le.getExpressions()) {
-                result.addExpression(transformInlineConstants(e));
-            }
-            return result;
-        }
-
-        return exp;
-    }
-
-    private static Expression findConstant(FieldNode fn) {
-        if (fn != null && !fn.isEnum() && fn.isStatic() && fn.isFinal())
{
-            if (fn.getInitialValueExpression() instanceof ConstantExpression) {
-                return fn.getInitialValueExpression();
-            }
-        }
-        return null;
-    }
-
     protected Expression transformMethodCallExpression(MethodCallExpression mce) {
         Expression args = transform(mce.getArguments());
         Expression method = transform(mce.getMethod());
@@ -597,24 +563,6 @@ public class StaticImportVisitor extends ClassCodeExpressionTransformer
{
         return null;
     }
 
-    private static FieldNode getField(ClassNode classNode, String fieldName) {
-        ClassNode node = classNode;
-        Set<String> visited = new HashSet<String>();
-        while (node != null) {
-            FieldNode fn = node.getDeclaredField(fieldName);
-            if (fn != null) return fn;
-            ClassNode[] interfaces = node.getInterfaces();
-            for (ClassNode iNode : interfaces) {
-                if (visited.contains(iNode.getName())) continue;
-                FieldNode ifn = getField(iNode, fieldName);
-                visited.add(iNode.getName());
-                if (ifn != null) return ifn;
-            }
-            node = node.getSuperClass();
-        }
-        return null;
-    }
-
     private static Expression findStaticMethod(ClassNode staticImportType, String methodName,
Expression args) {
         if (staticImportType.isPrimaryClassNode() || staticImportType.isResolved()) {
             if (staticImportType.hasPossibleStaticMethod(methodName, args)) {


Mime
View raw message