From 357951ff5c28dbd724611e8d41e23686f09a164a Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Tue, 25 May 2021 15:23:43 -0400 Subject: [PATCH] Remove some @SuppressWarnings. --- .../org/apache/commons/lang3/ObjectUtils.java | 7 +++---- .../commons/lang3/SerializationUtils.java | 20 ++++++++----------- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/ObjectUtils.java b/src/main/java/org/apache/commons/lang3/ObjectUtils.java index 78787fa6d..fd50655fe 100644 --- a/src/main/java/org/apache/commons/lang3/ObjectUtils.java +++ b/src/main/java/org/apache/commons/lang3/ObjectUtils.java @@ -1082,11 +1082,10 @@ public class ObjectUtils { Validate.notEmpty(items, "null/empty items"); Validate.noNullElements(items); Validate.notNull(comparator, "comparator"); - final TreeSet sort = new TreeSet<>(comparator); - Collections.addAll(sort, items); + final TreeSet treeSet = new TreeSet<>(comparator); + Collections.addAll(treeSet, items); @SuppressWarnings("unchecked") //we know all items added were T instances - final - T result = (T) sort.toArray()[(sort.size() - 1) / 2]; + final T result = (T) treeSet.toArray()[(treeSet.size() - 1) / 2]; return result; } diff --git a/src/main/java/org/apache/commons/lang3/SerializationUtils.java b/src/main/java/org/apache/commons/lang3/SerializationUtils.java index 947460d3d..8db268e59 100644 --- a/src/main/java/org/apache/commons/lang3/SerializationUtils.java +++ b/src/main/java/org/apache/commons/lang3/SerializationUtils.java @@ -138,21 +138,17 @@ public class SerializationUtils { final byte[] objectData = serialize(object); final ByteArrayInputStream bais = new ByteArrayInputStream(objectData); - try (ClassLoaderAwareObjectInputStream in = new ClassLoaderAwareObjectInputStream(bais, - object.getClass().getClassLoader())) { + final Class cls = ObjectUtils.getClass(object); + try (ClassLoaderAwareObjectInputStream in = new ClassLoaderAwareObjectInputStream(bais, cls.getClassLoader())) { /* - * when we serialize and deserialize an object, - * it is reasonable to assume the deserialized object - * is of the same type as the original serialized object + * when we serialize and deserialize an object, it is reasonable to assume the deserialized object is of the + * same type as the original serialized object */ - @SuppressWarnings("unchecked") // see above - final T readObject = (T) in.readObject(); - return readObject; + return cls.cast(in.readObject()); - } catch (final ClassNotFoundException ex) { - throw new SerializationException("ClassNotFoundException while reading cloned object data", ex); - } catch (final IOException ex) { - throw new SerializationException("IOException while reading or closing cloned object data", ex); + } catch (final ClassNotFoundException | IOException ex) { + throw new SerializationException( + String.format("%s while reading cloned object data", ex.getClass().getSimpleName()), ex); } }