The Effective Java says that readResolve works only if all fields are transient.
Isn’t this a bug? Why would the Java creators do a such thing?
— update
Sorry, I mean the More Effective Java, see slide 30.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
For completeness, slide 29 is:
slide 30 is:
Item 77 is:
It’s not a case of only working if all the fields are transient, it’s that it’s only safe if all Object references are transient. The slide you reference goes further and says is any fields are non-transient an attacker can grab a reference to the deserialized Object.
As to why: one can argue that Josh Bloch’s use of
readResolve()for a singleton was an unintended use of the API, one the creators didn’t envision when they made it. You could also argue it’s simply an unforeseen consequence. I don’t think however it was a deliberate weakness.