From 8c29c67486cc56d9a01ed238a4b81651e442e42b Mon Sep 17 00:00:00 2001 From: "Gary D. Gregory" Date: Thu, 5 Apr 2012 16:09:37 +0000 Subject: [PATCH] [LANG-798] Use generics in SerializationUtils git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1309920 13f79535-47bb-0310-9956-ffa450edef68 --- .../commons/lang3/SerializationUtils.java | 11 ++-- src/site/changes/changes.xml | 61 ++++++++++--------- .../AbstractExceptionContextTest.java | 2 +- .../lang3/time/FastDateParserTest.java | 4 +- .../lang3/time/FastDatePrinterTest.java | 4 +- 5 files changed, 42 insertions(+), 40 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/SerializationUtils.java b/src/main/java/org/apache/commons/lang3/SerializationUtils.java index 5e07338ed..fb8fdd499 100644 --- a/src/main/java/org/apache/commons/lang3/SerializationUtils.java +++ b/src/main/java/org/apache/commons/lang3/SerializationUtils.java @@ -181,7 +181,8 @@ public class SerializationUtils { * @throws IllegalArgumentException if {@code inputStream} is {@code null} * @throws SerializationException (runtime) if the serialization fails */ - public static Object deserialize(InputStream inputStream) { + @SuppressWarnings("unchecked") + public static T deserialize(InputStream inputStream) { if (inputStream == null) { throw new IllegalArgumentException("The InputStream must not be null"); } @@ -189,7 +190,7 @@ public class SerializationUtils { try { // stream closed in the finally in = new ObjectInputStream(inputStream); - return in.readObject(); + return (T) in.readObject(); } catch (ClassNotFoundException ex) { throw new SerializationException(ex); @@ -214,12 +215,12 @@ public class SerializationUtils { * @throws IllegalArgumentException if {@code objectData} is {@code null} * @throws SerializationException (runtime) if the serialization fails */ - public static Object deserialize(byte[] objectData) { + @SuppressWarnings("unchecked") + public static T deserialize(byte[] objectData) { if (objectData == null) { throw new IllegalArgumentException("The byte[] must not be null"); } - ByteArrayInputStream bais = new ByteArrayInputStream(objectData); - return deserialize(bais); + return (T) deserialize(new ByteArrayInputStream(objectData)); } /** diff --git a/src/site/changes/changes.xml b/src/site/changes/changes.xml index ec922c60c..7c532fa8e 100644 --- a/src/site/changes/changes.xml +++ b/src/site/changes/changes.xml @@ -22,17 +22,18 @@ + Use generics in SerializationUtils SerializationUtils throws ClassNotFoundException when cloning primitive classes StringUtils equals() relies on undefined behavior Documentation bug: StringUtils.split TypeUtilsTest contains incorrect type assignability assertion TypeUtils.getTypeArguments() misses type arguments for partially-assigned classes ImmutablePair doc contains nonsense text - ClassUtils.PACKAGE_SEPARATOR javadoc contains garbage text + ClassUtils.PACKAGE_SEPARATOR Javadoc contains garbage text EventListenerSupport.ProxyInvocationHandler no longer defines serialVersionUID StrBuilder is now serializable - Fix javadoc Ant warnings - JavaDoc bug in static inner class DateIterator + Fix Javadoc Ant warnings + Javadoc bug in static inner class DateIterator Add Triple class (ternary version of Pair) FastDateFormat supports parse methods @@ -77,7 +78,7 @@ swapCase and *capitalize speedups. CharSetUtils.squeeze() speedup. StringUtils doc/comment spelling fixes. - Increase test coverage of FieldUtils read methods and tweak javadoc. + Increase test coverage of FieldUtils read methods and tweak Javadoc. Add includeantruntime=false to javac targets to quell warnings in ant 1.8.1 and better (and modest performance gain). StringIndexOutOfBoundsException when calling unescapeHtml4("&#03"). StringEscapeUtils.escapeEcmaScript from lang3 cuts off long Unicode string. @@ -273,7 +274,7 @@ NumberUtils - isNumber(String) and createNumber(String) both modified to support '2.'. StringUtils - improve handling of case-insensitive Strings. StringUtils - replaceEach() no longer NPEs when null appears in the last String[]. - StringUtils - correct JavaDocs for startsWith() and startsWithIgnoreCase(). + StringUtils - correct Javadoc for startsWith() and startsWithIgnoreCase(). StringEscapeUtils - escapeJava() escapes '/' characters. StringEscapeUtils - change escapeJavaStyleString() to throw UnhandledException instead swallowing IOException and returning null. WordUtils - fix StringIndexOutOfBoundsException when lower is greater than the String length. @@ -329,9 +330,9 @@ Add toArray() method to IntRange and LongRange classes. add SystemUtils.IS_OS_WINDOWS_VISTA field. Pointless synchronized in ThreadLocal.initialValue should be removed. - ToStringStyle javadoc should show examples of styles. + ToStringStyle Javadoc should show examples of styles. Documentation bug for ignoreEmptyTokens accessors in StrTokenizer. - BooleanUtils toBooleanObject javadoc does not match implementation. + BooleanUtils toBooleanObject Javadoc does not match implementation. truncateNicely method which avoids truncating in the middle of a word. @@ -345,7 +346,7 @@ ToStringBuilder throws StackOverflowError when an Object cycle exists. Create more tests to test out the +=31 replacement code in DurationFormatUtils. StrBuilder contains usages of thisBuf.length when they should use size. - Enum JavaDoc: 1) outline 5.0 native Enum migration 2) warn not to use the switch() , 3) point out approaches for persistence and gui. + Enum Javadoc: 1) outline 5.0 native Enum migration 2) warn not to use the switch() , 3) point out approaches for persistence and gui. Wrong behavior of Entities.unescape. NumberUtils.createNumber throws NumberFormatException for one digit long. NullPointerException in isAvailableLocale(Locale). @@ -370,7 +371,7 @@ Replace Clover with Cobertura. ValuedEnum.compareTo(Object other) not typesafe - it easily could be... LocaleUtils test fails under Mustang. - javadoc example for StringUtils.splitByWholeSeparator incorrect. + Javadoc example for StringUtils.splitByWholeSeparator incorrect. PADDING array in StringUtils overflows on '\uffff'. ClassUtils.primitiveToWrapper and Void. unit test for org.apache.commons.lang.text.StrBuilder. @@ -380,7 +381,7 @@ Wrong length check in StrTokenizer.StringMatcher. ExceptionUtils goes into infinite loop in getThrowables is throwable.getCause() == throwable. FastDateFormat: wrong format for date "01.01.1000". - Unclear javadoc for DateUtils.iterator(). + Unclear Javadoc for DateUtils.iterator(). Memory "leak" in StringUtils. StringEscapeUtils should expose escape*() methods taking Writer argument. Fraction.toProperString() returns -1/1 for -1. @@ -396,13 +397,13 @@ Using ReflectionToStringBuilder and excluding secure fields. add generic add method to DateUtils. Tokenizer Enhancements: reset input string, static CSV/TSV factories. - Trivial cleanup of javadoc in various files. + Trivial cleanup of Javadoc in various files. CompositeFormat. Performance boost for RandomStringUtils. Enhanced Class.forName version. Add StringUtils.containsIgnoreCase(...). Support char array converters on ArrayUtils. - DurationFormatUtils.formatDurationISO() javadoc is missing T in duration string between date and time part. + DurationFormatUtils.formatDurationISO() Javadoc is missing T in duration string between date and time part. Minor build and checkstyle changes. Javadoc errors on StringUtils.splitPreserveAllTokens(String, char). EscapeUtil.escapeHtml() should clarify that it does not escape ' chars to '. @@ -412,11 +413,11 @@ Implementation of escape/unescapeHtml methods with Writer. CompareToBuilder excludeFields for reflection method. Add WordUtils.getInitials(String). - Error in an example in the javadoc of the StringUtils.splitPreserveAllTokens() method. - ToStringBuilder/HashCodeBuilder javadoc code examples. + Error in an example in the Javadoc of the StringUtils.splitPreserveAllTokens() method. + ToStringBuilder/HashCodeBuilder Javadoc code examples. Cannot build tests from latest SVN. - minor javadoc improvements for StringUtils.stripXxx() methods. - javadoc for StringUtils.removeEnd is incorrect. + minor Javadoc improvements for StringUtils.stripXxx() methods. + Javadoc for StringUtils.removeEnd is incorrect. Minor tweak to fix of bug # 26616. @@ -427,7 +428,7 @@ ToStringStyle.setArrayEnd(String) doesn't replace null with empty string. New class proposal: CharacterEncoding. SystemUtils fails init on HP-UX. - javadoc - 'four basic XML entities' should be 5 (apos is missing). + Javadoc - 'four basic XML entities' should be 5 (apos is missing). o.a.c.lang.enum.ValuedEnum: 'enum'is a keyword in JDK1.5.0. StringEscapeUtils.unescapeHtml() doesn't handle an empty entity. EqualsBuilder.append(Object[], Object[]) incorrectly checks that rhs[i] is instance of lhs[i]'s class. @@ -465,9 +466,9 @@ Add convenience format(long) methods to FastDateFormat. Enum's outer class may not be loaded for EnumUtils. WordUtils.capitalizeFully(String str) should take a delimiter. - Make javadoc crosslinking configurable. - Minor javadoc fixes for StringUtils.contains(String, String). - Error in JavaDoc for StringUtils.chomp(String, String). + Make Javadoc crosslinking configurable. + Minor Javadoc fixes for StringUtils.contains(String, String). + Error in Javadoc for StringUtils.chomp(String, String). StringUtils.defaultString: Documentation error. Add hashCode-support to class ObjectUtils. add another "known method" to ExceptionUtils. @@ -499,10 +500,10 @@ NumberUtils.createBigDecimal("") NPE in Sun 1.3.1_08. Rationalize StringUtils slice functions. SystemUtils.IS_OS_OS2 Javadoc is wrong. - A small, but important javadoc fix for Fraction proper whole and numerator. + A small, but important Javadoc fix for Fraction proper whole and numerator. Adding tolerance to double[] search methods in ArrayUtils. - lang.builder classes javadoc edits (mostly typo fixes). - StringUtils javadoc and test enhancements. + lang.builder classes Javadoc edits (mostly typo fixes). + StringUtils Javadoc and test enhancements. SystemUtils.IS_OS_*, IS_JAVA_* are always false. Improve util.Validate tests. maven-beta10 checkstyle problem. @@ -513,7 +514,7 @@ RandomStringUtils.randomAlpha methods omit 'z'. test.time fails in Japanese (non-us) locale. NumberUtils.isNumber allows illegal trailing characters. - Improve javadoc and overflow behavior of Fraction. + Improve Javadoc and overflow behavior of Fraction. RandomStringUtils infloops with length > 1. test.lang fails if compiled with non iso-8859-1 locales. SystemUtils does not play nice in an Applet. @@ -537,12 +538,12 @@ NumberRange inaccurate for Long, etc. Hierarchy support in ToStringBuilder.reflectionToString(). StringUtils.countMatches loops forever if substring empty. - javadoc fixes (remove @links to non-public identifiers). - Add javadoc examples and tests for StringUtils. + Javadoc fixes (remove @links to non-public identifiers). + Add Javadoc examples and tests for StringUtils. Make NumberUtils null handling consistent. Unused field 'startFinal' in DateIterator. reduce object creation in ToStringBuilder. - Improved tests, javadoc for CharSetUtils, StringEscapeUtils. + Improved tests, Javadoc for CharSetUtils, StringEscapeUtils. NumberUtils min/max, BooleanUtils.xor, and ArrayUtils toPrimitive and toObject. Javadoc, tests improvements for CharSet, CharSetUtils. StringUtil enhancement. @@ -552,17 +553,17 @@ Missing @since tags. Refactored reflection feature of ToStringBuilder into new ReflectionToStringBuilder. Typo in documentation. - Patch for javadocs. + Patch for Javadoc. Add join(..., char c) to StringUtils (and some performance fixes). Even contains tests!. Resurrect the WordWrapUtils from commons-sandbox/utils. EnumTest fails on Linux Sun JDK 1.3.0. What to do with FastDateFormat unused private constructors. Added class hierachy support to CompareToBuilder.reflectionCompare(). Removed compile warning in FastDateFormat. - typo in the javadoc example code. + typo in the Javadoc example code. MethodUtils: Removed unused code/unused local vars. Hierarchy support in EqualsBuilder.reflectionEquals(). - JavaDoc Errata. + Javadoc Errata. ArrayUtils.contains(). More flexibility for getRootCause in ExceptionUtils. diff --git a/src/test/java/org/apache/commons/lang3/exception/AbstractExceptionContextTest.java b/src/test/java/org/apache/commons/lang3/exception/AbstractExceptionContextTest.java index da703472b..6c717a747 100644 --- a/src/test/java/org/apache/commons/lang3/exception/AbstractExceptionContextTest.java +++ b/src/test/java/org/apache/commons/lang3/exception/AbstractExceptionContextTest.java @@ -170,7 +170,7 @@ public abstract class AbstractExceptionContextTest