You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2019/12/31 17:19:03 UTC

[groovy] 02/02: Minor refactoring: simplify `getLength`

This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit b13fa74da65bcb33770217d13b572c265c24fd9c
Author: Daniel Sun <su...@apache.org>
AuthorDate: Wed Jan 1 00:54:20 2020 +0800

    Minor refactoring: simplify `getLength`
---
 .../org/codehaus/groovy/vmplugin/v7/IndyArrayAccess.java | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/vmplugin/v7/IndyArrayAccess.java b/src/main/java/org/codehaus/groovy/vmplugin/v7/IndyArrayAccess.java
index 34205f7..84310a7 100644
--- a/src/main/java/org/codehaus/groovy/vmplugin/v7/IndyArrayAccess.java
+++ b/src/main/java/org/codehaus/groovy/vmplugin/v7/IndyArrayAccess.java
@@ -23,6 +23,7 @@ import org.codehaus.groovy.GroovyBugError;
 import java.lang.invoke.MethodHandle;
 import java.lang.invoke.MethodHandles;
 import java.lang.invoke.MethodType;
+import java.lang.reflect.Array;
 import java.util.HashMap;
 
 /**
@@ -99,16 +100,11 @@ public class IndyArrayAccess {
     }
 
     private static int getLength(Object array) {
-        if (array instanceof Object[]) return ((Object[]) array).length;
-        if (array instanceof boolean[]) return ((boolean[]) array).length;
-        if (array instanceof byte[]) return ((byte[]) array).length;
-        if (array instanceof char[]) return ((char[]) array).length;
-        if (array instanceof short[]) return ((short[]) array).length;
-        if (array instanceof int[]) return ((int[]) array).length;
-        if (array instanceof long[]) return ((long[]) array).length;
-        if (array instanceof float[]) return ((float[]) array).length;
-        if (array instanceof double[]) return ((double[]) array).length;
-        return 0;
+        if (null == array || !array.getClass().isArray()) {
+            return 0;
+        }
+
+        return Array.getLength(array);
     }
 
     private static int normalizeIndex(Object array, int i) {