groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject [3/6] groovy git commit: Minor refactoring: remove the duplicated code further
Date Tue, 06 Feb 2018 09:10:28 GMT
Minor refactoring: remove the duplicated code further

(cherry picked from commit 314738a)


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

Branch: refs/heads/GROOVY_2_6_X
Commit: 160cb076144c8a3c69e436cc22910be23a664c76
Parents: 3da3673
Author: sunlan <sunlan@apache.org>
Authored: Tue Feb 6 11:25:39 2018 +0800
Committer: sunlan <sunlan@apache.org>
Committed: Tue Feb 6 17:09:35 2018 +0800

----------------------------------------------------------------------
 .../org/codehaus/groovy/ast/ClassHelper.java    | 11 +--
 .../groovy/classgen/asm/BytecodeHelper.java     | 84 +++++++-----------
 .../classgen/asm/util/TypeDescriptionUtil.java  | 89 ++++++++++++++++++++
 3 files changed, 120 insertions(+), 64 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/160cb076/src/main/java/org/codehaus/groovy/ast/ClassHelper.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/ast/ClassHelper.java b/src/main/java/org/codehaus/groovy/ast/ClassHelper.java
index 98a1ba2..3626242 100644
--- a/src/main/java/org/codehaus/groovy/ast/ClassHelper.java
+++ b/src/main/java/org/codehaus/groovy/ast/ClassHelper.java
@@ -28,6 +28,7 @@ import groovy.lang.MetaClass;
 import groovy.lang.Range;
 import groovy.lang.Reference;
 import groovy.lang.Script;
+import org.codehaus.groovy.classgen.asm.util.TypeDescriptionUtil;
 import org.codehaus.groovy.runtime.GeneratedClosure;
 import org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport;
 import org.codehaus.groovy.transform.trait.Traits;
