Generic class literals - e.g,, Class<Map<String, Integer>>.class

Discussion in 'Java' started by Purush, Apr 12, 2005.

  1. Purush

    Purush Guest

    Hi,

    Let us say, I have a static method as follows:

    static <T> getData(Class<T> cls, String name) {
    ...
    return cls.cast(..);
    }

    I would like to call the above method like:

    Map<String, Integer> data = getData(Class<Map<String, Integer>>,
    "name")

    or

    Map<String, Integer> data = getData(Map<String, Integer>.class,
    "name")

    and avoid all compiler warnings. But, the compiler does not accept
    both.

    Did any one run into this and know of a way to get this working?

    Thanks
    Purush
    Purush, Apr 12, 2005
    #1
    1. Advertising

  2. Re: Generic class literals - e.g,, Class<Map<String,Integer>>.class

    "Purush" <> writes:

    > Let us say, I have a static method as follows:
    >
    > static <T> getData(Class<T> cls, String name) {


    Should have a return type:
    static <T> T getData(Class<T> cls, String name) {

    > ...
    > return cls.cast(..);
    > }
    >
    > I would like to call the above method like:
    >
    > Map<String, Integer> data = getData(Class<Map<String, Integer>>,
    > "name")


    Here "Class<Map<String,Integer>>" is not a valid expression.

    > Map<String, Integer> data = getData(Map<String, Integer>.class,
    > "name")


    Here "Map<String,Integer>.class" is not working, because
    "Map<String,Integer>" is not a class. It is a type, but not a class.
    If it should be any value (at runtime, mind you), it would be
    "Map.class", but that would go for "Map<Integer,Boolean>" as well.

    A class object exists only at runtime, and there are no generics left
    at that time, so "Map<String,Integer>.class" does not make sense.

    > and avoid all compiler warnings. But, the compiler does not accept
    > both.


    I guess your best bet would be an unchecked warning:

    Map<String, Integer> data = getData(Map.class, "name");

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Apr 12, 2005
    #2
    1. Advertising

  3. If I create a dummy interface like:

    interface MapStringInteger extends Map<String, Integer> {
    // empty marker interface
    }

    and change the call to:

    Map<String, Integer> data = getData(MapStringInteger.class, "name");

    the compiler seems to accept it without unchecked warnings. But, I am
    looking for a cleaner solution.

    Thanks
    Purush
    Purush Rudrakshala, Apr 13, 2005
    #3
  4. Purush

    P.Hill Guest

    Purush Rudrakshala wrote:
    > But, I am looking for a cleaner solution.


    Why do you say defining a named interface is less clean?

    -Paul
    P.Hill, Apr 13, 2005
    #4
  5. Because I have no further use for this interface other than avoiding
    this unchecked exception. So, either I need to define this as a public
    interface for all clients to use, or they need to create their own
    versions of this interface.

    If I use the named interface in method signature, I can not call
    getData() method with Map<String, Integer> values returned from other
    method calls.
    Purush Rudrakshala, Apr 13, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Goche
    Replies:
    8
    Views:
    16,449
  2. Replies:
    2
    Views:
    433
  3. Replies:
    3
    Views:
    321
    Pete Becker
    May 12, 2007
  4. albert kao
    Replies:
    12
    Views:
    594
    Roedy Green
    Oct 7, 2011
  5. Randy Kramer
    Replies:
    12
    Views:
    370
    Robert Klemme
    Oct 25, 2007
Loading...

Share This Page