ObjectInputStream custom classloader deserialization issue: resolveClass() not called

二次信任 提交于 2019-12-12 12:26:27

问题


I have an ObjectInputStream and want to load classes with a custom ClassLoader.

Thus is created a subclass of ObjectInputStream that overrides the resolveClass() function.

Now my problem is that i want to change the ClassLoader during execution. But sometimes resolveClass() does not seem to be executed when I do readObject()on this stream. Then the class is loaded with the wrong ClassLoader.

Any idea why resolveClass() is not executed and how to solve this issue?


回答1:


resolveClass() will be called once per class descriptor in the stream. I have no idea what would happen if you wrote multiple descriptors for the same full qualified class name - probably unspecified. The correct solution would be to use multiple streams (could be nested within one another).




回答2:


From Java API doc for ObjectInputStream.resolveClass():

This method will be invoked only once for each unique class in the stream.

Possible quick&dirty fix:

Always call reset() on the ObjectOutputStream after sending. This will reset the streams as if they were new and make them forget about having sent/received a class already.



来源:https://stackoverflow.com/questions/14385997/objectinputstream-custom-classloader-deserialization-issue-resolveclass-not-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!