Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Getting an EObject equivalent of a java object
Getting an EObject equivalent of a java object [message #524416] Wed, 31 March 2010 20:35 Go to next message
Greg Mising name is currently offline Greg Mising nameFriend
Messages: 47
Registered: July 2009
Member
I'm trying to create a set of EObjects from a set of Java Objects for those objects where either (a) it's already an instanceof EObject (that's a no brainer) or (b) there is an EClassifier with the right instanceClass.

In the case of (b) I can query my model's EPackage and EcorePackage to see if there's an existing EClassifier cls such that
cls.isInstance( object ) is true. Once I've determined that, how do I create an EObject instance of this EClassifer from this object - in essence I'd like to do something like:
cls.eCast( object ), which would return an EObject representation of the object.

In the simplest case, I'm passed a String. How do I turn it into an EString? I probably only need this for primitive types anyways so if there's a solution for that I would be very happy. Very Happy

Thanks.
Greg
Re: Getting an EObject equivalent of a java object [message #524423 is a reply to message #524416] Wed, 31 March 2010 20:46 Go to previous messageGo to next message
Jonas Helming is currently offline Jonas HelmingFriend
Messages: 699
Registered: July 2009
Senior Member
Hi,
EcoreFactory.eINSTANCE.create(eClass);
should do the trick

Greg wrote:
> I'm trying to create a set of EObjects from a set of Java Objects for
> those objects where either (a) it's already an instanceof EObject
> (that's a no brainer) or (b) there is an EClassifier with the right
> instanceClass.
>
> In the case of (b) I can query my model's EPackage and EcorePackage to
> see if there's an existing EClassifier cls such that
> cls.isInstance( object ) is true. Once I've determined that, how do I
> create an EObject instance of this EClassifer from this object - in
> essence I'd like to do something like:
> cls.eCast( object ), which would return an EObject representation of the
> object.
>
> In the simplest case, I'm passed a String. How do I turn it into an
> EString? I probably only need this for primitive types anyways so if
> there's a solution for that I would be very happy. :d
>
> Thanks.
> Greg
Re: Getting an EObject equivalent of a java object [message #524427 is a reply to message #524423] Wed, 31 March 2010 21:18 Go to previous messageGo to next message
Greg Mising name is currently offline Greg Mising nameFriend
Messages: 47
Registered: July 2009
Member
That just creates a new instance of the same EClass. What I need is a representation of the given Object, with some means to access its attribute values. Here's an example.
package test
public interface ISimpleClass {
public int getFoo();
public void setFoo( int newFoo );
}
public class SimpleClass implements ISimpleInterface {
....
}

Then there's some ecore model object defined as
<eClassifiers xsi:type="ecore:EClass" name="Simple" instanceClassName="test.ISimpleClass">
<eStructuralFeatures xsi:type="ecore:EAttribute" name="foo" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EInt"/>
</eClassifiers>

So when I'm passed an object of type ISimpleClass I want to convert it to an EObject of type Simple.

I already know how to query the EPackages to discover that EClass Simple has instanceClassName="test.ISimpleClass", which means that it implements ISimpleClass, but ISimpleClass objects don't necessarily implement EObject so I can't do a direct cast that way.

Maybe I need to create a new instance of Simple and then use reflection to copy over all of the stuff specific to the ISimpleClass interface? Is there not already some utility in eCore that will do this for me???

Greg


Jonas wrote on Wed, 31 March 2010 16:46
Hi,
EcoreFactory.eINSTANCE.create(eClass);
should do the trick

Greg wrote:
> I'm trying to create a set of EObjects from a set of Java Objects for
> those objects where either (a) it's already an instanceof EObject
> (that's a no brainer) or (b) there is an EClassifier with the right
> instanceClass.
>
> In the case of (b) I can query my model's EPackage and EcorePackage to
> see if there's an existing EClassifier cls such that
> cls.isInstance( object ) is true. Once I've determined that, how do I
> create an EObject instance of this EClassifer from this object - in
> essence I'd like to do something like:
> cls.eCast( object ), which would return an EObject representation of the
> object.
>
> In the simplest case, I'm passed a String. How do I turn it into an
> EString? I probably only need this for primitive types anyways so if
> there's a solution for that I would be very happy. Very Happy
>
> Thanks.
> Greg

Re: Getting an EObject equivalent of a java object [message #524444 is a reply to message #524427] Wed, 31 March 2010 23:53 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
Greg,

Comments below.

Greg wrote:
> That just creates a new instance of the same EClass. What I need is a
> representation of the given Object, with some means to access its
> attribute values.
It creates and EObject that is an instance. You can use eGet/eSet to
access the values or you can cast it to the generated API and access the
values that way.
> Here's an example.
> package test
> public interface ISimpleClass {
> public int getFoo();
> public void setFoo( int newFoo );
> }
> public class SimpleClass implements ISimpleInterface {
> .... }
>
> Then there's some ecore model object defined as <eClassifiers
> xsi:type="ecore:EClass" name="Simple"
> instanceClassName="test.ISimpleClass">
> <eStructuralFeatures xsi:type="ecore:EAttribute" name="foo"
> eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EInt"/>
> </eClassifiers>
>
> So when I'm passed an object of type ISimpleClass I want to convert it
> to an EObject of type Simple.
>
> I already know how to query the EPackages to discover that EClass
> Simple has instanceClassName="test.ISimpleClass", which means that it
> implements ISimpleClass, but ISimpleClass objects don't necessarily
> implement EObject so I can't do a direct cast that way.
What Jonas suggested will return a result you can cast to ISimpleClass
and which is also an EObject.
>
> Maybe I need to create a new instance of Simple and then use
> reflection to copy over all of the stuff specific to the ISimpleClass
> interface? Is there not already some utility in eCore that will do
> this for me???
All utilities in EMF will assume the objects implement EObject and use
reflection, so if you want to convert non-EObjects, you'll need to do
that in some other way, perhaps using Java reflection.

>
> Greg
>
>
> Jonas wrote on Wed, 31 March 2010 16:46
>> Hi,
>> EcoreFactory.eINSTANCE.create(eClass);
>> should do the trick
>>
>> Greg wrote:
>> > I'm trying to create a set of EObjects from a set of Java Objects
>> for > those objects where either (a) it's already an instanceof
>> EObject > (that's a no brainer) or (b) there is an EClassifier with
>> the right > instanceClass.
>> > > In the case of (b) I can query my model's EPackage and
>> EcorePackage to > see if there's an existing EClassifier cls such that
>> > cls.isInstance( object ) is true. Once I've determined that, how
>> do I > create an EObject instance of this EClassifer from this object
>> - in > essence I'd like to do something like:
>> > cls.eCast( object ), which would return an EObject representation
>> of the > object.
>> > > In the simplest case, I'm passed a String. How do I turn it into
>> an > EString? I probably only need this for primitive types anyways
>> so if > there's a solution for that I would be very happy. :d
>> > > Thanks.
>> > Greg
>
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:Same document reference question
Next Topic:Extracting partial EMF objects from a database
Goto Forum:
  


Current Time: Sat Apr 20 02:17:52 GMT 2024

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

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

Back to the top