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: move generics helper methods to `GenericsUtils`
Date Mon, 28 May 2018 00:23:16 GMT
Repository: groovy
Updated Branches:
  refs/heads/master 72e530db3 -> 968da9edc


Minor refactoring: move generics helper methods to `GenericsUtils`


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

Branch: refs/heads/master
Commit: 968da9edc79ca5b0545118d9a671af65b57bbd78
Parents: 72e530d
Author: sunlan <sunlan@apache.org>
Authored: Mon May 28 08:23:03 2018 +0800
Committer: sunlan <sunlan@apache.org>
Committed: Mon May 28 08:23:03 2018 +0800

----------------------------------------------------------------------
 .../codehaus/groovy/ast/tools/GeneralUtils.java | 51 -------------------
 .../groovy/ast/tools/GenericsUtils.java         | 52 ++++++++++++++++++++
 .../stc/StaticTypeCheckingSupport.java          |  5 +-
 .../stc/StaticTypeCheckingVisitor.java          |  4 +-
 4 files changed, 56 insertions(+), 56 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/968da9ed/src/main/java/org/codehaus/groovy/ast/tools/GeneralUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/ast/tools/GeneralUtils.java b/src/main/java/org/codehaus/groovy/ast/tools/GeneralUtils.java
index c0082bf..0fa8063 100644
--- a/src/main/java/org/codehaus/groovy/ast/tools/GeneralUtils.java
+++ b/src/main/java/org/codehaus/groovy/ast/tools/GeneralUtils.java
@@ -24,7 +24,6 @@ import org.codehaus.groovy.ast.AnnotationNode;
 import org.codehaus.groovy.ast.ClassHelper;
 import org.codehaus.groovy.ast.ClassNode;
 import org.codehaus.groovy.ast.FieldNode;
-import org.codehaus.groovy.ast.GenericsType;
 import org.codehaus.groovy.ast.MethodNode;
 import org.codehaus.groovy.ast.PackageNode;
 import org.codehaus.groovy.ast.Parameter;
@@ -69,7 +68,6 @@ import org.codehaus.groovy.transform.AbstractASTTransformation;
 import java.lang.reflect.Modifier;
 import java.util.ArrayList;
 import java.util.Collections;
-import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
@@ -879,53 +877,4 @@ public class GeneralUtils {
                         firstPackage != null && secondPackage != null &&
firstPackage.getName().equals(secondPackage.getName()));
     }
 
-    /**
-     * map declaring generics type to actual generics type, e.g. GROOVY-7204:
-     * declaring generics types:      T,      S extends Serializable
-     * actual generics types   : String,      Long
-     *
-     * the result map is [
-     *  T: String,
-     *  S: Long
-     * ]
-     *
-     * The resolved types can not help us to choose methods correctly if the argument is
a string:  T: Object, S: Serializable
-     * so we need actual types:  T: String, S: Long
-     */
-    public static Map<GenericsType, GenericsType> makeDeclaringAndActualGenericsTypeMap(ClassNode
declaringClass, ClassNode actualReceiver) {
-        ClassNode parameterizedType = GenericsUtils.findParameterizedTypeFromCache(declaringClass,
actualReceiver);
-
-        if (null == parameterizedType) {
-            return Collections.emptyMap();
-        }
-
-        GenericsType[] declaringGenericsTypes = declaringClass.getGenericsTypes();
-        GenericsType[] actualGenericsTypes = parameterizedType.getGenericsTypes();
-
-        Map<GenericsType, GenericsType> result = new LinkedHashMap<>();
-        for (int i = 0, n = declaringGenericsTypes.length; i < n; i++) {
-            result.put(declaringGenericsTypes[i], actualGenericsTypes[i]);
-        }
-
-        return result;
-    }
-
-    /**
-     * Get the actual type according to the placeholder name
-     *
-     * @param placeholderName the placeholder name, e.g. T, E
-     * @param genericsPlaceholderAndTypeMap the result of {@link #makeDeclaringAndActualGenericsTypeMap(ClassNode,
ClassNode}
-     * @return the actual type
-     */
-    public static ClassNode findActualTypeByGenericsPlaceholderName(String placeholderName,
Map<GenericsType, GenericsType> genericsPlaceholderAndTypeMap) {
-        for (Map.Entry<GenericsType, GenericsType> entry : genericsPlaceholderAndTypeMap.entrySet())
{
-            GenericsType declaringGenericsType = entry.getKey();
-
-            if (placeholderName.equals(declaringGenericsType.getName())) {
-                return entry.getValue().getType().redirect();
-            }
-        }
-
-        return null;
-    }
 }

http://git-wip-us.apache.org/repos/asf/groovy/blob/968da9ed/src/main/java/org/codehaus/groovy/ast/tools/GenericsUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/ast/tools/GenericsUtils.java b/src/main/java/org/codehaus/groovy/ast/tools/GenericsUtils.java
index 3d99e53..606cf63 100644
--- a/src/main/java/org/codehaus/groovy/ast/tools/GenericsUtils.java
+++ b/src/main/java/org/codehaus/groovy/ast/tools/GenericsUtils.java
@@ -45,6 +45,7 @@ import java.io.StringReader;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
@@ -734,6 +735,57 @@ public class GenericsUtils {
     }
 
     private static final EvictableCache<ParameterizedTypeCacheKey, ClassNode> PARAMETERIZED_TYPE_CACHE
