Promote integer types to the expected type to allow, for example, a

list of mixed integer types to be read as a list of longs.
This commit is contained in:
akwizgran
2011-08-05 13:26:37 +01:00
parent 5cb4075cfd
commit 6c5ce05c5d

View File

@@ -407,7 +407,26 @@ class ReaderImpl implements Reader {
private <T> T readObject(Class<T> t) throws IOException {
try {
return t.cast(readObject());
Object o = readObject();
// If this is a small integer type and we're expecting a larger
// integer type, promote before casting
if(o instanceof Byte) {
if(Short.class.isAssignableFrom(t))
return t.cast(Short.valueOf((Byte) o));
if(Integer.class.isAssignableFrom(t))
return t.cast(Integer.valueOf((Byte) o));
if(Long.class.isAssignableFrom(t))
return t.cast(Long.valueOf((Byte) o));
} else if(o instanceof Short) {
if(Integer.class.isAssignableFrom(t))
return t.cast(Integer.valueOf((Short) o));
if(Long.class.isAssignableFrom(t))
return t.cast(Long.valueOf((Short) o));
} else if(o instanceof Integer) {
if(Long.class.isAssignableFrom(t))
return t.cast(Long.valueOf((Integer) o));
}
return t.cast(o);
} catch(ClassCastException e) {
throw new FormatException();
}