Casting an object to a genericized TreeMap

K

Kaiser S.

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);
 
I

Ingo R. Homann

Hi,
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
 
K

Kaiser S.

Ingo R. Homann a écrit :
Hi,


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.
 
I

Ingo R. Homann

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
 
D

Daniel Pitts

Ingo R. Homann a écrit :







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?
 
K

Kaiser S.

Daniel Pitts a écrit :
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.
 
A

a24900

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top