Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » NotificationImpl.wasSet() ignores the feature default value when it's null(Related with EEnum default and how this affects the ChangeRecorder)
NotificationImpl.wasSet() ignores the feature default value when it's null [message #1451321] Thu, 23 October 2014 17:03 Go to next message
Silvestre Martins is currently offline Silvestre MartinsFriend
Messages: 84
Registered: July 2009
Member
I have model were some features of type Enum where added without any default defined.

The model generates fine and it's able to serialize and deserialize properly.

However, there is some code that simply ignores that the "null" is a valid value for the default of an Enum feature:

class EStructuralFeatureImpl:

public Object getDefaultValue()
  {
    EClassifier eType = getEType();
    String literal = getDefaultValueLiteral();

    if (literal == null && eType != null)
    {
      return isMany()? null : eType.getDefaultValue();
    }
...


As one can see in the code above, in case the default literal defined in the model for this feature is null, it returns the default value of the type itself:
» eType.getDefaultValue()

These leads to strange effects in the model.
Imagine this scenario:

I have an EEnum defined like this:
enum ValidationState { INVALID, VALID}

where the default value of the type is INVALID

and a feature of that type with name "state", that generates the following code:

protected static final ValidationState STATE_EDEFAULT = null;

protected ValidationState state = STATE_EDEFAULT ;

public ValidationState getState() {...}

public void setState(ValidationState) {...}


if we call the setState() with "INVALID", it will issue a NotificationImpl where wasSet() returns false, which is not correct, because comparing the value "INVALID" with the feature default "null", they are different.

So my question: is the "null" a valid value as a default for an Enum feature?
If not, shouldn't it fail to generate or simply assume the default of the Enum type?

But if it's a valid value, then the ChangeRecorder will break in this case because when the NotificationImpl.wasSet() returns true, the revert of changes calls the eUnSet() which then reverts the feature to its generated default (in this case, "null"), and not to the value that was set before the setter was called.

Re: NotificationImpl.wasSet() ignores the feature default value when it's null [message #1451689 is a reply to message #1451321] Fri, 24 October 2014 06:36 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 30372
Registered: July 2009
Senior Member
Silvestre,

Comments below.

On 23/10/2014 7:03 PM, Silvestre Martins wrote:
> I have model were some features of type Enum where added without any
> default defined.
Enums are treated like primitives. I.e., you might not define a default
value for a feature of type EInt, but the intrinsic default value of
that type is 0. Enums are treated similarly, i.e., the first enum
literal is the default when no other is specific.
>
> The model generates fine and it's able to serialize and deserialize
> properly.
>
> However, there is some code that simply ignores that the "null" is a
> valid value for the default of an Enum feature:
>
> class EStructuralFeatureImpl:
>
>
> public Object getDefaultValue()
> {
> EClassifier eType = getEType();
> String literal = getDefaultValueLiteral();
>
> if (literal == null && eType != null)
> {
> return isMany()? null : eType.getDefaultValue();
> }
> ..
>
>
> As one can see in the code above, in case the default literal defined
> in the model for this feature is null, it returns the default value of
> the type itself:
> » eType.getDefaultValue()
Yes.
>
> These leads to strange effects in the model.
> Imagine this scenario:
>
> I have an EEnum defined like this:
> enum ValidationState { INVALID, VALID}
> where the default value of the type is INVALID
Yes, the first literal.
>
> and a feature of that type with name "state", that generates the
> following code:
>
>
> protected static final ValidationState STATE_EDEFAULT = null;
That seems strange to me. For me EEnums generate such code:

/**
* The default value of the '{@link #getFeatureDelegation()
<em>Feature Delegation</em>}' attribute.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @see #getFeatureDelegation()
* @generated
* @ordered
*/
protected static final GenDelegationKind FEATURE_DELEGATION_EDEFAULT
= GenDelegationKind.NONE_LITERAL;


>
> protected ValidationState state = STATE_EDEFAULT ;
>
> public ValidationState getState() {...}
>
> public void setState(ValidationState) {...}
>
>
> if we call the setState() with "INVALID", it will issue a
> NotificationImpl where wasSet() returns false, which is not correct,
> because comparing the value "INVALID" with the feature default "null",
> they are different.
Did you modify the generated code? I don't expect to see null as a
default for an EEnum.
>
> So my question: is the "null" a valid value as a default for an Enum
> feature?
Not what I expect now.
> If not, shouldn't it fail to generate or simply assume the default of
> the Enum type?
I can't reproduce this.
>
> But if it's a valid value, then the ChangeRecorder will break in this
> case because when the NotificationImpl.wasSet() returns true, the
> revert of changes calls the eUnSet() which then reverts the feature to
> its generated default (in this case, "null"), and not to the value
> that was set before the setter was called.
What was the value before the call?
>
>
Re: NotificationImpl.wasSet() ignores the feature default value when it's null [message #1451801 is a reply to message #1451689] Fri, 24 October 2014 09:51 Go to previous messageGo to next message
Silvestre Martins is currently offline Silvestre MartinsFriend
Messages: 84
Registered: July 2009
Member
Hi Ed,

1) I didn't modify the generated code - it generates this (another example):

    
    /**
     * The default value of the '{@link #getFomOsnrState() <em>Fom Osnr State</em>}' attribute.
     * <!-- begin-user-doc -->
     * <!-- end-user-doc -->
     * @see #getFomOsnrState()
     * @generated
     * @ordered
     */
    protected static final ELightpathState FOM_OSNR_STATE_EDEFAULT = null;


I believe this is because I didn't defined any default for the "fomOsnrState" feature. In the feature properties I have the "Default Value Literal" field empty and in the ecore model the attribute defaultValueLiteral is absent.

2) Sorry, I forgot to mention what was the value before set, which is actually the most relevant one for this analysis: the value was "INVALID", which is the Enum default, but not the feature default. It doesn't matter then what is the value being set, because the notification is always issued regardless if the value being set is already equals to the current value.


