Skip to main content



      Home
Home » Modeling » EMF » Is there an easy way to clone an object?
Is there an easy way to clone an object? [message #390465] Thu, 13 January 2005 15:53 Go to next message
Eclipse UserFriend
I have a model that contains the interface Foo and at runtime I
encounter an object ff which is an instance of a different
implementation of Foo. I want to create an instance of the EMF-generated
FooImpl.

Is there a "EMF-approved" way to do this or should I use beanutils [1]?

Thank you,
florin

1:
http://jakarta.apache.org/commons/beanutils/apidocs/org/apac he/commons/beanutils/PropertyUtils.html#copyProperties(java.lang.Object,%20java.lang.Object)
Re: Is there an easy way to clone an object? [message #390468 is a reply to message #390465] Thu, 13 January 2005 16:19 Go to previous messageGo to next message
Eclipse UserFriend
Florin Iucha wrote:
> I have a model that contains the interface Foo and at runtime I
> encounter an object ff which is an instance of a different
> implementation of Foo. I want to create an instance of the EMF-generated
> FooImpl.
>
> Is there a "EMF-approved" way to do this or should I use beanutils [1]?

Hi Florin,

EcoreUtil.copy(EObject) is the "EMF-approved" way to clone an EMF object.

It uses the factory to instantiate the copy of the object and then
reflectively initializes it. It performs a deep copy of the specified
object and its contents. It's implemented via EcoreUtil.Copier, which
can be customized.

I'm somewhat suspicious of this "other implementation" of Foo. It should
all work, as long as it supports the EObject interface (which Foo
extends) and returns the correct metaobject from eClass().

Cheers,
Dave
Re: Is there an easy way to clone an object? [message #390470 is a reply to message #390468] Thu, 13 January 2005 16:57 Go to previous messageGo to next message
Eclipse UserFriend
Dave Steinberg wrote:
> Florin Iucha wrote:
>
>> I have a model that contains the interface Foo and at runtime I
>> encounter an object ff which is an instance of a different
>> implementation of Foo. I want to create an instance of the
>> EMF-generated FooImpl.
>>
>> Is there a "EMF-approved" way to do this or should I use beanutils [1]?
>
>
> Hi Florin,
>
> EcoreUtil.copy(EObject) is the "EMF-approved" way to clone an EMF object.

Thank you, this is what I was looking for (albeit not necessarily what
will help me ;)).

>
> It uses the factory to instantiate the copy of the object and then
> reflectively initializes it. It performs a deep copy of the specified
> object and its contents. It's implemented via EcoreUtil.Copier, which
> can be customized.
>
> I'm somewhat suspicious of this "other implementation" of Foo. It should
> all work, as long as it supports the EObject interface (which Foo
> extends) and returns the correct metaobject from eClass().

The "other implementation" is the proxy that Hibernate instantiates. I
have not found a way to tell Hibernate to instantiate my class instead
of a proxy.

florin
Re: Is there an easy way to clone an object? [message #390472 is a reply to message #390470] Thu, 13 January 2005 17:55 Go to previous messageGo to next message
Eclipse UserFriend
Florin Iucha wrote:

> The "other implementation" is the proxy that Hibernate instantiates. I
> have not found a way to tell Hibernate to instantiate my class instead
> of a proxy.

Hi Florin,

I see. As I said, if the other implementation actually implements
EObject, including returning the right result for eClass(), then all
should be well. If not, I'm not sure what to suggest.

Hopefully Aleksander will let us know if you're on the wrong track. I
think you both know much more about using Hibernate than I do. ;)

Cheers,
Dave
Re: Is there an easy way to clone an object? [message #390473 is a reply to message #390472] Thu, 13 January 2005 18:55 Go to previous message
Eclipse UserFriend
Dave Steinberg wrote:
>> The "other implementation" is the proxy that Hibernate instantiates. I
>> have not found a way to tell Hibernate to instantiate my class instead
>> of a proxy.
>
> I see. As I said, if the other implementation actually implements
> EObject, including returning the right result for eClass(), then all
> should be well. If not, I'm not sure what to suggest.
>
> Hopefully Aleksander will let us know if you're on the wrong track. I
> think you both know much more about using Hibernate than I do. ;)

Of course I was on the wrong track!

By default Hibernate tries to be helpful so it materializes objects on a
need-to-use basis (aka lazy). The trick was to ask the classes to not be
lazy, and that produced real objects.

Soo many knobs to turn ;) However I learned about EcoreUtil.

Thank you,
florin
Previous Topic:Dynamic method invocation with reflection
Next Topic:Enumeration simple type with contained hyphens
Goto Forum:
  


Current Time: Sun Apr 20 17:53:57 EDT 2025

Powered by FUDForum. Page generated in 0.03178 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top