Skip to content

Commit 395371e

Browse files
lexlerisidore
andcommitted
B!! fixed type and nullpointer bugs in combine
Co-Authored-By: Llewellyn Falco <[email protected]>
1 parent 3152444 commit 395371e

File tree

3 files changed

+72
-3
lines changed

3 files changed

+72
-3
lines changed

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,54 @@ public void testCombineArrays()
4747
String empty = null;
4848
assertEquals("[null]", Arrays.toString(ArrayUtils.combine(empty)));
4949
}
50+
public interface I
51+
{
52+
}
53+
public static class BrotherA implements I
54+
{
55+
@Override
56+
public String toString()
57+
{
58+
return "BrotherA";
59+
}
60+
}
61+
public static class BrotherB implements I
62+
{
63+
@Override
64+
public String toString()
65+
{
66+
return "BrotherB";
67+
}
68+
}
69+
public static class ChildOfA extends BrotherA
70+
{
71+
@Override
72+
public String toString()
73+
{
74+
return "ChildOfA";
75+
}
76+
}
77+
@Test
78+
void testCoAndContraVariance()
79+
{
80+
Integer nullObject = null;
81+
Object[][] result = new Object[][]{ArrayUtils.combine(new BrotherA(), new ChildOfA()),
82+
ArrayUtils.combine(new BrotherA(), new BrotherB()),
83+
ArrayUtils.combine(new BrotherB(), new BrotherA()),
84+
ArrayUtils.combine(new BrotherB(), new BrotherB()),
85+
ArrayUtils.combine(new ChildOfA(), new BrotherA()),
86+
ArrayUtils.combine(null, new BrotherA(), new ChildOfA()),
87+
ArrayUtils.combine(null, new BrotherA(), new BrotherA()),
88+
ArrayUtils.combine(null, new BrotherA(), new BrotherA(), new BrotherA()),
89+
ArrayUtils.combine(new BrotherA(), null, null),
90+
ArrayUtils.combine(new BrotherA(), new BrotherA[]{null}),
91+
ArrayUtils.combine(new BrotherA()),
92+
ArrayUtils.combine(null),
93+
ArrayUtils.combine(new BrotherA(), null),
94+
ArrayUtils.combine(nullObject, null),};
95+
Approvals.verifyAll("", result,
96+
a -> String.format("%s = %s", a.getClass().getSimpleName(), Arrays.toString(a)));
97+
}
5098
@Test
5199
void testToArray()
52100
{
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
BrotherA[] = [BrotherA, ChildOfA]
2+
I[] = [BrotherA, BrotherB]
3+
I[] = [BrotherB, BrotherA]
4+
BrotherB[] = [BrotherB, BrotherB]
5+
BrotherA[] = [ChildOfA, BrotherA]
6+
BrotherA[] = [null, BrotherA, ChildOfA]
7+
BrotherA[] = [null, BrotherA, BrotherA]
8+
BrotherA[] = [null, BrotherA, BrotherA, BrotherA]
9+
BrotherA[] = [BrotherA, null, null]
10+
BrotherA[] = [BrotherA, null]
11+
BrotherA[] = [BrotherA]
12+
Object[] = [null]
13+
BrotherA[] = [BrotherA, null]
14+
Object[] = [null, null]

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,20 +209,27 @@ else if (isEmpty(b))
209209
}
210210
else
211211
{
212-
T[] newArray = null;
213-
newArray = (T[]) Array.newInstance(a.getClass().getComponentType(), a.length + b.length);
212+
T[] newArray = (T[]) Array.newInstance(a.getClass().getComponentType(), a.length + b.length);
214213
System.arraycopy(a, 0, newArray, 0, a.length);
215214
System.arraycopy(b, 0, newArray, a.length, b.length);
216215
return newArray;
217216
}
218217
}
219218
public static <T> T[] combine(T a, T... b)
220219
{
221-
final Class<?> type = a != null ? a.getClass() : b.getClass().getComponentType();
220+
if (b == null)
221+
{
222+
b = (T[]) Array.newInstance(getClassFor(a), 1);
223+
}
224+
final Class<?> type = b.getClass().getComponentType();
222225
final T[] toArray = (T[]) Array.newInstance(type, 1);
223226
toArray[0] = a;
224227
return combine(toArray, b);
225228
}
229+
private static <T> Class<?> getClassFor(T object)
230+
{
231+
return object != null ? object.getClass() : Object.class;
232+
}
226233
public static <T> boolean contains(T[] values, T value)
227234
{
228235
for (int i = 0; i < values.length; i++)

0 commit comments

Comments
 (0)