mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-03-29 21:22:06 +01:00
java: change spec. of pack methods in ListTemplate and MapTemplate as follow: If user passes null object to the pack method, MessageTypeException is thrown.
This commit is contained in:
parent
419d2e9564
commit
0c07e745f8
@ -37,8 +37,11 @@ public class ListTemplate implements Template {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
if(!(target instanceof List)) {
|
||||
throw new MessageTypeException();
|
||||
if (! (target instanceof List)) {
|
||||
if (target == null) {
|
||||
throw new MessageTypeException(new NullPointerException("target is null."));
|
||||
}
|
||||
throw new MessageTypeException("target is not List type: " + target.getClass());
|
||||
}
|
||||
List<Object> list = (List<Object>)target;
|
||||
pk.packArray(list.size());
|
||||
@ -47,13 +50,14 @@ public class ListTemplate implements Template {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
int length = pac.unpackArray();
|
||||
List<Object> list;
|
||||
if(to == null) {
|
||||
list = new ArrayList<Object>(length);
|
||||
} else {
|
||||
list = (List<Object>)to;
|
||||
list = (List<Object>) to;
|
||||
list.clear();
|
||||
}
|
||||
for(; length > 0; length--) {
|
||||
@ -62,6 +66,7 @@ public class ListTemplate implements Template {
|
||||
return list;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||
MessagePackObject[] array = from.asArray();
|
||||
List<Object> list;
|
||||
@ -69,7 +74,7 @@ public class ListTemplate implements Template {
|
||||
list = new ArrayList<Object>(array.length);
|
||||
} else {
|
||||
// TODO: optimize if list is instanceof ArrayList
|
||||
list = (List<Object>)to;
|
||||
list = (List<Object>) to;
|
||||
list.clear();
|
||||
}
|
||||
for(MessagePackObject element : array) {
|
||||
|
@ -44,9 +44,12 @@ public class MapTemplate implements Template {
|
||||
@SuppressWarnings("unchecked")
|
||||
public void pack(Packer pk, Object target) throws IOException {
|
||||
if(!(target instanceof Map)) {
|
||||
throw new MessageTypeException();
|
||||
if (target == null) {
|
||||
throw new MessageTypeException(new NullPointerException("target is null."));
|
||||
}
|
||||
throw new MessageTypeException("target is not Map type: " + target.getClass());
|
||||
}
|
||||
Map<Object,Object> map = (Map<Object,Object>)target;
|
||||
Map<Object,Object> map = (Map<Object,Object>) target;
|
||||
pk.packMap(map.size());
|
||||
for(Map.Entry<Object,Object> pair : map.entrySet()) {
|
||||
keyTemplate.pack(pk, pair.getKey());
|
||||
@ -54,13 +57,14 @@ public class MapTemplate implements Template {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||
int length = pac.unpackMap();
|
||||
Map<Object,Object> map;
|
||||
if(to == null) {
|
||||
map = new HashMap<Object,Object>(length);
|
||||
} else {
|
||||
map = (Map<Object,Object>)to;
|
||||
map = (Map<Object,Object>) to;
|
||||
map.clear();
|
||||
}
|
||||
for(; length > 0; length--) {
|
||||
@ -78,7 +82,7 @@ public class MapTemplate implements Template {
|
||||
if(to == null) {
|
||||
map = new HashMap<Object,Object>(src.size());
|
||||
} else {
|
||||
map = (Map<Object,Object>)to;
|
||||
map = (Map<Object,Object>) to;
|
||||
map.clear();
|
||||
}
|
||||
for(Map.Entry<MessagePackObject,MessagePackObject> pair : src.entrySet()) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user