users@jersey.java.net

RE: [Jersey] Generics driving me crazy

From: Markus Karg <markus.karg_at_gmx.net>
Date: Mon, 15 Mar 2010 22:28:06 +0100

Thank you so much! Never ever have seen that syntax before…!

 

I wonder whether the Generics inventors actually thought THAT to be
intuitive syntax… ;-)

 

Regards

Markus

 

From: Paul.Sandoz_at_Sun.COM [mailto:Paul.Sandoz_at_Sun.COM]
Sent: Montag, 15. März 2010 08:44
To: users_at_jersey.dev.java.net
Subject: Re: [Jersey] Generics driving me crazy

 

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