FindBugs wants readObject(…) to be private for serialization, why?

后端 未结 5 2043
太阳男子
太阳男子 2021-01-20 08:59

I am running findbugs on some code and it says the readObject(...) method must be private to be invoked for serialization/unserialization? Why? What is the problem if it is

5条回答
  •  灰色年华
    2021-01-20 09:55

    I'm not sure why findbugs thinks it's a bug, but I can guess at two possible reasons. Making readObject public breaks encapsulation because the calling code has visibility into the internal structure of your class. Also, by making it public you force all derived classes to declare readObject as public. So unless the class is final, you're changing the contract for serialization.

    I thought findbugs could provide rationale for most of its messages. Does it have anything to say about this?

提交回复
热议问题