[xtend] Generic return type for EOperation [message #700324] |
Sat, 23 July 2011 03:34  |
Eclipse User |
|
|
|
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 04:35  |
Eclipse User |
|
|
|
Ah. I found the EList. It was called: EEList (with double E ) ).
I managed to get this working . 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 04:48] by Moderator
|
|
|
Powered by
FUDForum. Page generated in 0.24898 seconds