[jdom-interest] Serialization Trouble through Inheritance

Oliver Ruebenacker curoli at gmail.com
Thu Aug 9 08:03:52 PDT 2012


     Hello,

  Rolf, it shouldn't be necessary to add these methods.

  Björn, can you check the import statement for Element?

     Take care
     Oliver

On Thu, Aug 9, 2012 at 10:37 AM, Rolf Lear <jdom at tuis.net> wrote:
>
> Hi Björn
>
> I don't have my 'JDOM Laptop' with me at the moment, so I can't easily
> reproduce your problem, but, I suspect this is just an issue of
> implementing your own read/write object methods.
>
> What happens if you add the following methods to your 'MyElement' class? I
> believe this should fix things..... try it... :-)
>
>
>
>         /**
>          * Serialize out the MyElement.
>          *
>          * @serialData
>          * The Stream protocol is:
>          * <ol>
>          *   <li>Write the super Element class out...
>          * </ol>
>          *
>          * @param out where to write the Element to.
>          * @throws IOException if there is a writing problem.
>          */
>         private void writeObject(final ObjectOutputStream out) throws IOException
> {
>                 out.defaultWriteObject();
>         }
>
>         /**
>          * Read a MyElement off the ObjectInputStream.
>          *
>          * @see #writeObject(ObjectOutputStream)
>          * @param in where to read the Element from.
>          * @throws IOException if there is a reading problem.
>          * @throws ClassNotFoundException when a class cannot be found
>          */
>         private void readObject(final ObjectInputStream in)
>                         throws IOException, ClassNotFoundException {
>                 in.defaultReadObject();
>         }
>
>
> Rolf
>
>
>
> On Thu, 09 Aug 2012 16:08:48 +0200, Björn Buchner
> <b.buchner at isys-software.de> wrote:
>> Hi Folks,
>>
>> I am using the JDOM 2.0.2 release and experienced some trouble when it
>> comes to serialization.
>>
>> De-/serializing an Element object through the standard
>> ObjectOutput/InputStream is no problem. Trouble starts when I try to
>> deserialize an object of a class that inherits from Element.
>>
>> It always ends with a InvalidClassException: no valid constructor.
>> Normally this indicates that JRE is missing a public default
>> constructor, but as you can see in the example below the subclass
>> has a default constructor.
>>
>> Thanks in advance for your help
>>
>> Example code:
>>
>> public class MyElement extends Element {
>>
>>      private static final long serialVersionUID = -4220756491425652053L;
>>
>>      public MyElement() {
>>          super();
>>      }
>>
>>
>>      public static void main(String ... args) throws IOException,
>> ClassNotFoundException {
>>         ByteArrayOutputStream buffer = new ByteArrayOutputStream();
>>         ObjectOutputStream outStream = new ObjectOutputStream(buffer);
>>
>>         MyElement element = new MyElement();
>>
>>         outStream.writeObject(element);
>>         outStream.flush();
>>         ObjectInputStream inStream = new ObjectInputStream(new
>> ByteArrayInputStream(buffer.toByteArray()));
>>         element = (MyElement)inStream.readObject();
>>      }
>> }
>>
>> Result:
>>
>> Exception in thread "main" java.io.InvalidClassException:
>> <packageremoved>; no valid constructor
>>      at
>>
> java.io.ObjectStreamClass$ExceptionInfo.newInvalidClassException(ObjectStreamClass.java:147)
>>      at
>> java.io.ObjectStreamClass.checkDeserialize(ObjectStreamClass.java:755)
>>      at
>>
> java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1751)
>>      at
> java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
>>      at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
>>      at <packageremoved>.main(MyElement.java:29)
> _______________________________________________
> To control your jdom-interest membership:
> http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com



-- 
Java Developer (Bioinformatics) at PanGenX (http://www.pangenx.com)
President and Founder of Knowomics
(http://www.knowomics.com/wiki/Oliver_Ruebenacker)
Consultant at Predictive Medicine
(http://predmed.com/people/oliverruebenacker.html)
SBPAX: Turning Bio Knowledge into Math Models (http://www.sbpax.org)



More information about the jdom-interest mailing list