users@jersey.java.net

Re: [Jersey] Generics driving me crazy

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 15 Mar 2010 08:43:38 +0100

Hi Markus,

Do this:

     public Set<Class<?>> getClasses() {
         return Collections.<Class<?>>singleton(MyResource.class);
     }

I agree this is not obvious. It caught the JAX-RS EG out when
attempting to design a generic Response. If i was aware of this syntax
at the time...

Paul.

On Mar 14, 2010, at 2:42 PM, Markus Karg wrote:

> Even after years of Generics being around that syntax drives me
> crazy… I hope that you can help.
>
> Currently I am registering my JAX-RS application using this code line:
>
> public Set<Class<?>> getClasses() {
> return new HashSet<Class<?>>(Arrays.asList(MyResource.class));
> }
>
> Actually there is only one resource, so it would make sense to
> reduce complexity to…
>
> public Set<Class<?>> getClasses() {
> return Collections.singleton(MyResource.class);
> }
>
> …which looks rather smart (and correct, since singleton() returns
> Set<Class<MyResource>>) but actually produces a compilation error:
>
> cannot convert from Set<Class<MyResource>> to Set<Class<?>>
>
> I wonder how to tell the compiler to accept my idea, without twirks
> and casts…
>
> What certainly is working is the below solution, which is in fact
> not smart as it is even longer than the line I originally wanted to
> replace and still imposes creation of another object...
>
> public final Set<Class<?>> getClasses() {
> return new HashSet<Class<?
> >>(Collections.singleton(OpenSearchResource.class));
> }
>
> Thanks
> Markus