
Signature
========================================================================
Ian Pilcher i.pilcher@comcast.net
========================================================================
> Given a Map that is Cloneable, is it possible to actually clone the damn
> thing without resorting to reflection?
In general, no. Cloneable has no public methods. However a specific
subtype (say java.util.HashMap), may make the clone method public.
Alternatively, you could just copy it.
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Ian Pilcher wrote On 10/06/05 16:23,:
> Given a Map that is Cloneable, is it possible to actually clone the damn
> thing without resorting to reflection?
Um, er, have you tried the clone() method? ;-)
You can't invoke clone() through a reference of type
Map or AbstractMap, because they don't implement Cloneable.
But if you've got a HashMap reference, say, you can use
clone() on it with no trouble.
(Ian surely knows all this, so what's his difficulty?
Hmmm...)
Maybe you've got a Map or AbstractMap reference and
you want to duplicate it. You probably use instanceof
to discover that it implements Cloneable, and then you
run into the problem of needing to discover a more specific
class you can downcast it to in order to make the clone()
call. Okay, the only way I can imagine doing that in full
generality is to use reflection.
But you probably have some kind of fallback procedure
in case the Map turns out not to be Cloneable, and unless
that procedure is just "I can't do that, Dave" it would
presumably work on a Cloneable Map just as well as on a
non-Cloneable one. Why not just use the fallback for all
Maps, and not bother with clone() at all? Efficiency,
maybe? If so, have you measured the performance hit? If
you *have* measured an unacceptable performance hit and
you're dead-set on using clone() when possible, why so
reluctant to use reflection if that's what's needed?
Maybe a little more background on what you're trying
to do with this Map would elicit some better ideas ...

Signature
Eric.Sosman@sun.com
Ian Pilcher - 06 Oct 2005 22:13 GMT
> Maybe you've got a Map or AbstractMap reference and
> you want to duplicate it. You probably use instanceof
[quoted text clipped - 3 lines]
> call. Okay, the only way I can imagine doing that in full
> generality is to use reflection.
You are correct.
I am working on a "TransactionalMap" that will wrap a Map and allow
it to be "rolled back" to a previous state. In the future, I may
track invididual updates, but it seemed like it would be simpler to
simply clone the underlying map the first time it is modified during a
transaction.
I'd very much like to create a general wrapper, rather than a
TransactionalHashMap, TransactionalTreeMap, TransactionalLinkedHashMap,
etc., etc.
> But you probably have some kind of fallback procedure
> in case the Map turns out not to be Cloneable, and unless
[quoted text clipped - 6 lines]
> you're dead-set on using clone() when possible, why so
> reluctant to use reflection if that's what's needed?
No fallback procedure. I check the underlying map in my constructor and
throw an IllegalArgumentException if it's not Cloneable.

Signature
========================================================================
Ian Pilcher i.pilcher@comcast.net
========================================================================