Skip to content

Commit

Permalink
[Dubbo-4299]Fix NPE when PojoUtils realize Null element in collection(#…
Browse files Browse the repository at this point in the history
…4299) (#4300)

* fix NPE when PojoUtils realize Null element in collection(#4299)

* add unit tests for bugfix of PojoUtils NPE(#4299)

* revert import (#4299)
  • Loading branch information
HzjNeverStop authored and zonghaishang committed Jun 24, 2019
1 parent fa619c9 commit 7285ce9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ private static Object realize0(Object pojo, Class<?> type, Type genericType, fin
history.put(pojo, dest);
for (Object obj : src) {
Type keyType = getGenericClassByIndex(genericType, 0);
Class<?> keyClazz = obj.getClass();
Class<?> keyClazz = obj == null ? null : obj.getClass();
if (keyType instanceof Class) {
keyClazz = (Class<?>) keyType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.HashSet;
import java.util.Map;
import java.util.UUID;

Expand Down Expand Up @@ -682,6 +683,24 @@ public void testDateTimeTimestamp() throws Exception {
assertEquals(dateTimeStr, new SimpleDateFormat(dateFormat[0]).format(timestamp));
}

@Test
public void testRealizeCollectionWithNullElement() {
LinkedList<String> listStr = new LinkedList<>();
listStr.add("arrayValue");
listStr.add(null);
HashSet<String> setStr = new HashSet<>();
setStr.add("setValue");
setStr.add(null);

Object listResult = PojoUtils.realize(listStr, LinkedList.class);
assertEquals(LinkedList.class, listResult.getClass());
assertEquals(listResult, listStr);

Object setResult = PojoUtils.realize(setStr, HashSet.class);
assertEquals(HashSet.class, setResult.getClass());
assertEquals(setResult, setStr);
}

public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}
Expand Down

0 comments on commit 7285ce9

Please sign in to comment.