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

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

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

sebb-2-2
On 25 August 2010 17:15,  <[hidden email]> 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: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

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

Henri Yandell
On Wed, Aug 25, 2010 at 10:46 PM, sebb <[hidden email]> wrote:

> On 25 August 2010 17:15,  <[hidden email]> 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: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

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

James Carman
On Thu, Aug 26, 2010 at 5:04 AM, Henri Yandell <[hidden email]> 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: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

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

jodastephen
On 26 August 2010 11:29, James Carman <[hidden email]> 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: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

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

James Carman
On Thu, Aug 26, 2010 at 6:48 AM, Stephen Colebourne
<[hidden email]> 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: [hidden email]
For additional commands, e-mail: [hidden email]