views:

289

answers:

1

I want to clone an object of a class (whose source code I do not have) and copy all of the associated event handlers from the original object to the new cloned object. Does anyone know how I can do that? I know how to copy the properties from the original to the new, but can I iterate over all the event handlers and add them to the new object?

Any ideas?

A: 

I've used ObjectUtil.copy() to clone objects before, but only simple value objects. According to the langref, it is only meant for such uses:

This method is designed for copying data objects, such as elements of a collection. It is not intended for copying a UIComponent object, such as a TextInput control. If you want to create copies of specific UIComponent objects, you can create a subclass of the component and implement a clone() method, or other method to perform the copy.

Have you tried ObjectUtil.copy() and found it lacking?

houser2112
The class that I'm cloning to is a subclass of the original, e.g. lets say I have a class Feline and a class Tiger that inherits from Feline and just adds some more capabilities to it. So now at runtime I am given a Feline object and I want to create a Tiger object from that Feline object, how do I make Tiger get all of the Feline objects event handlers etc. I tried doing something like "super=felineObj" but that doesn't work.
RR
If Feline's event handlers are added in a constructor, you could get them by calling Feline's constructor in Tiger's constructor (although if you don't have the source for Feline, you'd have no way of knowing that). If copy() doesn't work, then you'll probably have to add them manually.
houser2112
Houser2112, thanks for your reply, it seems that I'll have to add them manually. Do you know how I can list all the eventhandlers for an object?
RR
The only way I can see would be to loop through all possible events, calling willTrigger() or hasEventListener() with the appropriate type parameter. If you know what Feline's parent class is (if any), you can pare this list down.
houser2112
would copying using a bytearray clone eventlisteners? I never tried it though...
jonathanasdf