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.