Casting an object to a genericized TreeMap

Discussion in 'Java' started by Kaiser S., May 3, 2007.

  1. Kaiser S.

    Kaiser S. Guest

    What do you think of this code ? Is there a better way do enforce this
    kind of cast ?

    public static <K, V> TreeMap<K, V> treemap(Object o, Class<K>
    keyClass, Class<V> valueClass) {
    TreeMap<?, ?> tm = (TreeMap)o; // warning 1

    for (Map.Entry<?, ?> couple : tm.entrySet()) {
    keyClass.cast(couple.getKey());
    valueClass.cast(couple.getValue());
    }
    return (TreeMap)o; // warning 2
    }


    called with:

    TreeMap<String, Double> tm = treemap(o, String.class, Double.class);
    Kaiser S., May 3, 2007
    #1
    1. Advertising

  2. Hi,

    Kaiser S. wrote:
    > What do you think of this code ? Is there a better way do enforce this
    > kind of cast ?
    >
    > public static <K, V> TreeMap<K, V> treemap(Object o, Class<K>
    > keyClass, Class<V> valueClass) {
    > TreeMap<?, ?> tm = (TreeMap)o; // warning 1
    >
    > for (Map.Entry<?, ?> couple : tm.entrySet()) {
    > keyClass.cast(couple.getKey());
    > valueClass.cast(couple.getValue());
    > }
    > return (TreeMap)o; // warning 2
    > }
    >
    >
    > called with:
    >
    > TreeMap<String, Double> tm = treemap(o, String.class, Double.class);


    You know that your code does not do anything, and that the following
    would do exactly the same?

    public static <K, V> TreeMap<K, V> treemap(Object o) {
    return (TreeMap<K,V>)o; // warning
    }

    Ciao,
    Ingo
    Ingo R. Homann, May 3, 2007
    #2
    1. Advertising

  3. Kaiser S.

    Kaiser S. Guest

    Ingo R. Homann a écrit :
    > Hi,
    >
    > Kaiser S. wrote:
    >> What do you think of this code ? Is there a better way do enforce this
    >> kind of cast ?
    >>
    >> public static <K, V> TreeMap<K, V> treemap(Object o, Class<K>
    >> keyClass, Class<V> valueClass) {
    >> TreeMap<?, ?> tm = (TreeMap)o; // warning 1
    >> for (Map.Entry<?, ?> couple : tm.entrySet()) {
    >> keyClass.cast(couple.getKey());
    >> valueClass.cast(couple.getValue());
    >> }
    >> return (TreeMap)o; // warning 2
    >> }
    >>
    >>
    >> called with:
    >>
    >> TreeMap<String, Double> tm = treemap(o, String.class, Double.class);

    >
    > You know that your code does not do anything, and that the following
    > would do exactly the same?
    >
    > public static <K, V> TreeMap<K, V> treemap(Object o) {
    > return (TreeMap<K,V>)o; // warning
    > }


    Well i hope not. I check the class of all the keys and values; you must
    have seen it...

    Now the doc of Class.cast says it throw a ClassCastException if the cast
    is invalid, so after the for loop, i can make the ugly cast because i'm
    sure i won't get a ClassCastException somewhere else in my program.
    Kaiser S., May 3, 2007
    #3
  4. Hi Kevin S., ;-)

    Kaiser S. wrote:
    > Well i hope not. I check the class of all the keys and values; you must
    > have seen it...
    >
    > Now the doc of Class.cast says it throw a ClassCastException if the cast
    > is invalid, so after the for loop, i can make the ugly cast because i'm
    > sure i won't get a ClassCastException somewhere else in my program.


    Ah, OK, for *checking* the types, it is OK. I thought you wanted to
    really *convert* something!

    Ciao,
    Ingo
    Ingo R. Homann, May 3, 2007
    #4
  5. Kaiser S.

    Daniel Pitts Guest

    On May 3, 6:41 am, "Kaiser S." <> wrote:
    > Ingo R. Homann a écrit :
    >
    >
    >
    > > Hi,

    >
    > > Kaiser S. wrote:
    > >> What do you think of this code ? Is there a better way do enforce this
    > >> kind of cast ?

    >
    > >> public static <K, V> TreeMap<K, V> treemap(Object o, Class<K>
    > >> keyClass, Class<V> valueClass) {
    > >> TreeMap<?, ?> tm = (TreeMap)o; // warning 1
    > >> for (Map.Entry<?, ?> couple : tm.entrySet()) {
    > >> keyClass.cast(couple.getKey());
    > >> valueClass.cast(couple.getValue());
    > >> }
    > >> return (TreeMap)o; // warning 2
    > >> }

    >
    > >> called with:

    >
    > >> TreeMap<String, Double> tm = treemap(o, String.class, Double.class);

    >
    > > You know that your code does not do anything, and that the following
    > > would do exactly the same?

    >
    > > public static <K, V> TreeMap<K, V> treemap(Object o) {
    > > return (TreeMap<K,V>)o; // warning
    > > }

    >
    > Well i hope not. I check the class of all the keys and values; you must
    > have seen it...
    >
    > Now the doc of Class.cast says it throw a ClassCastException if the cast
    > is invalid, so after the for loop, i can make the ugly cast because i'm
    > sure i won't get a ClassCastException somewhere else in my program.


    If you really want to check the types, I suggest using instanceof
    I'm kind of curious why you go through the effort. Whats going on that
    you have a TreeMap object thats not in a TreeMap type reference?
    Daniel Pitts, May 4, 2007
    #5
  6. Kaiser S.

    Kaiser S. Guest

    Daniel Pitts a écrit :
    > On May 3, 6:41 am, "Kaiser S." <> wrote:
    >> Ingo R. Homann a écrit :
    >>
    >>
    >>
    >>> Hi,
    >>> Kaiser S. wrote:
    >>>> What do you think of this code ? Is there a better way do enforce this
    >>>> kind of cast ?
    >>>> public static <K, V> TreeMap<K, V> treemap(Object o, Class<K>
    >>>> keyClass, Class<V> valueClass) {
    >>>> TreeMap<?, ?> tm = (TreeMap)o; // warning 1
    >>>> for (Map.Entry<?, ?> couple : tm.entrySet()) {
    >>>> keyClass.cast(couple.getKey());
    >>>> valueClass.cast(couple.getValue());
    >>>> }
    >>>> return (TreeMap)o; // warning 2
    >>>> }
    >>>> called with:
    >>>> TreeMap<String, Double> tm = treemap(o, String.class, Double.class);
    >>> You know that your code does not do anything, and that the following
    >>> would do exactly the same?
    >>> public static <K, V> TreeMap<K, V> treemap(Object o) {
    >>> return (TreeMap<K,V>)o; // warning
    >>> }

    >> Well i hope not. I check the class of all the keys and values; you must
    >> have seen it...
    >>
    >> Now the doc of Class.cast says it throw a ClassCastException if the cast
    >> is invalid, so after the for loop, i can make the ugly cast because i'm
    >> sure i won't get a ClassCastException somewhere else in my program.

    >
    > If you really want to check the types, I suggest using instanceof
    > I'm kind of curious why you go through the effort. Whats going on that
    > you have a TreeMap object thats not in a TreeMap type reference?


    I don't see how i can use instanceof in a general way. Trying to rewrite
    the method with instanceof doesn't seem possible.

    The object reference is because of a design flaw. We use a
    treemap<string, object> as a generic class/object, which can contains
    treemap:

    public void doStuff(TreeMap<String, Object> myGenericObject) {
    // instead of map1 = myGenericObject.getMap1();
    TreeMap<Integer, Double> map1 = treemap(myGenericObject.get("map1"),
    Integer.class, Double.class);
    }

    We have a lot of classes which overload the doStuff(...) method, and
    each one has its own properties. That's the rationale behind the design
    flaw.
    Kaiser S., May 4, 2007
    #6
  7. Kaiser S.

    Guest

    On May 3, 3:41 pm, "Kaiser S." <> wrote:
    > Now the doc of Class.cast says it throw a ClassCastException if the cast
    > is invalid, so after the for loop, i can make the ugly cast because i'm
    > sure i won't get a ClassCastException somewhere else in my program.


    Does it matter? Why do you try to force to get the exception? If
    something is wrong you would get one anyhow.

    Further, your check does not prevent you from getting into trouble.
    If some other part of the application still holds a reference to the
    map, that part can still insert objects into the map after you
    checked. So things can still blow up.

    To be on the save side you need to clone() the map first.
    TreeMap.clone() does a shallow copy, which should be good enough
    here. Then you could do the checks on the cloned map. And when you
    have to pass the resulting sanitized map around to other unsafe parts
    of the application consider wrapping it in
    Collections.checkedSortedMap().

    Fixing the design flaw (and getting the programmer demoted who came up
    with the great idea of making everything an Object) would be a much
    better ideas.
    , May 4, 2007
    #7
    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. Ahmed Moustafa

    HashMap vs TreeMap

    Ahmed Moustafa, Aug 9, 2003, in forum: Java
    Replies:
    2
    Views:
    46,365
    Roedy Green
    Aug 10, 2003
  2. Harry

    TreeMap problem...

    Harry, Sep 22, 2003, in forum: Java
    Replies:
    3
    Views:
    450
    Harry
    Sep 23, 2003
  3. Wendy S
    Replies:
    4
    Views:
    14,209
    Jim McMaster
    Feb 19, 2004
  4. Miguel Angel
    Replies:
    9
    Views:
    13,644
    Christian Kaufhold
    Apr 15, 2004
  5. hiwa
    Replies:
    2
    Views:
    414
    Tony Morris
    May 7, 2005
Loading...

Share This Page