Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

Casting an object to a genericized TreeMap

Thread view: 
Kaiser S. - 03 May 2007 14:08 GMT
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);
Ingo R. Homann - 03 May 2007 14:18 GMT
Hi,

> What do you think of this code ? Is there a better way do enforce this
> kind of cast ?
[quoted text clipped - 13 lines]
>
> 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
Kaiser S. - 03 May 2007 14:41 GMT
Ingo R. Homann a écrit :
> Hi,
>
[quoted text clipped - 21 lines]
>  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.
Ingo R. Homann - 03 May 2007 14:46 GMT
Hi Kevin S., ;-)

> 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
Daniel Pitts - 04 May 2007 02:14 GMT
> Ingo R. Homann a écrit :
>
[quoted text clipped - 30 lines]
> 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?
Kaiser S. - 04 May 2007 09:04 GMT
Daniel Pitts a écrit :
>> Ingo R. Homann a écrit :
>>
[quoted text clipped - 27 lines]
> 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.
a24900@googlemail.com - 04 May 2007 19:36 GMT
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.