Bonjour,
j'ai une méthode cloneObjectThroughSerialization que je n'arrive pas à construire correctement : pas moyen de retourner le type correct.
A l'exécution, l'exception suivante est retournée :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 public <T extends Serializable> T cloneObjectThroughSerialization(T obj) { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oso = new ObjectOutputStream(baos); oso.writeObject(obj); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ObjectInputStream osi = new ObjectInputStream(bais); return (T) osi.readObject(); } catch (IOException e) { throw new RuntimeException("Serialization Fail!", e); } catch (ClassNotFoundException e) { throw new RuntimeException("Serialization Fail!", e); } }
Merci de votre aide
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 Exception in thread "main" java.lang.Error: Unresolved compilation problem: Type safety: Unchecked cast from Object to T at org.polymorphisme.beans.SerializationUtil.cloneObjectThroughSerialization(SerializationUtil.java:141) at org.polymorphisme.beans.TestSerialization.testCustomSerialization(TestSerialization.java:69) at org.polymorphisme.beans.TestSerialization.main(TestSerialization.java:60)![]()
Partager