Remko Tronçon wrote:
> Hi Jörg,
>
>> Since Optional is abstract, "myfield" must have been populated with a
>
> Ah, I see that 'Optional' wasn't the best example. My version of
> Optional isn't abstract, I directly
> instantiate 'Optional<String>'.
Since Optional is an abstract class ... how do you instantiate it
"directly"?
>
> You're right, I don't nest my output, so in my case,
> class Bar {
> Optional<String> myField1;
> }
>
> becomes
>
> <Bar>
> <myField1>Hello, world!</myField1>
> </Bar>
>
>> context.getRequiredType() should contain "OptionalString". And I am quite
>> sure that you can find the generic type with:
>> Class optional=context.getRequiredType();
>> while(!Optional.class.equals(optional)) {
>> optional = optional.getSuperclass();
>> }
>> Class genericType = optional.getGenericType();
>
> I would have hoped, but I don't think there is a 'getGenericType' on a
> Class object, so I don't think I can get it from getRequiredType.
> If I would indeed inherit from my Optional, I probably could have used
> getGenericSuperclass() on the class.
> I guess it would have also worked if getRequiredType() returned a Type
> instead of a class?
The question is simply, if the value from "getRequiredType()" is acquired by
the parent converter with "Bar.class.getDeclaredField("myField1").getType()"
in the end. Then you should be able to deduce the generic type.
The problem is that you cannot rely on it:
=============== %< ==============
<list>
<optional>Hello, world!</optional>
</list>
=============== %< ==============
or
=============== %< ==============
class Derived<T> extends Optional<T> {}
<Bar>
<myField1 class="Derived">Hello, world!</myField1>
</Bar>
=============== %< ==============
No info about the generic type available in both cases. Additionally you
will have problems in this case:
=============== %< ==============
class Bar {
Optional<? extends CharSequence> myField1;
}
<Bar>
<myField1>Hello, world!</myField1>
</Bar>
=============== %< ==============
The generic type will not necessarily tell you about the real type of the
instance you have to create at deserialization time.
IMHO you have only two reliably possibilities. Either write the type of the
value into an own attribute or use multiple instances of your converter, let
them take the type as constructor argument and register those converters as
local ones individually.
Cheers,
Jörg
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email