Remko Tronçon wrote:
> Hi Jörg,
>
>> Since Optional is an abstract class ... how do you instantiate it
>> "directly"?
>
> That was the confusing part: I don't actually use the "Optional" from
> Guava myself, I have my own Optional class which isn't abstract. It's
> just a container of a reference to the given type.
OK.
>> 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.
>
> My question is then: shouldn't the parent converter use
> getGenericType() instead of getType()?
> getType() returns a class, so for a declared field Optional<String>,
> it will only give me "Optional"; from getGenericType(), I would have
> been able to get the "String" part as well.
A converter is called because it claims to handle a Class, i.e. for an
instance of
============ %< ==========
class Bar {
Optional<String> opt = new Optional<String>("foo");
Object obj = new Optional<String>("bar");
List<Optional<String>> l =
Collections.singletonList(new Optional<String>("baz"));
}
============ %< ==========
your converter would be called 3 times. The ParametrizedType can be provided
only for the instance in the field 'opt'. You would get nothing for the
instance in field 'obj' nor for the one in the list.
>> 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.
>
> Thanks for the suggestion. The multiple instances alternative sounds
> doable for me.
Cheers,
Jörg
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email