On Apr 14, 2010, at 11:38 PM, Marc Hadley wrote:
> On Apr 14, 2010, at 4:37 PM, ljnelson wrote:
>
>> On Wed, Apr 14, 2010 at 4:31 PM, Marc Hadley-2 [via Jersey]
>> <[hidden email]> wrote:
>>> The isWritable method is responsible for filtering the types that
>>> the
>>> writeTo will be called for so you might get a subclass of T if you
>>> decide to
>>> support that.
>>
>> OK. Looks like I should be using a wildcard in here as you suggest.
>>
>>>> This leads to my next question. The first thing I need to do in my
>>>> "chaining" MessageBodyWriter is to ask Providers for another one
>>>> that
>>>> matches. I'm armed at this point with a T, and a Class<?>. But
>>>> Providers#getMessageBodyWriter() needs a Class argument that has as
>>>> its parameter T. It would seem that I can't guarantee this.
>>>> What's
>>>> your advice?
>>>>
>>> If you use Class<?> then you'll get a MessageBodyWriter<?> which
>>> should
>>> still work fine for you. I'd probably go that way for a chaining
>>> writer that
>>> supports multiple types.
>>
>> OK...well, here, here's how I've implemented my getSize() method,
>> which is the actual one still causing problems once I fixed the other
>> wildcard issues (in the other methods):
>>
>> @Override
>> public long getSize(final T t, final Class<?> type, final Type
>> genericType, final Annotation[] annotations, final MediaType
>> mediaType) {
>> final MessageBodyWriter<?> delegate =
>> this.providers.getMessageBodyWriter(type, genericType, annotations,
>> mediaType);
>
> Generics can be mind boggling. I don't know the correct incantation
> to fix this but if you change the above line to
>
> final MessageBodyWriter delegate = ...
>
> Then the compiler is happy.
>
See here for why:
http://www.angelikalanger.com/GenericsFAQ/FAQSections/TechnicalDetails.html#FAQ602
Paul.