Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Strange difference between ModelInferrer and ModelGenerator(Why is a member of an element in ModelInferrer != null but in ModelGenerator == null?)
Strange difference between ModelInferrer and ModelGenerator [message #1441102] Thu, 09 October 2014 05:02 Go to next message
Eclipse UserFriend
Hi,

because I'm wavered using ModelInferrer or the ModelGenerator I'm using at the moment both to determine the pros and cons of each way.

My grammar has the following passage to support inheritance:
(abstract='abstract')? 'entity' name=ID ('extends' superType=JvmTypeReference)? ('implements' interfaceTypes+=JvmTypeReference (',' interfaceTypes+=JvmTypeReference)*)?


The corresponding passage of my ModelGenerator is:
public «IF element?.methods.size > 0»abstract«ENDIF» class «element.name»«IF element.superType != null» extends «element.superType.type.identifier»«ENDIF»«IF element.interfaceTypes.size > 0» implements «FOR intfc : element.interfaceTypes SEPARATOR ','»«intfc.type.identifier»«ENDFOR»«ENDIF»


And in the ModelInferrer I'm setting superTypes by this:
if(element.superType != null) {
  superTypes += element.superType
}
for(interface : element.interfaceTypes) {
  superTypes += interface
}


I tested with this code-snippet:
entity Adresse extends Superclass implements Serializable


When I test this feature the code of the ModelInferrer works for the superType and creates the corresponding extends-Statement, but the ModelGenerator doesn't. I debugged my ModelGenerator and there is element.superType null.

Another problem is that the lines for adding the interfaces to superTypes in ModelInferrer results in java.util.ConcurrentModificationException.

Can anybody explain this in my opinion strange behaviour?

Why is element.superType in ModelInferrer != null but in ModelGenerator == null?
What is wrong that I get java.util.ConcurrentModificationExceptions when I'm adding interfaces to superTypes?
Re: Strange difference between ModelInferrer and ModelGenerator [message #1441110 is a reply to message #1441102] Thu, 09 October 2014 05:15 Go to previous messageGo to next message
Eclipse UserFriend
Hi i think you should clone there: element.superType
Re: Strange difference between ModelInferrer and ModelGenerator [message #1441116 is a reply to message #1441110] Thu, 09 October 2014 05:25 Go to previous messageGo to next message
Eclipse UserFriend
Quote:
Hi i think you should clone there: element.superType


Thanks. It works now.

Do you also have an idea why I'm getting the java.util.ConcurrentModificationException?
Re: Strange difference between ModelInferrer and ModelGenerator [message #1441195 is a reply to message #1441116] Thu, 09 October 2014 07:34 Go to previous messageGo to next message
Eclipse UserFriend
I've found the problem. If I use a classic for-loop everything works fine. So ist should be an error in Xtend.

So this Xtend-Code works:
for(var i=0; i < element.interfaceTypes.size; i++) {
  superTypes += element.interfaceTypes.get(i)
}

Corresponding generated Java-Code:
for (int i = 0; (i < element.getInterfaceTypes().size()); i++) {
  EList<JvmTypeReference> _superTypes_1 = it.getSuperTypes();
  EList<JvmTypeReference> _interfaceTypes = element.getInterfaceTypes();
  JvmTypeReference _get = _interfaceTypes.get(i);
  PersistenceServiceJvmModelInferrer.this._jvmTypesBuilder.<JvmTypeReference>operator_add(_superTypes_1, _get);
}


This Xtend-Code will cause a java.util.ConcurrentModificationException:
for(intfc : element.interfaceTypes) {
  superTypes += intfc
}

Corresponding generated Java-Code
EList<JvmTypeReference> _interfaceTypes = element.getInterfaceTypes();
for (final JvmTypeReference intfc : _interfaceTypes) {
  EList<JvmTypeReference> _superTypes_1 = it.getSuperTypes();
  PersistenceServiceJvmModelInferrer.this._jvmTypesBuilder.<JvmTypeReference>operator_add(_superTypes_1, intfc);
}
Re: Strange difference between ModelInferrer and ModelGenerator [message #1441412 is a reply to message #1441195] Thu, 09 October 2014 13:50 Go to previous message
Eclipse UserFriend
It's not an error in Xtend but you are modifying your model by accident
in both cases. The classic for-loop just hides the exception from you.
If you use the JvmTypesBuilder as an extension in your class, the +=
will do an implicit clone of your type references rather than moving it
out of place. Generally speaking, you should strictly avoid to modify
your source model in the model inferrer. Christian already tried to
suggest that you use
superTypes += intf.cloneWithProxies
rather than adding it directly as a supertype.

Hope that helps,
Sebastian
--
Looking for professional support for Xtext, Xtend or Eclipse Modeling?
Go visit: http://xtext.itemis.com
Previous Topic:Automatically indent while typing
Next Topic:Annoying popup when editing model (after 2 key strokes!) with Xtext 2.7
Goto Forum:
  


Current Time: Sat Jul 05 07:47:18 EDT 2025

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

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

Back to the top