Assume the following structure:
class Foo implements Serializable {
private static final long serialVersionUID = 7...L;
private final String someField; ...
}
class BarWithErrorInformation extends Foo {
private static final long serialVersionUID = 1L
private final Exception someCause; ...
}
( the serialVersionUIDs represent the values that we actually have in our product right now )
Our (probably stupid) design does "remote" calls from some system A to another system B. The result on B is a serialized BarWithErrorInformation
object; that goes back to A.
That alone works fine, but unfortunately, A and B can be on different code levels. This means that we are now receiving BarWithErrorInformation
objects containing an exception object of a class that does not exist on A. In that case, deserialization fails (throwing a ClassNotFoundException
on A).
I thought I could simply add a custom readObject()
method to BarWithErrorInformation
:
private void readObject(ObjectInputStream o) throws IOException, ClassNotFoundException {
try {
o.defaultReadObject();
} catch (ClassNotFoundException cnfe) {
this.someCause = new SpecialException(cnfe);
} ...
But if I read the answers from here correctly, the above code would result in only someCause
having a meaningful value.
Is there a way to deserialize all inherited fields with the correct information, while at the same time handling the problem with the unknown exception class?
Update: I agree with the comments that were made; our approach is wrong; and we need to change our mechanism to transport error information. Nonetheless I keep this question open for answers; as the pure technical question (can it be done, and if so, how) still seems worth following on its own!