commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Joerg Schaible (JIRA)" <>
Subject [jira] [Resolved] (LANG-1159) SerializationUtils.clone cannot be used with java.util.List and "type safety"
Date Mon, 27 Jul 2015 12:36:04 GMT


Joerg Schaible resolved LANG-1159.
    Resolution: Invalid

The compiler simply complains, that you did not provide a Serializable type as argument, since
the method creates a close based on serialization. All you have to do is to provide a Serializable
type. List is not serializable. However, if you know, that you are using a serializable Implementation,
cast to that one:

List<Integer> list = new ArrayList<>();
List<Integer> clone = SerializationUtils.clone(ArrayList.class.cast(list));

A separate method to clone a collection is superfluous, especially if the method should only
work for serializable collection implementations.

> SerializationUtils.clone cannot be used with java.util.List and "type safety"
> -----------------------------------------------------------------------------
>                 Key: LANG-1159
>                 URL:
>             Project: Commons Lang
>          Issue Type: Improvement
>    Affects Versions: 3.3.2
>            Reporter: Stefan Cordes
>            Priority: Minor
> Having a 
> java.util.List<Integer> someElements;
> and wanting to clone with
> deepClone = SerializationUtils.clone(someElements);
> gives error
> "is not a valid substitute for the bounded parameter <T extends Serializable>	"
> Casting to Serializable:
> deepClone = SerializationUtils.clone((Serializable)someElements);
> gives error
> "Type mismatch: cannot convert from Serializable to List<Integer>"
> and finally 
> deepClone = (java.util.List<Integer>) SerializationUtils.clone((Serializable)someElements);
> gives warning
> "Type safety: Unchecked cast from Serializable to List<Integer>"
> Please add a Method to clone Collections in SerializationUtils:
> @SuppressWarnings("unchecked")
> public static <T extends Collection<S>, S extends Serializable> T cloneCollection(T
aCollection) {
> 	// Assuming Serializable implementation of Collection is used (e.g. ArrayList)
> 	return (T) clone((Serializable) aCollection);
> }

This message was sent by Atlassian JIRA

View raw message