diff --git a/src/java/org/apache/commons/collections/bag/AbstractMapBag.java b/src/java/org/apache/commons/collections/bag/AbstractMapBag.java index 37d209a4d..e63ac9963 100644 --- a/src/java/org/apache/commons/collections/bag/AbstractMapBag.java +++ b/src/java/org/apache/commons/collections/bag/AbstractMapBag.java @@ -508,14 +508,15 @@ public abstract class AbstractMapBag implements Bag { * @param in the input stream * @throws IOException * @throws ClassNotFoundException + * @throws ClassCastException if the stream does not contain the correct objects */ - @SuppressWarnings("unchecked") - protected void doReadObject(Map map, ObjectInputStream in) throws IOException, + protected void doReadObject(Map map, ObjectInputStream in) throws IOException, ClassNotFoundException { this.map = map; int entrySize = in.readInt(); for (int i = 0; i < entrySize; i++) { - Object obj = in.readObject(); + @SuppressWarnings("unchecked") // This will fail at runtime if the stream is incorrect + E obj = (E) in.readObject(); int count = in.readInt(); map.put(obj, new MutableInteger(count)); size += count; diff --git a/src/java/org/apache/commons/collections/bag/TreeBag.java b/src/java/org/apache/commons/collections/bag/TreeBag.java index c49f2ad4b..716835a7d 100644 --- a/src/java/org/apache/commons/collections/bag/TreeBag.java +++ b/src/java/org/apache/commons/collections/bag/TreeBag.java @@ -125,11 +125,11 @@ public class TreeBag extends AbstractMapBag implements SortedBag, Seria /** * Read the bag in using a custom routine. */ - @SuppressWarnings("unchecked") private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); - Comparator comp = (Comparator) in.readObject(); - super.doReadObject(new TreeMap(comp), in); + @SuppressWarnings("unchecked") // This will fail at runtime if the stream is incorrect + Comparator comp = (Comparator) in.readObject(); + super.doReadObject(new TreeMap(comp), in); } }