|
|
|
Re: Getting an EObject equivalent of a java object [message #524444 is a reply to message #524427] |
Wed, 31 March 2010 23:53 |
Ed Merks 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/
|
|
|
Powered by
FUDForum. Page generated in 0.03096 seconds