Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » How to set a default value of an attribute in a subclass?
How to set a default value of an attribute in a subclass? [message #787494] Tue, 31 January 2012 16:42 Go to next message
Luca Gherardi is currently offline Luca GherardiFriend
Messages: 62
Registered: November 2010
Member
Hi all,

is it possible to set the default value of an attribute in a subclass?

For example:

class A
   String name;

class B extends A
class C extends A


How can I set the default values for the attribute name of the classes B and C?

Thanks!

[Updated on: Tue, 07 February 2012 10:06]

Report message to a moderator

Re: How to set a default value of an attribute in a subclass? [message #787507 is a reply to message #787494] Tue, 31 January 2012 17:01 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Luca,

Comments below.

On 31/01/2012 5:42 PM, Luca Mising name wrote:
> Hi all,
>
> how is it possible to set the default value of an attribute in a
> subclass?
No. It's stored on the feature itself in the Ecore model at the point
it's defined. It can't be modified.
>
> For example:
>
>
> class A
> String name;
>
> class B extends A
> class C extends A
>
>
> How can I set the default values for the attribute name of the classes
> B and C?
You can't. The best you could do is define operations on A that match
the signature of the generated getter and setter and the define two
different features in B and C, both named "name". Then each can have a
different default. Of course A won't reflectively know it has a feature
called name.
>
> Thanks!


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: How to set a default value of an attribute in a subclass? [message #958795 is a reply to message #787494] Fri, 26 October 2012 08:10 Go to previous messageGo to next message
jschoedt Mising name is currently offline jschoedt Mising nameFriend
Messages: 11
Registered: March 2010
Junior Member
I know this is an old post but I think there is a better way:

We have a feature "visibility" on our NamedObject. All sub-classes can can have its own default value using an Eannotation. In this case called "defaultValues" with a Details Entry "visibility -> 500".

index.php/fa/12088/0/


Then in the constructor of NamedObject we check for the annotation and set it:

  /**
   * <!-- begin-user-doc --> <!-- end-user-doc -->
   * @generated NOT
   */
  protected NamedObjectImpl() {
    super();
    EAnnotation annotation = eClass().getEAnnotation("map");
    if (annotation != null) {
      String vis = annotation.getDetails().get("defaultValues");
      setVisibility(Integer.parseInt(vis));
    }
  }
Re: How to set a default value of an attribute in a subclass? [message #959234 is a reply to message #958795] Fri, 26 October 2012 14:58 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
One problem with this is that the feature is considered set (eIsSet will
return true) but a newly created object should have eIsSet false for all
of its features. Worse yet, if you set it back to the real default,
nothing will be serialized and when you deserialize, it will take on
this artificial default, i.e., setting to the real default won't
preserve it's state when you serialize and then serialize (or similarly
when copying)


On 26/10/2012 10:10 AM, jschoedt Mising name wrote:
> I know this is an old post but I think there is a better way:
>
> We have a feature "visibility" on our NamedObject. All sub-classes can can have its own default value using an Eannotation. In this case called "defaultValues" with a Details Entry "visibility -> 500".
>
>
>
>
> Then in the constructor of NamedObject we check for the annotation and set it:
>
>
> /**
> * <!-- begin-user-doc --> <!-- end-user-doc -->
> * @generated NOT
> */
> protected NamedObjectImpl() {
> super();
> EAnnotation annotation = eClass().getEAnnotation("map");
> if (annotation != null) {
> String vis = annotation.getDetails().get("defaultValues");
> setVisibility(Integer.parseInt(vis));
> }
> }
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:unary reference f, fESet=true, despite setting feature to null, is this correct?
Next Topic:save resource as instance of org.w3c.dom.Document
Goto Forum:
  


Current Time: Wed Apr 24 20:00:34 GMT 2024

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

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

Back to the top