RE: JAX-RS: generics: vs

  • From: "Manger, James H" <James.H.Manger@...>
  • To: "users@..." <users@...>
  • Subject: RE: JAX-RS: generics: <String,Object> vs <String,?>
  • Date: Thu, 17 Jul 2008 09:37:50 +1000
  • Accept-language: en-US, en-AU
  • Acceptlanguage: en-US, en-AU
  • Mailing-list: contact users-help@...; run by ezmlm

> Map<String, String> is assignable to Map<String, Object>

No, that is wrong -- as I found in Sun's Generics tutorial 
<http://java.sun.com/docs/books/tutorial/extra/generics/subtype.html>.

We do need to change the method signature to
  UriBuilder.build(Map<String,?> values)
to accept a Map<String,String> argument.

I found the reason Jersey was not complaining --
calling build with a Map<String,String> argument calls
the VARARG method build(Object... values)!!!

Compile & run the following code. It produces "varargs".
Change Map<String,Object> to Map<String,?> and it produces "map".
There are no compiler errors in either case.

import java.util.*;
class Generic2
{
        static String build() { return "noargs"; }
        static String build(Map<String,Object> values) { return "map"; }
        static String build(Object... values) { return "varargs"; }

        public static void main(String[] args)
        {
                Map<String,String> values = new HashMap<String,String>();
                values.put("foo", "bar");
                System.out.println(build(values));
        }
}

This makes it a bit more important to add the following method now:
  UriBuilder.build(MultivaluedMap<String,?> values)
Otherwise (if it is only added in, say, version 2) we wont get explicit 
errors (which we want) when version 2 code tries to call 
build(MultivaluedMap) in a version 1 library. Instead the call will succeed 
but with unexpected results.

James Manger


JAX-RS: generics: <String,Object> vs <String,?>

Manger, James H 07/16/2008

Re: JAX-RS: generics: <String,Object> vs <String,?>

Marc Hadley 07/16/2008

RE: JAX-RS: generics: <String,Object> vs <String,?>

Manger, James H 07/16/2008

Re: JAX-RS: generics: <String,Object> vs <String,?>

Marc Hadley 07/17/2008

RE: JAX-RS: generics: <String,Object> vs <String,?>

Manger, James H 07/17/2008

Re: JAX-RS: generics: <String,Object> vs <String,?>

Stephan Koops 07/17/2008

RE: JAX-RS: generics: <String,Object> vs <String,?>

Manger, James H 07/18/2008

Re: JAX-RS: generics: <String,Object> vs <String,?>

Marc Hadley 07/19/2008
Terms of Use; Privacy Policy; Copyright ©2013-2014 (revision 20140418.2d69abc)
 
 
Close
loading
Please Confirm
Close