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:
Muga Nishizawa
2010-12-12 00:47:20 +09:00
parent 419d2e9564
commit 0c07e745f8
2 changed files with 17 additions and 8 deletions

View File

@@ -38,7 +38,10 @@ 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,6 +50,7 @@ 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;
@@ -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;

View File

@@ -44,7 +44,10 @@ 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());
@@ -54,6 +57,7 @@ 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;