groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject groovy git commit: Trivial refactoring: remove duplicated code
Date Sat, 03 Feb 2018 13:05:43 GMT
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_6_X a7bdf0aa8 -> 6e90f6d5f


Trivial refactoring: remove duplicated code

(cherry picked from commit 04e3099)


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

Branch: refs/heads/GROOVY_2_6_X
Commit: 6e90f6d5f27d60b87da70f90f209fe5287bcac79
Parents: a7bdf0a
Author: Daniel Sun <realbluesun@hotmail.com>
Authored: Sat Feb 3 20:17:28 2018 +0800
Committer: Daniel Sun <realbluesun@hotmail.com>
Committed: Sat Feb 3 21:04:13 2018 +0800

----------------------------------------------------------------------
 .../groovy/ast/tools/GenericsUtils.java         | 44 ++++++++++++--------
 .../stc/StaticTypeCheckingSupport.java          | 39 ++++++++---------
 2 files changed, 44 insertions(+), 39 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/6e90f6d5/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 a7cf7c0..642f90c 100644
--- a/src/main/java/org/codehaus/groovy/ast/tools/GenericsUtils.java
+++ b/src/main/java/org/codehaus/groovy/ast/tools/GenericsUtils.java
@@ -38,7 +38,6 @@ import org.codehaus.groovy.control.ResolveVisitor;
 import org.codehaus.groovy.control.SourceUnit;
 import org.codehaus.groovy.syntax.ParserException;
 import org.codehaus.groovy.syntax.Reduction;
-import org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport;
 
 import java.io.StringReader;
 import java.util.ArrayList;
@@ -48,6 +47,9 @@ import java.util.List;
 import java.util.Map;
 import java.util.concurrent.atomic.AtomicReference;
 
