Skip to content

Commit

Permalink
Fixes NPE in PrefabValues #552
Browse files Browse the repository at this point in the history
  • Loading branch information
jqno committed Dec 15, 2021
1 parent 49e673e commit 018adb4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public <T> T giveOther(TypeTag tag, T value) {
if (type.isArray() && arraysAreDeeplyEqual(tuple.getRed(), value)) {
return tuple.getBlue();
}
if (!type.isArray() && tuple.getRed().equals(value)) {
if (!type.isArray() && value != null && tuple.getRed().equals(value)) {
return tuple.getBlue();
}
return tuple.getRed();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ public void giveOtherWhenValueIsNull() {
assertEquals(expected, pv.giveOther(POINT_TAG, null));
}

@Test
public void giveOtherWhenValueIsNullAndTypeWouldThrowNpe() {
TypeTag tag = new TypeTag(NpeThrowing.class);
NpeThrowing expected = pv.giveRed(tag);
assertEquals(expected, pv.giveOther(tag, null));
}

@Test
public void giveOtherWhenValueIsKnownArray() {
String[] red = pv.giveRed(STRING_ARRAY_TAG);
Expand Down Expand Up @@ -236,6 +243,29 @@ public void addLazyFactoryIsLazy() {
}
}

public static class NpeThrowing {

private final int i;

public NpeThrowing(int i) {
this.i = i;
}

public boolean equals(Object obj) {
if (obj == null) {
throw new NullPointerException();
}
if (!(obj instanceof NpeThrowing)) {
return false;
}
return i == ((NpeThrowing) obj).i;
}

public int hashCode() {
return i;
}
}

private static class AppendingStringTestFactory implements PrefabValueFactory<String> {

private String red;
Expand Down

0 comments on commit 018adb4

Please sign in to comment.