= new StampedCommonCache<>(128);
+
+    /**
+     * map declaring generics type to actual generics type, e.g. GROOVY-7204:
+     * declaring generics types:      T,      S extends Serializable
+     * actual generics types   : String,      Long
+     *
+     * the result map is [
+     *  T: String,
+     *  S: Long
+     * ]
+     *
+     * The resolved types can not help us to choose methods correctly if the argument is
a string:  T: Object, S: Serializable
+     * so we need actual types:  T: String, S: Long
+     */
+    public static Map<GenericsType, GenericsType> makeDeclaringAndActualGenericsTypeMap(ClassNode
declaringClass, ClassNode actualReceiver) {
+        ClassNode parameterizedType = findParameterizedTypeFromCache(declaringClass, actualReceiver);
+
+        if (null == parameterizedType) {
+            return Collections.emptyMap();
+        }
+
+        GenericsType[] declaringGenericsTypes = declaringClass.getGenericsTypes();
+        GenericsType[] actualGenericsTypes = parameterizedType.getGenericsTypes();
+
+        Map<GenericsType, GenericsType> result = new LinkedHashMap<>();
+        for (int i = 0, n = declaringGenericsTypes.length; i < n; i++) {
+            result.put(declaringGenericsTypes[i], actualGenericsTypes[i]);
+        }
+
+        return result;
+    }
+
+    /**
+     * Get the actual type according to the placeholder name
+     *
+     * @param placeholderName the placeholder name, e.g. T, E
+     * @param genericsPlaceholderAndTypeMap the result of {@link #makeDeclaringAndActualGenericsTypeMap(ClassNode,
ClassNode}
+     * @return the actual type
+     */
+    public static ClassNode findActualTypeByGenericsPlaceholderName(String placeholderName,
Map<GenericsType, GenericsType> genericsPlaceholderAndTypeMap) {
+        for (Map.Entry<GenericsType, GenericsType> entry : genericsPlaceholderAndTypeMap.entrySet())
{
+            GenericsType declaringGenericsType = entry.getKey();
+
+            if (placeholderName.equals(declaringGenericsType.getName())) {
+                return entry.getValue().getType().redirect();
+            }
+        }
+
+        return null;
+    }
+
     private static class ParameterizedTypeCacheKey {
         private ClassNode genericsClass;
         private ClassNode actualType;

http://git-wip-us.apache.org/repos/asf/groovy/blob/968da9ed/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
index c5ee2fe..a195ed7 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
@@ -36,7 +36,6 @@ import org.codehaus.groovy.ast.expr.ListExpression;
 import org.codehaus.groovy.ast.expr.MapExpression;
 import org.codehaus.groovy.ast.expr.VariableExpression;
 import org.codehaus.groovy.ast.stmt.ReturnStatement;
-import org.codehaus.groovy.ast.tools.GeneralUtils;
 import org.codehaus.groovy.ast.tools.GenericsUtils;
 import org.codehaus.groovy.ast.tools.ParameterUtils;
 import org.codehaus.groovy.ast.tools.WideningCategories;
@@ -1100,7 +1099,7 @@ public abstract class StaticTypeCheckingSupport {
                 Person p = foo(b)
              */
 
-            Map<GenericsType, GenericsType> declaringAndActualGenericsTypeMap = GeneralUtils.makeDeclaringAndActualGenericsTypeMap(declaringClassForDistance,
actualReceiverForDistance);
+            Map<GenericsType, GenericsType> declaringAndActualGenericsTypeMap = GenericsUtils.makeDeclaringAndActualGenericsTypeMap(declaringClassForDistance,
actualReceiverForDistance);
             Parameter[] params = makeRawTypes(safeNode.getParameters(), declaringAndActualGenericsTypeMap);
             int dist = measureParametersAndArgumentsDistance(params, safeArgs);
             if (dist >= 0) {
@@ -1197,7 +1196,7 @@ public abstract class StaticTypeCheckingSupport {
         for (int i = 0; i < params.length; i++) {
             Parameter oldP = params[i];
 
-            ClassNode actualType = GeneralUtils.findActualTypeByGenericsPlaceholderName(oldP.getType().getUnresolvedName(),
genericsPlaceholderAndTypeMap);
+            ClassNode actualType = GenericsUtils.findActualTypeByGenericsPlaceholderName(oldP.getType().getUnresolvedName(),
genericsPlaceholderAndTypeMap);
             Parameter newP = new Parameter(makeRawType(null == actualType ? oldP.getType()
: actualType), oldP.getName());
             newParam[i] = newP;
         }

http://git-wip-us.apache.org/repos/asf/groovy/blob/968da9ed/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
----------------------------------------------------------------------
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 6300deb..e27c6a9 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -169,9 +169,9 @@ import static org.codehaus.groovy.ast.tools.GeneralUtils.args;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.binX;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.callX;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.castX;
-import static org.codehaus.groovy.ast.tools.GeneralUtils.findActualTypeByGenericsPlaceholderName;
-import static org.codehaus.groovy.ast.tools.GeneralUtils.makeDeclaringAndActualGenericsTypeMap;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.varX;
+import static org.codehaus.groovy.ast.tools.GenericsUtils.findActualTypeByGenericsPlaceholderName;
+import static org.codehaus.groovy.ast.tools.GenericsUtils.makeDeclaringAndActualGenericsTypeMap;
 import static org.codehaus.groovy.ast.tools.GenericsUtils.toGenericTypesString;
 import static org.codehaus.groovy.ast.tools.WideningCategories.LowestUpperBoundClassNode;
 import static org.codehaus.groovy.ast.tools.WideningCategories.isBigDecCategory;


Mime
View raw message