+import static org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.getCorrectedClassNode;
+import static org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.implementsInterfaceOrIsSubclassOf;
+
 /**
  * Utility methods to deal with generic types.
  *
@@ -56,6 +58,7 @@ import java.util.concurrent.atomic.AtomicReference;
  */
 public class GenericsUtils {
     public static final GenericsType[] EMPTY_GENERICS_ARRAY = new GenericsType[0];
+    public static final String JAVA_LANG_OBJECT = "java.lang.Object";
 
     /**
      * Given a parameterized type and a generic type information, aligns actual type parameters.
For example, if a
@@ -213,7 +216,7 @@ public class GenericsUtils {
             }
             return target;
         }
-        if (!target.equals(hint) && StaticTypeCheckingSupport.implementsInterfaceOrIsSubclassOf(target,
hint)) {
+        if (!target.equals(hint) && implementsInterfaceOrIsSubclassOf(target, hint))
{
             ClassNode nextSuperClass = ClassHelper.getNextSuperClass(target, hint);
             if (!hint.equals(nextSuperClass)) {
                 Map<String, ClassNode> genericsSpec = createGenericsSpec(hint);
@@ -425,40 +428,45 @@ public class GenericsUtils {
         return ret;
     }
 
-    public static void extractSuperClassGenerics(ClassNode type, ClassNode target, Map<String,ClassNode>
spec) {
+    public static void extractSuperClassGenerics(ClassNode type, ClassNode target, Map<String,
ClassNode> spec) {
         // TODO: this method is very similar to StaticTypesCheckingSupport#extractGenericsConnections,
         // but operates on ClassNodes instead of GenericsType
-        if (target==null || type==target) return;
+        if (target == null || type == target) return;
         if (type.isArray() && target.isArray()) {
             extractSuperClassGenerics(type.getComponentType(), target.getComponentType(),
spec);
-        } else if (type.isArray() && target.getName().equals("java.lang.Object"))
{
+        } else if (type.isArray() && JAVA_LANG_OBJECT.equals(target.getName())) {
             // Object is superclass of arrays but no generics involved
-        } else if (target.isGenericsPlaceHolder() || type.equals(target) || !StaticTypeCheckingSupport.implementsInterfaceOrIsSubclassOf(type,
target)) {
+        } else if (target.isGenericsPlaceHolder() || type.equals(target) || !implementsInterfaceOrIsSubclassOf(type,
target)) {
             // structural match route
             if (target.isGenericsPlaceHolder()) {
-                spec.put(target.getGenericsTypes()[0].getName(),type);
+                spec.put(target.getGenericsTypes()[0].getName(), type);
             } else {
                 extractSuperClassGenerics(type.getGenericsTypes(), target.getGenericsTypes(),
spec);
             }
         } else {
             // have first to find matching super class or interface
-            Map <String,ClassNode> genSpec = createGenericsSpec(type);
-            ClassNode superClass = ClassHelper.getNextSuperClass(type,target);
+            ClassNode superClass = getSuperClass(type, target);
 
-            if (superClass == null) {
-                if (ClassHelper.isPrimitiveType(type)) {
-                    superClass = ClassHelper.getNextSuperClass(ClassHelper.getWrapper(type),
target);
-                }
-            }
-
-            if (superClass!=null){
-                ClassNode corrected = GenericsUtils.correctToGenericsSpecRecurse(genSpec,
superClass);
+            if (superClass != null) {
+                ClassNode corrected = getCorrectedClassNode(type, superClass, false);
                 extractSuperClassGenerics(corrected, target, spec);
             } else {
                 // if we reach here, we have an unhandled case 
-                throw new GroovyBugError("The type "+type+" seems not to normally extend
"+target+". Sorry, I cannot handle this.");
+                throw new GroovyBugError("The type " + type + " seems not to normally extend
" + target + ". Sorry, I cannot handle this.");
+            }
+        }
+    }
+
+    public static ClassNode getSuperClass(ClassNode type, ClassNode target) {
+        ClassNode superClass = ClassHelper.getNextSuperClass(type, target);
+
+        if (superClass == null) {
+            if (ClassHelper.isPrimitiveType(type)) {
+                superClass = ClassHelper.getNextSuperClass(ClassHelper.getWrapper(type),
target);
             }
         }
+
+        return superClass;
     }
 
     private static void extractSuperClassGenerics(GenericsType[] usage, GenericsType[] declaration,
Map<String, ClassNode> spec) {

http://git-wip-us.apache.org/repos/asf/groovy/blob/6e90f6d5/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 126cc41..5f0e983 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
@@ -20,7 +20,6 @@
 package org.codehaus.groovy.transform.stc;
 
 import org.codehaus.groovy.GroovyBugError;
-import org.codehaus.groovy.ast.ClassHelper;
 import org.codehaus.groovy.ast.ClassNode;
 import org.codehaus.groovy.ast.GenericsType;
 import org.codehaus.groovy.ast.MethodNode;
@@ -96,7 +95,6 @@ import static org.codehaus.groovy.ast.ClassHelper.byte_TYPE;
 import static org.codehaus.groovy.ast.ClassHelper.char_TYPE;
 import static org.codehaus.groovy.ast.ClassHelper.double_TYPE;
 import static org.codehaus.groovy.ast.ClassHelper.float_TYPE;
-import static org.codehaus.groovy.ast.ClassHelper.getNextSuperClass;
 import static org.codehaus.groovy.ast.ClassHelper.getUnwrapper;
 import static org.codehaus.groovy.ast.ClassHelper.getWrapper;
 import static org.codehaus.groovy.ast.ClassHelper.int_TYPE;
@@ -108,6 +106,7 @@ import static org.codehaus.groovy.ast.ClassHelper.make;
 import static org.codehaus.groovy.ast.ClassHelper.makeWithoutCaching;
 import static org.codehaus.groovy.ast.ClassHelper.short_TYPE;
 import static org.codehaus.groovy.ast.ClassHelper.void_WRAPPER_TYPE;
+import static org.codehaus.groovy.ast.tools.GenericsUtils.getSuperClass;
 import static org.codehaus.groovy.syntax.Types.ASSIGN;
 import static org.codehaus.groovy.syntax.Types.BITWISE_AND;
 import static org.codehaus.groovy.syntax.Types.BITWISE_AND_EQUAL;
@@ -1754,43 +1753,41 @@ public abstract class StaticTypeCheckingSupport {
      * Should the target not have any generics this method does nothing.
      */
     static void extractGenericsConnections(Map<String, GenericsType> connections, ClassNode
type, ClassNode target) {
-        if (target==null || type==target || !isUsingGenericsOrIsArrayUsingGenerics(target))
return;
-        if (type == null || type==UNKNOWN_PARAMETER_TYPE) return;
+        if (target == null || type == target || !isUsingGenericsOrIsArrayUsingGenerics(target))
return;
+        if (type == null || type == UNKNOWN_PARAMETER_TYPE) return;
         if (type.isArray() && target.isArray()) {
             extractGenericsConnections(connections, type.getComponentType(), target.getComponentType());
         } else if (target.isGenericsPlaceHolder() || type.equals(target) || !implementsInterfaceOrIsSubclassOf(type,
target)) {
             // structural match route
             if (target.isGenericsPlaceHolder()) {
-                connections.put(target.getGenericsTypes()[0].getName(),new GenericsType(type));
+                connections.put(target.getGenericsTypes()[0].getName(), new GenericsType(type));
             } else {
                 extractGenericsConnections(connections, type.getGenericsTypes(), target.getGenericsTypes());
             }
         } else {
             // have first to find matching super class or interface
-            Map <String,ClassNode> genSpec = GenericsUtils.createGenericsSpec(type);
-            ClassNode superClass = getNextSuperClass(type,target);
+            ClassNode superClass = getSuperClass(type, target);
 
-            if (superClass == null) {
-                if (ClassHelper.isPrimitiveType(type)) {
-                    superClass = ClassHelper.getNextSuperClass(ClassHelper.getWrapper(type),
target);
-                }
-            }
-
-            if (superClass!=null){
-                ClassNode corrected;
-                if (missesGenericsTypes(type)) {
-                    corrected = superClass.getPlainNodeReference();
-                } else {
-                    corrected = GenericsUtils.correctToGenericsSpecRecurse(genSpec, superClass);
-                }
+            if (superClass != null) {
+                ClassNode corrected = getCorrectedClassNode(type, superClass, true);
                 extractGenericsConnections(connections, corrected, target);
             } else {
                 // if we reach here, we have an unhandled case 
-                throw new GroovyBugError("The type "+type+" seems not to normally extend
"+target+". Sorry, I cannot handle this.");
+                throw new GroovyBugError("The type " + type + " seems not to normally extend
" + target + ". Sorry, I cannot handle this.");
             }
         }
     }
 
+    public static ClassNode getCorrectedClassNode(ClassNode type, ClassNode superClass, boolean
handlingGenerics) {
+        ClassNode corrected;
+        if (handlingGenerics && missesGenericsTypes(type)) {
+            corrected = superClass.getPlainNodeReference();
+        } else {
+            corrected = GenericsUtils.correctToGenericsSpecRecurse(GenericsUtils.createGenericsSpec(type),
superClass);
+        }
+        return corrected;
+    }
+
     private static void extractGenericsConnections(Map<String, GenericsType> connections,
GenericsType[] usage, GenericsType[] declaration) {
         // if declaration does not provide generics, there is no connection to make 
         if (usage==null || declaration==null || declaration.length==0) return;


Mime
View raw message