Re: NotificationImpl.wasSet() ignores the feature default value when it's null [message #1451810 is a reply to message #1451801] Fri, 24 October 2014 10:09 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 30372
Registered: July 2009
Senior Member
Silvestre,

Comments below.

On 24/10/2014 11:51 AM, Silvestre Martins wrote:
> Hi Ed,
>
> 1) I didn't modify the generated code - it generates this (another
> example):
>
> /**
> * The default value of the '{@link #getFomOsnrState() <em>Fom Osnr
> State</em>}' attribute.
> * <!-- begin-user-doc -->
> * <!-- end-user-doc -->
> * @see #getFomOsnrState()
> * @generated
> * @ordered
> */
> protected static final ELightpathState FOM_OSNR_STATE_EDEFAULT = null;
I can only find examples like this:

protected static final RepositoryType TYPE_EDEFAULT =
RepositoryType.COMBINED;

There must be something you're not telling me. I'll need to see your
model...
>
>
> I believe this is because I didn't defined any default for the
> "fomOsnrState" feature.
No, that can't be it. For an EEnum, there is always a default.
> In the feature properties I have the "Default Value Literal" field
> empty and in the ecore model the attribute defaultValueLiteral is absent.
Yes, all my examples are like that too.
>
> 2) Sorry, I forgot to mention what was the value before set, which is
> actually the most relevant one for this analysis: the value was
> "INVALID", which is the Enum default, but not the feature default.
Are you actually modeling an EEnum?
> It doesn't matter then what is the value being set, because the
> notification is always issued regardless if the value being set is
> already equals to the current value.
Indeed, but given I can't reproduce what you show, I'll need to
understand why you have this kind of behavior...

Can you show your EEnum definition and your use of it? Perhaps I could
suggest to create a simple new model with just an EEnum, and an EClass
with an EAttribute of that type and look at what's generated for that.
If that appears wrong, you can open a bug with that attached. If it
works (as I expect) perhaps that will suggest how what you have is
different from that (and from what I expect).
>
>
>
Previous Topic:Customize XML deserialization so old project can be loaded into modified model
Next Topic:TreeViewer is not updating as exected
Goto Forum:
  


Current Time: Mon Aug 26 09:40:09 GMT 2019

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

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

Back to the top