Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Generation of field initializers and methods body at JVM Inferring
Generation of field initializers and methods body at JVM Inferring [message #757399] Fri, 18 November 2011 09:17 Go to next message
rjarana Missing name is currently offline rjarana Missing name
Messages: 12
Registered: November 2011
Junior Member
Hello

I'm starting with XText and I'm a bit lost. I want to create an initializer for fields with a default value and generate body for methods with "default code". How can I archived this?

I found that an initializer can be assigned to JvmField with "initilalization()", but I don't now how to create the parameter "strategy" that is of type Function1<ImportManager, ? extends CharSequence>.

Thanks!
Re: Generation of field initializers and methods body at JVM Inferring [message #757534 is a reply to message #757399] Fri, 18 November 2011 19:28 Go to previous messageGo to next message
Max Goltzsche is currently offline Max Goltzsche
Messages: 40
Registered: November 2011
Member
Hey,

I am also new to Xtext but it seems to be a closure, an inline function. At that point, you should be able to implement Function1<ImportManager, ? extends CharSequence> by writing
[importMngr, str | doYourInitializationThing(importMngr, str)]
.

regards,
Max
Re: Generation of field initializers and methods body at JVM Inferring [message #757537 is a reply to message #757534] Fri, 18 November 2011 19:36 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6388
Registered: July 2009
Senior Member
No,

it is actually (in a dummy case hardcoded)

[im|'''1.0''']


you may have a look at org.eclipse.xtext.xtend2.jvmmodel.Xtend2JvmModelInferrer.transform(XtendFunction, JvmGenericType)

~Christian

[Updated on: Fri, 18 November 2011 19:39]

Report message to a moderator

Re: Generation of field initializers and methods body at JVM Inferring [message #757540 is a reply to message #757537] Fri, 18 November 2011 20:17 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6388
Registered: July 2009
Senior Member
And here a example with a real dynamic expression

val field = f.toField(f.name, f.type)
field.initialization([im|{val x = new StringBuilderBasedAppendable(im)
xbaseCompiler.toJavaExpression(f.initExpression,x)
'''«x.toString»'''
}])
Re: Generation of field initializers and methods body at JVM Inferring [message #757655 is a reply to message #757540] Mon, 21 November 2011 07:47 Go to previous messageGo to next message
rjarana Missing name is currently offline rjarana Missing name
Messages: 12
Registered: November 2011
Junior Member
It works fantastic, thank you guys!
Re: Generation of field initializers and methods body at JVM Inferring [message #936593 is a reply to message #757655] Mon, 08 October 2012 06:15 Go to previous messageGo to next message
Alexey Romanov is currently offline Alexey Romanov
Messages: 263
Registered: May 2010
Senior Member
By Xtext 2.3.1 things have changed a bit. In case it's useful to somebody:
toField(name, type) [
  val Procedure1<ITreeAppendable> proc = [append('''new java.util.BitSet(«dataDef.fields.size»)''')]
  initializer = proc
]
Re: Generation of field initializers and methods body at JVM Inferring [message #936595 is a reply to message #757655] Mon, 08 October 2012 06:17 Go to previous messageGo to next message
Alexey Romanov is currently offline Alexey Romanov
Messages: 263
Registered: May 2010
Senior Member
By Xtext 2.3.1 things have changed a bit. In case it's useful to somebody:
toField(name, type) [
  val Procedure1<ITreeAppendable> proc = [append('''new java.util.BitSet(«dataDef.fields.size»)''')]
  initializer = proc
]
Re: Generation of field initializers and methods body at JVM Inferring [message #936599 is a reply to message #936595] Mon, 08 October 2012 06:20 Go to previous messageGo to next message
Alexey Romanov is currently offline Alexey Romanov
Messages: 263
Registered: May 2010
Senior Member
By the way, could I do anything if I wanted to add BitSet to imports and write simply "new BitSet"?
Re: Generation of field initializers and methods body at JVM Inferring [message #937670 is a reply to message #936599] Tue, 09 October 2012 06:58 Go to previous messageGo to next message
Pascal Degenne is currently offline Pascal Degenne
Messages: 5
Registered: September 2012
Junior Member
Hi,

I have been experimenting similar things recently.
Just a hint from what I understood : I guess you could at least generate the import statement using an injected TypeReferenceSerializer. The serialize() method appends the type name to the TreeAppendable but also deals with the import manager if needed.

I mean something like the following :

@Inject extension TypeReferenceSerializer

...

f.toField(name, type) [
  val Procedure1<ITreeAppendable> proc = [
     append('''new ''')
     newTypeRef('java.util.BitSet').serialize(f,it)
     append('''(«dataDef.fields.size»)''')
    ]
  initializer = proc
]


Regards,
Pascal

[Updated on: Tue, 09 October 2012 13:16]

Report message to a moderator

Re: Generation of field initializers and methods body at JVM Inferring [message #937699 is a reply to message #937670] Tue, 09 October 2012 07:30 Go to previous message
Alexey Romanov is currently offline Alexey Romanov
Messages: 263
Registered: May 2010
Senior Member
I see, thanks!
Previous Topic:Adding Perspective to Xtext
Next Topic:Adding validation errors (and handling exceptions) in JvmModelInferrer
Goto Forum:
  


Current Time: Sat Oct 25 00:58:30 GMT 2014

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

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