Re: [Simple-support] generics
Brought to you by:
niallg
|
From: Niall G. <gal...@ya...> - 2008-06-07 10:09:39
|
Hi,
There are two problems here. Firstly you need to have the inner class as static so that it can be reflectively instantiated. Also, you may have heard that Java generics are not great. This is for a number of reasons, one being that they are not reified which means that for the following
T m_vehichle
T is not available reflectively. The compiler does not maintain the compiled class type. So its simply an Object. Since the serializer has no type information it can not determine how to serialize it.
Niall
--- On Fri, 6/6/08, Mark Wyszomierski <ma...@gm...> wrote:
> From: Mark Wyszomierski <ma...@gm...>
> Subject: [Simple-support] generics
> To: sim...@li...
> Date: Friday, June 6, 2008, 9:27 PM
> Hi,
>
> I'm trying to serialize a class that uses generics
> like:
>
> @Root
> public class Garage<T extends Vehichle>
> {
> @Element
> protected T m_vehichle;
> }
>
> @Root
> public class Vehichle
> {
> @Element
> protected int m_serialNumber;
> }
>
> @Root
> public class Car extends Vehichle
> {
> @Element
> protected int m_numDoors;
> }
>
> Serializing an instance of Garage works ok:
>
> Garage<Car> garage = new Garage<Car>(new
> Car());
> // ...
> serializer.write(garage);
>
> But deserializing throws an exception:
>
> java.lang.NoSuchMethodException:
> TestSimpleXML$Garage.<init>()
>
> Garage<Car> garage = null;
> try {
> garage = serializer.read(Garage.class, xmlString);
>
> Is it not possible to do?
>
> Thanks,
> Mark
>
> -------------------------------------------------------------------------
> Check out the new SourceForge.net Marketplace.
> It's the best place to buy or sell services for
> just about anything Open Source.
> http://sourceforge.net/services/buy/index.php
> _______________________________________________
> Simple-support mailing list
> Sim...@li...
> https://lists.sourceforge.net/lists/listinfo/simple-support
|