Hi,
I hope someone can really help me.
This problem has me stumped. I have class "A" that does not implement
Serializable, but class "A" subclasses Serializable class "B". Therefore,
when reading class "A" it should be read as a serialized class. Class "A"
has one data member "private char m_operator".
When I compile with JDK 1.3.1_17, the data file is read correctly.
m_operator is read as a char.
When I compile with JDK 1.5.0_06, the data file is read incorrectly.
m_operator is tried to be read as a String; thus, the InvalidClassException.
I tried to add the method "private void readObject(ObjectInputStream in)" to
class "A" so that I could get the fields using ObjectInputStream.Field inner
class. With the inner class I could then write:
private void readObject(ObjectInputStream in)
throws IOException, ClassNotFoundException
{
ObjectInputStream.Field fields = in.getFields();
try
{
m_operator = fields.get("m_operator", ' ');
}
catch (IllegalArgumentException exception)
{
try
{
String operator = (String) fields.get("m_operator", "+");
if ((operator != null) && (operator.length() > 0))
{
m_operator = operator.charAt(0);
}
else
{
throw exception
}
}
catch (IllegalArgumentException iaException)
{
throw iaException
}
}
}
I have 2 questions that I hope someone can help explain:
(1) Why is JDK 1.5.0_06 reading m_operator as a String?
(2) Why isn't my readObject() method being called? A serialized class is
supposed to override the default and call this method instead. I tried
implementing Serializable on class "A:, but it had no effect.
Thank you,
Don