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 Gherardi
Messages: 60
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 Merks
Messages: 26054
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!
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 Missing name is currently offline jschoedt Missing name
Messages: 10
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 Merks
Messages: 26054
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));
> }
> }
>
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: Fri Sep 19 17:58:38 GMT 2014

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

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