Skip to content

Commit b533932

Browse files
committed
F handle null input to ArrayUtils.combine
1 parent 36e7e89 commit b533932

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

approvaltests-util-tests/src/test/java/com/spun/util/ArrayUtilsTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public void testCombineArrays()
4343
Integer[] ints = new Integer[]{5, 6, 7};
4444
assertEquals("[1, 5, 6, 7]", Arrays.toString(ArrayUtils.combine(1, ints)));
4545
assertEquals("[1]", Arrays.toString(ArrayUtils.combine(1)));
46+
assertEquals("[null, a, b, c]", Arrays.toString(ArrayUtils.combine(null, "a", "b", "c")));
47+
String empty = null;
48+
assertEquals("[null]", Arrays.toString(ArrayUtils.combine(empty)));
4649
}
4750
@Test
4851
void testToArray()

approvaltests-util/src/main/java/com/spun/util/ArrayUtils.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,9 @@ else if (isEmpty(b))
213213
}
214214
public static <T> T[] combine(T a, T... b)
215215
{
216-
final T[] toArray = (T[]) Array.newInstance(a.getClass(), 1);
216+
final Class<?> type = a != null ? a.getClass() : b.getClass().getComponentType();
217+
218+
final T[] toArray = (T[]) Array.newInstance(type, 1);
217219
toArray[0] = a;
218220
return combine(toArray, b);
219221
}

0 commit comments

Comments
 (0)