java - Hitting ClassCastException to String -
edit : solved using this solution
i have strange problem. i'm trying deserialize hashmap 2 classes have since been changed, though not large degree. can't post code because it's under copyright.
notably constructor of class 2 has added field (string name) since file i'm trying deserialize serialized.
it seems class 1 trying cast string name field of class 2.
i've been using objectinputstream , objectoutputstream. part has me mystified class 1 has never had member variable of type class 2, , hashmap types have been in order . don't know what's going on, tried solution posted in this question had same exception i'm getting, despite lots of research on classloaders, still don't understand , didn't know classloader use "free variable".
here stack trace (class names changed) of problem i'm encountering:
java.lang.classcastexception: cannot assign instance of [class 1] field [class 2].name of type java.lang.string in instance of [class 2] @ java.io.objectstreamclass$fieldreflector.setobjfieldvalues(objectstreamclass.java:2034) @ java.io.objectstreamclass.setobjfieldvalues(objectstreamclass.java:1207) @ java.io.objectinputstream.defaultreadfields(objectinputstream.java:1975) @ java.io.objectinputstream.readserialdata(objectinputstream.java:1893) @ java.io.objectinputstream.readordinaryobject(objectinputstream.java:1775) @ java.io.objectinputstream.readobject0(objectinputstream.java:1327) @ java.io.objectinputstream.readobject(objectinputstream.java:349) @ java.util.hashmap.readobject(hashmap.java:1030) @ sun.reflect.generatedmethodaccessor27.invoke(unknown source) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:25) @ java.lang.reflect.method.invoke(method.java:597) @ java.io.objectstreamclass.invokereadobject(objectstreamclass.java:969) @ java.io.objectinputstream.readserialdata(objectinputstream.java:1871) @ java.io.objectinputstream.readordinaryobject(objectinputstream.java:1775) @ java.io.objectinputstream.readobject0(objectinputstream.java:1327) @ java.io.objectinputstream.readobject(objectinputstream.java:349) code snip:
final classloader loader = this.getclass().getclassloader(); //attempt solve classcastexception issue below. fileinputstream fin = new fileinputstream(targetfile); inputstream buffer = new bufferedinputstream(fin); objectinputstream ois = new objectinputstream(buffer) { /* * seen here: https://stackoverflow.com/questions/2358886/how-can-i-deserialize-the-object-if-it-was-moved-to-another-package-or-renamed */ @override protected objectstreamclass readclassdescriptor() throws ioexception, classnotfoundexception { objectstreamclass resultclassdescriptor = super.readclassdescriptor(); if (resultclassdescriptor.getname().equals("class 1 [old path]")) resultclassdescriptor = objectstreamclass.lookup(class 1 [new path].class); else if (resultclassdescriptor.getname().equals("class 2 [old path]")) resultclassdescriptor = objectstreamclass.lookup(class 2 [new path].class); return resultclassdescriptor; } /* * seen here : https://stackoverflow.com/questions/9110677/readresolve-not-working-an-instance-of-guavas-serializedform-appears */ @suppresswarnings("rawtypes") @override protected class resolveclass(objectstreamclass objectstreamclass) throws ioexception, classnotfoundexception { return class.forname(objectstreamclass.getname(), true, loader); } }; object ob = ois.readobject(); //exception happens here hashmap<class 1, class 2> loadedmap = (hashmap<class 1, class 2>)ob; ois.close(); buffer.close(); fin.close();
it looks assigning 2 incompatible classes.
class_1 = new class_2() class_1 has either of class_2 or superclass of class_2. seems
resultclassdescriptor = objectstreamclass.lookup(class 2 [new path].class); may culprit. //................... edit
class a{} class b extends a{} class c{} a obj = new b(); , not give error since super class of b. if cast obj class c, throw classcastexception
c cobj = (c) obj;
Comments
Post a Comment