Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » [xtend] Generic return type for EOperation
[xtend] Generic return type for EOperation [message #700324] Sat, 23 July 2011 07:34 Go to next message
Timotei Mising name is currently offline Timotei Mising nameFriend
Messages: 89
Registered: March 2010
Member
Hi!

I have a question. How can I make a generic return type for an operation? I could successfully create a new operation with already existing EParameters, but not this.

For example, I will need to write something like:

public List<MyGrammarEntity> getEntities()
{
 // create the new list and return
}


How is that possible? I've tried to look on th existing generated feature. For example
for my grammar element I have something like:
public EList<MyGrammarEntity> get...()

But I couldn't find the EList anywhere.

Is this even possible? If yes, could anyone give me some tips on how to tackle that?

I've seen that if I open the .ecore with eclipse, I can set the type to something generic, like a Map, but I want that to be a List or something like that. Also I couldn't figure on how to specify the generic type.

Thanks.
Re: [xtend] Generic return type for EOperation [message #700347 is a reply to message #700324] Sat, 23 July 2011 08:35 Go to previous message
Timotei Mising name is currently offline Timotei Mising nameFriend
Messages: 89
Registered: March 2010
Member
Ah. I found the EList. It was called: EEList (with double E ) Smile).

I managed to get this working Very Happy. Here is the code in case someone else will want to do the same:


GrammarPostProcessor.ext:
import ecore;
import xtext;

process( GeneratedMetamodel this ):
    process( ePackage );

process( EPackage this ):
    eClassifiers.process();

process( EClassifier this ):
    null;

process( EClass this ):
    eStructuralFeatures.process() ->
     
    // enrich the grammar elements with schema-specific attributes
    if name == "WMLExpression" then {

        createOperation( "isWMLKey", 
            "return ( this instanceof WMLKey );", eboolean() ) ->

        createGenericOperation( "getWMLTags", "return null;", ecoreType( "EEList" ),
            wmlType( "WMLTag" ) )
    };

createOperation( EClass this, String name, String body, EClassifier returnType ):
    let op = newOperation( name, returnType) : newAnnotation( op, body ); 

create EOperation this newOperation( EClass owner, String name, EClassifier returnType ) :
    setName( name ) -> setEType( returnType ) ->
    owner.eOperations.add( this );

createGenericOperation( EClass this, String name, String body, EClassifier genericType,
        EClassifier typeArgument ):
    let op = newGenericOperation( name, createGenericType( genericType, typeArgument ) ) 
        : newAnnotation( op, body ); 

create EGenericType this createGenericType( EClassifier type, EClassifier argumentType ):
    let genType = new EGenericType : 
        setEClassifier( type ) ->
        eTypeArguments.add( let gen = new EGenericType : gen.setEClassifier( argumentType ) );

create EOperation this newGenericOperation( EClass owner, String name, EGenericType type ):
    setName( name ) -> setEGenericType( type ) ->
    owner.eOperations.add( this );

create EAnnotation this newAnnotation( EOperation op, String value ):
    let an = new EStringToStringMapEntry :
        setSource( "http://www.eclipse.org/emf/2002/GenModel" ) ->
        an.setKey( "body" ) -> 
        an.setValue( value ) ->
        details.add( an ) ->
        op.eAnnotations.add( this );
    

EDataType estring(): ecoreType( "EString" );
EDataType echar(): ecoreType( "EChar" );
EDataType eboolean(): ecoreType( "EBoolean" );

EClassifier wmlType( String name ):
    wmlPackage().getEClassifier( name );

EPackage wmlPackage():
    JAVA org.wesnoth.wml.impl.WmlPackageImpl.init();

EClassifier ecoreType( String name ) :
    ecorePackage().getEClassifier( name );

EPackage ecorePackage(): 
    JAVA org.eclipse.emf.ecore.impl.EcorePackageImpl.init();


PS: is it possible to upgrade the PostProcessor to xtend2?

[Updated on: Sat, 23 July 2011 08:48]

Report message to a moderator

Previous Topic:[Xtext] Multiple objects in one line
Next Topic:Single Character compete versus INT and ID
Goto Forum:
  


Current Time: Thu Apr 25 14:30:52 GMT 2024

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

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

Back to the top