mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-04-01 09:24:51 +02: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")
|
@SuppressWarnings("unchecked")
|
||||||
public void pack(Packer pk, Object target) throws IOException {
|
public void pack(Packer pk, Object target) throws IOException {
|
||||||
if(!(target instanceof List)) {
|
if (! (target instanceof List)) {
|
||||||
throw new MessageTypeException();
|
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;
|
List<Object> list = (List<Object>)target;
|
||||||
pk.packArray(list.size());
|
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 {
|
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||||
int length = pac.unpackArray();
|
int length = pac.unpackArray();
|
||||||
List<Object> list;
|
List<Object> list;
|
||||||
if(to == null) {
|
if(to == null) {
|
||||||
list = new ArrayList<Object>(length);
|
list = new ArrayList<Object>(length);
|
||||||
} else {
|
} else {
|
||||||
list = (List<Object>)to;
|
list = (List<Object>) to;
|
||||||
list.clear();
|
list.clear();
|
||||||
}
|
}
|
||||||
for(; length > 0; length--) {
|
for(; length > 0; length--) {
|
||||||
@ -62,6 +66,7 @@ public class ListTemplate implements Template {
|
|||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
|
||||||
MessagePackObject[] array = from.asArray();
|
MessagePackObject[] array = from.asArray();
|
||||||
List<Object> list;
|
List<Object> list;
|
||||||
@ -69,7 +74,7 @@ public class ListTemplate implements Template {
|
|||||||
list = new ArrayList<Object>(array.length);
|
list = new ArrayList<Object>(array.length);
|
||||||
} else {
|
} else {
|
||||||
// TODO: optimize if list is instanceof ArrayList
|
// TODO: optimize if list is instanceof ArrayList
|
||||||
list = (List<Object>)to;
|
list = (List<Object>) to;
|
||||||
list.clear();
|
list.clear();
|
||||||
}
|
}
|
||||||
for(MessagePackObject element : array) {
|
for(MessagePackObject element : array) {
|
||||||
|
@ -44,9 +44,12 @@ public class MapTemplate implements Template {
|
|||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public void pack(Packer pk, Object target) throws IOException {
|
public void pack(Packer pk, Object target) throws IOException {
|
||||||
if(!(target instanceof Map)) {
|
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());
|
pk.packMap(map.size());
|
||||||
for(Map.Entry<Object,Object> pair : map.entrySet()) {
|
for(Map.Entry<Object,Object> pair : map.entrySet()) {
|
||||||
keyTemplate.pack(pk, pair.getKey());
|
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 {
|
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||||
int length = pac.unpackMap();
|
int length = pac.unpackMap();
|
||||||
Map<Object,Object> map;
|
Map<Object,Object> map;
|
||||||
if(to == null) {
|
if(to == null) {
|
||||||
map = new HashMap<Object,Object>(length);
|
map = new HashMap<Object,Object>(length);
|
||||||
} else {
|
} else {
|
||||||
map = (Map<Object,Object>)to;
|
map = (Map<Object,Object>) to;
|
||||||
map.clear();
|
map.clear();
|
||||||
}
|
}
|
||||||
for(; length > 0; length--) {
|
for(; length > 0; length--) {
|
||||||
@ -78,7 +82,7 @@ public class MapTemplate implements Template {
|
|||||||
if(to == null) {
|
if(to == null) {
|
||||||
map = new HashMap<Object,Object>(src.size());
|
map = new HashMap<Object,Object>(src.size());
|
||||||
} else {
|
} else {
|
||||||
map = (Map<Object,Object>)to;
|
map = (Map<Object,Object>) to;
|
||||||
map.clear();
|
map.clear();
|
||||||
}
|
}
|
||||||
for(Map.Entry<MessagePackObject,MessagePackObject> pair : src.entrySet()) {
|
for(Map.Entry<MessagePackObject,MessagePackObject> pair : src.entrySet()) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user