@@ -321,15 +322,7 @@ public class ClassHelper {
      * @see #make(String)
      */
     public static boolean isPrimitiveType(ClassNode cn) {
-        return cn == boolean_TYPE ||
-                cn == char_TYPE ||
-                cn == byte_TYPE ||
-                cn == short_TYPE ||
-                cn == int_TYPE ||
-                cn == long_TYPE ||
-                cn == float_TYPE ||
-                cn == double_TYPE ||
-                cn == VOID_TYPE;
+        return TypeDescriptionUtil.isPrimitiveType(cn);
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/groovy/blob/160cb076/src/main/java/org/codehaus/groovy/classgen/asm/BytecodeHelper.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/classgen/asm/BytecodeHelper.java b/src/main/java/org/codehaus/groovy/classgen/asm/BytecodeHelper.java
index 7ebdc3a..4b812f1 100644
--- a/src/main/java/org/codehaus/groovy/classgen/asm/BytecodeHelper.java
+++ b/src/main/java/org/codehaus/groovy/classgen/asm/BytecodeHelper.java
@@ -25,6 +25,7 @@ import org.codehaus.groovy.ast.GenericsType;
 import org.codehaus.groovy.ast.MethodNode;
 import org.codehaus.groovy.ast.Parameter;
 import org.codehaus.groovy.ast.decompiled.DecompiledClassNode;
+import org.codehaus.groovy.classgen.asm.util.TypeDescriptionUtil;
 import org.codehaus.groovy.reflection.ReflectionCache;
 import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;
 import org.objectweb.asm.Label;
@@ -33,6 +34,16 @@ import org.objectweb.asm.Opcodes;
 
 import java.lang.reflect.Modifier;
 
+import static org.codehaus.groovy.ast.ClassHelper.VOID_TYPE;
+import static org.codehaus.groovy.ast.ClassHelper.boolean_TYPE;
+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.int_TYPE;
+import static org.codehaus.groovy.ast.ClassHelper.long_TYPE;
+import static org.codehaus.groovy.ast.ClassHelper.short_TYPE;
+
 /**
  * A helper class for bytecode generation with AsmClassGenerator.
  *
@@ -157,27 +168,7 @@ public class BytecodeHelper implements Opcodes {
         while (true) {
             if (ClassHelper.isPrimitiveType(d.redirect())) {
                 d = d.redirect();
-                char car;
-                if (d == ClassHelper.int_TYPE) {
-                    car = 'I';
-                } else if (d == ClassHelper.VOID_TYPE) {
-                    car = 'V';
-                } else if (d == ClassHelper.boolean_TYPE) {
-                    car = 'Z';
-                } else if (d == ClassHelper.byte_TYPE) {
-                    car = 'B';
-                } else if (d == ClassHelper.char_TYPE) {
-                    car = 'C';
-                } else if (d == ClassHelper.short_TYPE) {
-                    car = 'S';
-                } else if (d == ClassHelper.double_TYPE) {
-                    car = 'D';
-                } else if (d == ClassHelper.float_TYPE) {
-                    car = 'F';
-                } else /* long */ {
-                    car = 'J';
-                }
-                buf.append(car);
+                buf.append(TypeDescriptionUtil.getDescriptionByType(d));
                 return buf.toString();
             } else if (d.isArray()) {
                 buf.append('[');
@@ -305,28 +296,11 @@ public class BytecodeHelper implements Opcodes {
         if (name.endsWith("[]")) { // todo need process multi
             prefix = "[";
             name = name.substring(0, name.length() - 2);
-            if (name.equals("int")) {
-                return prefix + "I";
-            } else if (name.equals("long")) {
-                return prefix + "J";
-            } else if (name.equals("short")) {
-                return prefix + "S";
-            } else if (name.equals("float")) {
-                return prefix + "F";
-            } else if (name.equals("double")) {
-                return prefix + "D";
-            } else if (name.equals("byte")) {
-                return prefix + "B";
-            } else if (name.equals("char")) {
-                return prefix + "C";
-            } else if (name.equals("boolean")) {
-                return prefix + "Z";
-            } else {
-                return prefix + "L" + name.replace('/', '.') + ";";
-            }
+
+            return prefix + TypeDescriptionUtil.getDescriptionByName(name) + (TypeDescriptionUtil.isPrimitiveType(name)
? "" : name.replace('/', '.') + ";");
         }
-        return name.replace('/', '.');
 
+        return name.replace('/', '.');
     }
 
     /*public void dup() {
@@ -487,7 +461,7 @@ public class BytecodeHelper implements Opcodes {
 
     public static void doCast(MethodVisitor mv, ClassNode type) {
         if (type == ClassHelper.OBJECT_TYPE) return;
-        if (ClassHelper.isPrimitiveType(type) && type != ClassHelper.VOID_TYPE) {
+        if (ClassHelper.isPrimitiveType(type) && type != VOID_TYPE) {
             unbox(mv, type);
         } else {
             mv.visitTypeInsn(
@@ -639,22 +613,22 @@ public class BytecodeHelper implements Opcodes {
      * @param type primitive type to convert
      */
     public static void convertPrimitiveToBoolean(MethodVisitor mv, ClassNode type) {
-        if (type == ClassHelper.boolean_TYPE) {
+        if (type == boolean_TYPE) {
             return;
         }
         // Special handling is done for floating point types in order to
         // handle checking for 0 or NaN values.
-        if (type == ClassHelper.double_TYPE) {
+        if (type == double_TYPE) {
             convertDoubleToBoolean(mv);
             return;
-        } else if (type == ClassHelper.float_TYPE) {
+        } else if (type == float_TYPE) {
             convertFloatToBoolean(mv);
             return;
         }
         Label trueLabel = new Label();
         Label falseLabel = new Label();
         // Convert long to int for IFEQ comparison using LCMP
-        if (type==ClassHelper.long_TYPE) {
+        if (type== long_TYPE) {
             mv.visitInsn(LCONST_0);
             mv.visitInsn(LCMP);
         }
@@ -846,20 +820,20 @@ public class BytecodeHelper implements Opcodes {
         }
 
         public void handle() {
-            if (type == ClassHelper.double_TYPE) {
+            if (type == double_TYPE) {
                 handleDoubleType();
-            } else if (type == ClassHelper.float_TYPE) {
+            } else if (type == float_TYPE) {
                 handleFloatType();
-            } else if (type == ClassHelper.long_TYPE) {
+            } else if (type == long_TYPE) {
                 handleLongType();
             } else if (
-                    type == ClassHelper.boolean_TYPE
-                            || type == ClassHelper.char_TYPE
-                            || type == ClassHelper.byte_TYPE
-                            || type == ClassHelper.int_TYPE
-                            || type == ClassHelper.short_TYPE) {
+                    type == boolean_TYPE
+                            || type == char_TYPE
+                            || type == byte_TYPE
+                            || type == int_TYPE
+                            || type == short_TYPE) {
                 handleIntType();
-            } else if (type == ClassHelper.VOID_TYPE) {
+            } else if (type == VOID_TYPE) {
                 handleVoidType();
             } else {
                 handleRefType();

http://git-wip-us.apache.org/repos/asf/groovy/blob/160cb076/src/main/java/org/codehaus/groovy/classgen/asm/util/TypeDescriptionUtil.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/classgen/asm/util/TypeDescriptionUtil.java
b/src/main/java/org/codehaus/groovy/classgen/asm/util/TypeDescriptionUtil.java
new file mode 100644
index 0000000..18a6e8d
--- /dev/null
+++ b/src/main/java/org/codehaus/groovy/classgen/asm/util/TypeDescriptionUtil.java
@@ -0,0 +1,89 @@
+/*
+ *  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.codehaus.groovy.classgen.asm.util;
+
+import org.apache.groovy.util.Maps;
+import org.codehaus.groovy.ast.ClassNode;
+
+import java.util.Map;
+
+import static org.codehaus.groovy.ast.ClassHelper.VOID_TYPE;
+import static org.codehaus.groovy.ast.ClassHelper.boolean_TYPE;
+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.int_TYPE;
+import static org.codehaus.groovy.ast.ClassHelper.long_TYPE;
+import static org.codehaus.groovy.ast.ClassHelper.short_TYPE;
+
+public class TypeDescriptionUtil {
+    private static final String REF_DESCRIPTION = "L";
+    private static final Map<ClassNode, String> TYPE_TO_DESCRIPTION_MAP = Maps.of(
+            int_TYPE, "I",
+            VOID_TYPE,"V",
+            boolean_TYPE, "Z",
+            byte_TYPE, "B",
+            char_TYPE, "C",
+            short_TYPE, "S",
+            double_TYPE, "D",
+            float_TYPE, "F",
+            long_TYPE, "J"
+    );
+
+    private static final Map<String, ClassNode> NAME_TO_TYPE_MAP = Maps.of(
+            "int", int_TYPE,
+            "void", VOID_TYPE,
+            "boolean", boolean_TYPE,
+            "byte", byte_TYPE,
+            "char", char_TYPE,
+            "short", short_TYPE,
+            "double", double_TYPE,
+            "float", float_TYPE,
+            "long", long_TYPE
+    );
+
+    public static boolean isPrimitiveType(String name) {
+        return NAME_TO_TYPE_MAP.containsKey(name);
+    }
+
+    public static boolean isPrimitiveType(ClassNode type) {
+        return TYPE_TO_DESCRIPTION_MAP.containsKey(type);
+    }
+
+    public static String getDescriptionByType(ClassNode type) {
+        if (null == type) {
+            return REF_DESCRIPTION;
+        }
+
+        String desc = TYPE_TO_DESCRIPTION_MAP.get(type);
+
+        return null == desc ? REF_DESCRIPTION : desc;
+    }
+
+    public static String getDescriptionByName(String name) {
+        if (null == name) {
+            return REF_DESCRIPTION;
+        }
+
+        return getDescriptionByType(NAME_TO_TYPE_MAP.get(name));
+    }
+
+}


Mime
View raw message