[jdom-interest] Serialization Trouble through Inheritance

Rolf Lear jdom at tuis.net
Thu Aug 9 09:23:03 PDT 2012


Hmmm... in a moment of daftness, I attached the wrong file (more haste...
less speed).

here is the working CloneBase file.



Rolf

On Thu, 09 Aug 2012 11:51:38 -0400, Rolf Lear <jdom at tuis.net> wrote:
> Hi Björn, Oliver
> 
> I have identified what the problem is...... this is not actually a Java
> bug, but it describes what the JDOM bug is... I think....
> 
> http://bugs.sun.com/view_bug.do?bug_id=6522514
> 
> What this implies is that there is an issue in the permissions of the
JDOM
> element hierarchy.
> 
> in my understanding, it goes like this....
> 
> org.jdom2.Element extends org.jdom2.Content.
> org.jdom2.Content extends org.jdom2.CloneBase.
> org.jdom2.CloneBase is a package-private (not public) class, and
CloneBase
> has no declared constructor (it has the default no-arg constructor).
> 
> Your class 'MyElement' is not in the org.jdom2 package, so, it cannot
> 'see' the no-arg constructor for (package private) org.jdom2.CloneBase.
> 
> Thus the MyElement class cannot be de-serialized.
> 
> I have 'proven' that this is the logic fault by putting the MyElement
> class in the org.jdom2 package, and then, miraculously, the code works.
> When I get home I will try it again but instead with a 'protected'
no-arg
> constructor on org.jdom2.CloneBase, and the MyElement class in some
other
> package..... actually, I have just tried it now, and it works.....
> 
> Thus, there are two work-arounds:
>  - putting your MyElement code in the org.jdom2 package....
>  - using the attached org.jdom2.CloneBase class which has a protected
> no-arg constructor.
> 
> 
> I have created a new issue (#88), and I have attached a working
CloneBase
> class you can add to your project temporarily.....
> 
> I will push out JDOM 2.0.3
> 
> Rolf
> 
> 
> 
> 
> On Thu, 9 Aug 2012 11:03:52 -0400, Oliver Ruebenacker <curoli at gmail.com>
> wrote:
>> 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
-------------- next part --------------
/*--

 Copyright (C) 2012 Jason Hunter & Brett McLaughlin.
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:

 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions, and the following disclaimer.

 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions, and the disclaimer that follows
    these conditions in the documentation and/or other materials
    provided with the distribution.

 3. The name "JDOM" must not be used to endorse or promote products
    derived from this software without prior written permission.  For
    written permission, please contact <request_AT_jdom_DOT_org>.

 4. Products derived from this software may not be called "JDOM", nor
    may "JDOM" appear in their name, without prior written permission
    from the JDOM Project Management <request_AT_jdom_DOT_org>.

 In addition, we request (but do not require) that you include in the
 end-user documentation provided with the redistribution and/or in the
 software itself an acknowledgement equivalent to the following:
     "This product includes software developed by the
      JDOM Project (http://www.jdom.org/)."
 Alternatively, the acknowledgment may be graphical using the logos
 available at http://www.jdom.org/images/logos.

 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED.  IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 SUCH DAMAGE.

 This software consists of voluntary contributions made by many
 individuals on behalf of the JDOM Project and was originally
 created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
 Brett McLaughlin <brett_AT_jdom_DOT_org>.  For more information
 on the JDOM Project, please see <http://www.jdom.org/>.

 */

package org.jdom2;

/**
 * This simple class just tidies up any cloneable classes. This method deals
 * with any CloneNotSupported exceptions. THis class is package private only.
 * 
 * @author Rolf Lear
 */
class CloneBase implements Cloneable {
	
	/**
	 * Make the default no-arg constructor available to all sub-classes, not just subclasses in the
	 * org.jdom2 package.
	 * 
	 */
	protected CloneBase() {
		super();
	}

	/**
	 * Return a deep clone of this instance. Even if this instance has a parent,
	 * the returned clone will not.
	 * <p>
	 * All JDOM core classes are Cloneable, and never throw
	 * CloneNotSupportedException. Additionally all Cloneable JDOM classes
	 * return the correct type of instance from this method and there is no
	 * need to cast the result (co-variant return vaue).
	 * <p>
	 * Subclasses of this should still call super.clone() in their clone method.
	 */
	@Override
	protected CloneBase clone() {
		/*
		 * Additionally, when you use the concept of 'co-variant return values'
		 * you create 'bridge' methods. By way of example, because we change the
		 * return type of clone() from Object to CloneBase, Java is forced to
		 * put in a 'bridge' method that has an Object return type, even though
		 * we never actually call it. <p> This has an impact on the code
		 * coverage tool Cobertura, which reports that there is missed code (and
		 * there is, the bridge method). It reports it as being '0' calls to the
		 * 'class' line (the class line is marked red). By making this CloneBase
		 * code do the first level of co-variant return, it is this class which
		 * is victim of the Cobertura reporting, not the multiple subclasses
		 * (like Attribute, Document, Content, etc.).
		 */
		try {
			return (CloneBase) super.clone();
		} catch (CloneNotSupportedException e) {
			throw new IllegalStateException(String.format(
					"Unable to clone class %s which should always support it.",
					this.getClass().getName()), e);
		}
	}

}


More information about the jdom-interest mailing list