Home » Modeling » EMF » Add dynamic attribute to a static class
Add dynamic attribute to a static class [message #716882] |
Thu, 18 August 2011 16:40 |
|
Hi,
I've been trying to add a dynamic attribute to a static class:
EAttribute extension = Cdk5Package.Literals.CDK5_CONSUMES__OPERATION;
JbiPackage.Literals.CONSUMES.getEStructuralFeatures().add(extension);
But I get an issue when trying to set this feature since the eIsSet
method does not see this attribute as a dynamic one (getFeatureCount
returns the number of attributes, including the one I just added). This
leads to an infinite call stack.
What is the correct way to achieve that and ensure that the attribute is
seen as dynamic from inside the class?
--
http://mickaelistria.wordpress.com
http://twitter.com/#!/mickaelistria
http://www.petalslink.com
|
|
| |
Re: Add dynamic attribute to a static class [message #716895 is a reply to message #716889] |
Thu, 18 August 2011 17:40 |
|
>> I've been trying to add a dynamic attribute to a static class:
>>
>> EAttribute extension = Cdk5Package.Literals.CDK5_CONSUMES__OPERATION;
>> JbiPackage.Literals.CONSUMES.getEStructuralFeatures().add(extension);
> No, that's evil. That's like trying to add a field to a Java class
> dynamically...
Maybe evil, but for sure helpful! It is a kind of aspect-oriented
approach. My goal is to add 1 attribute to an existing "final" 3rd-party
model.
>> What is the correct way to achieve that and ensure that the attribute
>> is seen as dynamic from inside the class?
> You can only extend the class with a new dynamic class in a new dynamic
> package and introduce things to that new class. Then you have to create
> instances of that new class rather than of the base.
The issue is that I already have the object retrieved from 3rd-party
code. I am not responsible of its creation. Then I don't see how I could
"supertype" the provided item to my enriched type.
--
http://mickaelistria.wordpress.com
http://twitter.com/#!/mickaelistria
http://www.petalslink.com
|
|
| | | | | | | |
Re: Add dynamic attribute to a static class [message #719981 is a reply to message #717229] |
Mon, 29 August 2011 15:40 |
|
Guys,
Holidays helped me to clarify my needs, and I come with a workaround for
this use-case that does not imply modification of static models.
The idea is more to extend _instances_ of the metamodel rather than the
metamodel itself.
I found a kind of workaround:
Assuming I have a LegacyElement and I want to manipulate a ModernElement
that extends LegacyElement with an additional attibute modernAttribute.
Then I create a new ModernPackage that contains my ModernElement, and
when I retrieve an instanceof LegacyElement, I perform
((BasicEObjectImpl)element).eSetClass(ModernPackage.Literals.MODERN_ELEMENT).
Then the instance supports my new attribute, databinding works, and XML
serialization too. This is still static EMF.
The only issue I still have with this workaround is that, since it is
inheritance, I cannot add attributes from several independant sources.
Here is a new version of the issue: What I'd like to do now is to be
able to extend any _EObject_ (no more EClass) with any
EStructuralFeature, independently of its EClass. Is there something on
this topic in EMF, or in EMF-Facet ?
Regards,
--
http://mickaelistria.wordpress.com
http://twitter.com/#!/mickaelistria
http://www.petalslink.com
|
|
|
Re: Add dynamic attribute to a static class [message #720008 is a reply to message #719981] |
Mon, 29 August 2011 16:34 |
Ed Merks Messages: 33140 Registered: July 2009 |
Senior Member |
|
|
Mickael,
Dynamic extensions are fine and are well supported. However, changing
the type after the instance has already been created and seen by other
parts of the framework may well get you into trouble. For example, I
doubt CDO or Teneo would be happy to have the type of an instance morph
dynamically.
You can always define an EClass that extends a bunch of other EClasses
(though not a bunch of static ones) and hence combines features from
several sources.
Note that for models like Ecore, Java, XML Schema, and so on, the model
itself defines an extensibility mechanism, i.e., annotations. That's a
far better approach.
On 29/08/2011 8:40 AM, Mickael Istria wrote:
> Guys,
>
> Holidays helped me to clarify my needs, and I come with a workaround
> for this use-case that does not imply modification of static models.
> The idea is more to extend _instances_ of the metamodel rather than
> the metamodel itself.
>
>
> I found a kind of workaround:
> Assuming I have a LegacyElement and I want to manipulate a
> ModernElement that extends LegacyElement with an additional attibute
> modernAttribute. Then I create a new ModernPackage that contains my
> ModernElement, and when I retrieve an instanceof LegacyElement, I
> perform
> ((BasicEObjectImpl)element).eSetClass(ModernPackage.Literals.MODERN_ELEMENT).
>
> Then the instance supports my new attribute, databinding works, and
> XML serialization too. This is still static EMF.
> The only issue I still have with this workaround is that, since it is
> inheritance, I cannot add attributes from several independant sources.
>
>
> Here is a new version of the issue: What I'd like to do now is to be
> able to extend any _EObject_ (no more EClass) with any
> EStructuralFeature, independently of its EClass. Is there something on
> this topic in EMF, or in EMF-Facet ?
>
> Regards,
>
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
| |
Goto Forum:
Current Time: Wed Apr 24 16:34:22 GMT 2024
Powered by FUDForum. Page generated in 0.04603 seconds
|