diff --git a/CHANGELOG.md b/CHANGELOG.md index c61a8b71a..69f165da5 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ * 【core 】 CharUtil.isBlankChar增加\u180e(pr#2738@Github) ### 🐞Bug修复 * 【json 】 修复普通byte数组转JSONArray时的异常(pr#875@Gitee) +* 【core 】 修复ArrayUtil.insert()不支持原始类型数组的问题(pr#874@Gitee) ------------------------------------------------------------------------------------------------------------- diff --git a/hutool-core/src/main/java/cn/hutool/core/util/ArrayUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/ArrayUtil.java index a2e69510d..ef381e820 100755 --- a/hutool-core/src/main/java/cn/hutool/core/util/ArrayUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/ArrayUtil.java @@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.UniqueKeySet; import cn.hutool.core.comparator.CompareUtil; +import cn.hutool.core.convert.Convert; import cn.hutool.core.exceptions.UtilException; import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.Editor; @@ -461,9 +462,16 @@ public class ArrayUtil extends PrimitiveArrayUtil { index = (index % len) + len; } - final T[] result = newArray(array.getClass().getComponentType(), Math.max(len, index) + newElements.length); + // 已有数组的元素类型 + final Class originComponentType = array.getClass().getComponentType(); + Object newEleArr = newElements; + // 如果 已有数组的元素类型是 原始类型,则需要转换 新元素数组 为该类型,避免ArrayStoreException + if (originComponentType.isPrimitive()) { + newEleArr = Convert.convert(array.getClass(), newElements); + } + final Object result = Array.newInstance(originComponentType, Math.max(len, index) + newElements.length); System.arraycopy(array, 0, result, 0, Math.min(len, index)); - System.arraycopy(newElements, 0, result, index, newElements.length); + System.arraycopy(newEleArr, 0, result, index, newElements.length); if (index < len) { System.arraycopy(array, index, result, index + newElements.length, len - index); } diff --git a/hutool-core/src/test/java/cn/hutool/core/util/ArrayUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/util/ArrayUtilTest.java index 3ee9ecf9e..9e47997b2 100755 --- a/hutool-core/src/test/java/cn/hutool/core/util/ArrayUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/util/ArrayUtilTest.java @@ -543,4 +543,33 @@ public class ArrayUtilTest { final String[] c = {"d", "e"}; Assert.assertTrue(ArrayUtil.containsAll(c, resultO[0], resultO[1])); } + + @Test + public void testInsertPrimitive() { + final boolean[] booleans = new boolean[10]; + final byte[] bytes = new byte[10]; + final char[] chars = new char[10]; + final short[] shorts = new short[10]; + final int[] ints = new int[10]; + final long[] longs = new long[10]; + final float[] floats = new float[10]; + final double[] doubles = new double[10]; + + final boolean[] insert1 = (boolean[]) ArrayUtil.insert(booleans, 0, 0, 1, 2); + Assert.assertNotNull(insert1); + final byte[] insert2 = (byte[]) ArrayUtil.insert(bytes, 0, 1, 2, 3); + Assert.assertNotNull(insert2); + final char[] insert3 = (char[]) ArrayUtil.insert(chars, 0, 1, 2, 3); + Assert.assertNotNull(insert3); + final short[] insert4 = (short[]) ArrayUtil.insert(shorts, 0, 1, 2, 3); + Assert.assertNotNull(insert4); + final int[] insert5 = (int[]) ArrayUtil.insert(ints, 0, 1, 2, 3); + Assert.assertNotNull(insert5); + final long[] insert6 = (long[]) ArrayUtil.insert(longs, 0, 1, 2, 3); + Assert.assertNotNull(insert6); + final float[] insert7 = (float[]) ArrayUtil.insert(floats, 0, 1, 2, 3); + Assert.assertNotNull(insert7); + final double[] insert8 = (double[]) ArrayUtil.insert(doubles, 0, 1, 2, 3); + Assert.assertNotNull(insert8); + } }