diff --git a/android/guava-tests/test/com/google/common/collect/ObjectArraysTest.java b/android/guava-tests/test/com/google/common/collect/ObjectArraysTest.java index 2f6d757101e4..b46cd467defa 100644 --- a/android/guava-tests/test/com/google/common/collect/ObjectArraysTest.java +++ b/android/guava-tests/test/com/google/common/collect/ObjectArraysTest.java @@ -197,6 +197,24 @@ public void testAppendTwoElements() { assertThat(result).asList().containsExactly("foo", "bar", "baz").inOrder(); } + public void testPrependIncompatibleType_throwsArrayStoreException() { + try { + Object[] unused = ObjectArrays.concat(0, new String[] {"foo"}); + fail("Expected ArrayStoreException"); + } catch (ArrayStoreException expected) { + // Expected behavior: cannot store Integer in String[] + } + } + + public void testAppendIncompatibleType_throwsArrayStoreException() { + try { + Object[] unused = ObjectArrays.concat(new String[] {"foo"}, 0); + fail("Expected ArrayStoreException"); + } catch (ArrayStoreException expected) { + // Expected behavior: cannot store Integer in String[] + } + } + public void testEmptyArrayToEmpty() { doTestNewArrayEquals(new Object[0], 0); } diff --git a/android/guava/src/com/google/common/collect/ObjectArrays.java b/android/guava/src/com/google/common/collect/ObjectArrays.java index df639d0c11b2..9f409bdb2e9e 100644 --- a/android/guava/src/com/google/common/collect/ObjectArrays.java +++ b/android/guava/src/com/google/common/collect/ObjectArrays.java @@ -85,6 +85,8 @@ private ObjectArrays() {} * @param array the array of elements to append * @return an array whose size is one larger than {@code array}, with {@code element} occupying * the first position, and the elements of {@code array} occupying the remaining elements. + * @throws ArrayStoreException if {@code element} is not of a runtime type that can be stored in + * an array of type {@code T[]} */ public static T[] concat(@ParametricNullness T element, T[] array) { T[] result = newArray(array, array.length + 1); @@ -100,6 +102,8 @@ private ObjectArrays() {} * @param element the element to append to the end * @return an array whose size is one larger than {@code array}, with the same contents as {@code * array}, plus {@code element} occupying the last position. + * @throws ArrayStoreException if {@code element} is not of a runtime type that can be stored in + * an array of type {@code T[]} */ public static T[] concat(T[] array, @ParametricNullness T element) { T[] result = Arrays.copyOf(array, array.length + 1); diff --git a/guava-tests/test/com/google/common/collect/ObjectArraysTest.java b/guava-tests/test/com/google/common/collect/ObjectArraysTest.java index 2f6d757101e4..b46cd467defa 100644 --- a/guava-tests/test/com/google/common/collect/ObjectArraysTest.java +++ b/guava-tests/test/com/google/common/collect/ObjectArraysTest.java @@ -197,6 +197,24 @@ public void testAppendTwoElements() { assertThat(result).asList().containsExactly("foo", "bar", "baz").inOrder(); } + public void testPrependIncompatibleType_throwsArrayStoreException() { + try { + Object[] unused = ObjectArrays.concat(0, new String[] {"foo"}); + fail("Expected ArrayStoreException"); + } catch (ArrayStoreException expected) { + // Expected behavior: cannot store Integer in String[] + } + } + + public void testAppendIncompatibleType_throwsArrayStoreException() { + try { + Object[] unused = ObjectArrays.concat(new String[] {"foo"}, 0); + fail("Expected ArrayStoreException"); + } catch (ArrayStoreException expected) { + // Expected behavior: cannot store Integer in String[] + } + } + public void testEmptyArrayToEmpty() { doTestNewArrayEquals(new Object[0], 0); } diff --git a/guava/src/com/google/common/collect/ObjectArrays.java b/guava/src/com/google/common/collect/ObjectArrays.java index 9c619a0147e6..7c529ca97843 100644 --- a/guava/src/com/google/common/collect/ObjectArrays.java +++ b/guava/src/com/google/common/collect/ObjectArrays.java @@ -85,6 +85,8 @@ private ObjectArrays() {} * @param array the array of elements to append * @return an array whose size is one larger than {@code array}, with {@code element} occupying * the first position, and the elements of {@code array} occupying the remaining elements. + * @throws ArrayStoreException if {@code element} is not of a runtime type that can be stored in + * an array of type {@code T[]} */ public static T[] concat(@ParametricNullness T element, T[] array) { T[] result = newArray(array, array.length + 1); @@ -100,6 +102,8 @@ private ObjectArrays() {} * @param element the element to append to the end * @return an array whose size is one larger than {@code array}, with the same contents as {@code * array}, plus {@code element} occupying the last position. + * @throws ArrayStoreException if {@code element} is not of a runtime type that can be stored in + * an array of type {@code T[]} */ public static T[] concat(T[] array, @ParametricNullness T element) { T[] result = Arrays.copyOf(array, array.length + 1);