You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mb...@apache.org on 2010/08/25 18:15:44 UTC

svn commit: r989199 - in /commons/proper/lang/trunk/src: main/java/org/apache/commons/lang3/SerializationUtils.java test/java/org/apache/commons/lang3/SerializationUtilsTest.java

Author: mbenson
Date: Wed Aug 25 16:15:44 2010
New Revision: 989199

URL: http://svn.apache.org/viewvc?rev=989199&view=rev
Log:
make SerializationUtils.clone() type-safe

Modified:
    commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/SerializationUtils.java
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/SerializationUtilsTest.java

Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/SerializationUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/SerializationUtils.java?rev=989199&r1=989198&r2=989199&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/SerializationUtils.java (original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/SerializationUtils.java Wed Aug 25 16:15:44 2010
@@ -77,8 +77,9 @@ public class SerializationUtils {
      * @return the cloned object
      * @throws SerializationException (runtime) if the serialization fails
      */
-    public static Object clone(Serializable object) {
-        return deserialize(serialize(object));
+    @SuppressWarnings("unchecked")
+    public static <T extends Serializable> T clone(T object) {
+        return (T) deserialize(serialize(object));
     }
     
     // Serialize

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/SerializationUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/SerializationUtilsTest.java?rev=989199&r1=989198&r2=989199&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/SerializationUtilsTest.java (original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/SerializationUtilsTest.java Wed Aug 25 16:15:44 2010
@@ -340,7 +340,7 @@ public class SerializationUtilsTest exte
     //-----------------------------------------------------------------------
 
     public void testClone() throws Exception {
-        Object test = SerializationUtils.clone(iMap);
+        HashMap<?, ?> test = SerializationUtils.clone(iMap);
         assertNotNull(test);
         assertTrue(test instanceof HashMap<?,?>);
         assertTrue(test != iMap);



Re: svn commit: r989199 - in /commons/proper/lang/trunk/src: main/java/org/apache/commons/lang3/SerializationUtils.java test/java/org/apache/commons/lang3/SerializationUtilsTest.java

Posted by James Carman <ja...@carmanconsulting.com>.
On Thu, Aug 26, 2010 at 6:48 AM, Stephen Colebourne
<sc...@joda.org> wrote:
> I add this "auto-cast" behaviour to some of my classes too. Its a
> useful technique to handle annoying cases. However, the alternative:
>  Person p = SerializableUtils.deserialize(Person.class, bytes);
> is more widely used.
>

But, using "Person.class," as opposed to "(Person)" is more characters
and doesn't really save you anything.  Either way, you get a runtime
exception if you screw up.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: svn commit: r989199 - in /commons/proper/lang/trunk/src: main/java/org/apache/commons/lang3/SerializationUtils.java test/java/org/apache/commons/lang3/SerializationUtilsTest.java

Posted by Stephen Colebourne <sc...@joda.org>.
On 26 August 2010 11:29, James Carman <ja...@carmanconsulting.com> wrote:
> This way, you could do:
>
> Person p = SerializableUtils.deserialize(bytes);
>
> No casting!  Now, if the bytes don't represent a Person object, then
> you'll get a ClassCastException, but that's exactly the same thing
> that would happen if you did this (with the current API):
>
> Person p = (Person)SerializableUtils.deserialize(bytes);
>
> So, we make it easier to use the API by avoiding a cast.
I add this "auto-cast" behaviour to some of my classes too. Its a
useful technique to handle annoying cases. However, the alternative:
 Person p = SerializableUtils.deserialize(Person.class, bytes);
is more widely used.

Stephen

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: svn commit: r989199 - in /commons/proper/lang/trunk/src: main/java/org/apache/commons/lang3/SerializationUtils.java test/java/org/apache/commons/lang3/SerializationUtilsTest.java

Posted by James Carman <ja...@carmanconsulting.com>.
On Thu, Aug 26, 2010 at 5:04 AM, Henri Yandell <fl...@gmail.com> wrote:
>
> I wonder if this could be a findbugs/checkstyle/pmd rule?
>

Why use findbugs for something like this when it's completely obvious
why it's okay?  When you serialize an object and then deserialize it,
you can be pretty sure you're going to get back the same type of
object.  Complaining about this particular piece of code it just plain
nitpicking, IMHO.

I would argue that the deserialize() method should be genericized as such:

public static <T extends Serializable> T deserialize(byte[] bytes);

This way, you could do:

Person p = SerializableUtils.deserialize(bytes);

No casting!  Now, if the bytes don't represent a Person object, then
you'll get a ClassCastException, but that's exactly the same thing
that would happen if you did this (with the current API):

Person p = (Person)SerializableUtils.deserialize(bytes);

So, we make it easier to use the API by avoiding a cast.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: svn commit: r989199 - in /commons/proper/lang/trunk/src: main/java/org/apache/commons/lang3/SerializationUtils.java test/java/org/apache/commons/lang3/SerializationUtilsTest.java

Posted by Henri Yandell <fl...@gmail.com>.
On Wed, Aug 25, 2010 at 10:46 PM, sebb <se...@gmail.com> wrote:
> On 25 August 2010 17:15,  <mb...@apache.org> wrote:
>> Author: mbenson
>> Date: Wed Aug 25 16:15:44 2010
>> New Revision: 989199
>>
>> URL: http://svn.apache.org/viewvc?rev=989199&view=rev
>> Log:
>> make SerializationUtils.clone() type-safe
>>
>> Modified:
>>    commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/SerializationUtils.java
>>    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/SerializationUtilsTest.java
>>
>> Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/SerializationUtils.java
>> URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/SerializationUtils.java?rev=989199&r1=989198&r2=989199&view=diff
>> ==============================================================================
>> --- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/SerializationUtils.java (original)
>> +++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/SerializationUtils.java Wed Aug 25 16:15:44 2010
>> @@ -77,8 +77,9 @@ public class SerializationUtils {
>>      * @return the cloned object
>>      * @throws SerializationException (runtime) if the serialization fails
>>      */
>> -    public static Object clone(Serializable object) {
>> -        return deserialize(serialize(object));
>> +    @SuppressWarnings("unchecked")
>
> Why is it safe to suppress the warning?
>
> Please can you add a // comment to explain the reason?

I wonder if this could be a findbugs/checkstyle/pmd rule?

Hen

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: svn commit: r989199 - in /commons/proper/lang/trunk/src: main/java/org/apache/commons/lang3/SerializationUtils.java test/java/org/apache/commons/lang3/SerializationUtilsTest.java

Posted by sebb <se...@gmail.com>.
On 25 August 2010 17:15,  <mb...@apache.org> wrote:
> Author: mbenson
> Date: Wed Aug 25 16:15:44 2010
> New Revision: 989199
>
> URL: http://svn.apache.org/viewvc?rev=989199&view=rev
> Log:
> make SerializationUtils.clone() type-safe
>
> Modified:
>    commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/SerializationUtils.java
>    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/SerializationUtilsTest.java
>
> Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/SerializationUtils.java
> URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/SerializationUtils.java?rev=989199&r1=989198&r2=989199&view=diff
> ==============================================================================
> --- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/SerializationUtils.java (original)
> +++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/SerializationUtils.java Wed Aug 25 16:15:44 2010
> @@ -77,8 +77,9 @@ public class SerializationUtils {
>      * @return the cloned object
>      * @throws SerializationException (runtime) if the serialization fails
>      */
> -    public static Object clone(Serializable object) {
> -        return deserialize(serialize(object));
> +    @SuppressWarnings("unchecked")

Why is it safe to suppress the warning?

Please can you add a // comment to explain the reason?

> +    public static <T extends Serializable> T clone(T object) {
> +        return (T) deserialize(serialize(object));
>     }
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org