Skip to main content



      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 04:17 Go to next message
Eclipse UserFriend
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 14:28 Go to previous messageGo to next message
Eclipse UserFriend
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 14:36 Go to previous messageGo to next message
Eclipse UserFriend
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 14:39] by Moderator

Re: Generation of field initializers and methods body at JVM Inferring [message #757540 is a reply to message #757537] Fri, 18 November 2011 15:17 Go to previous messageGo to next message
Eclipse UserFriend
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 02:47 Go to previous messageGo to next message
Eclipse UserFriend
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 02:15 Go to previous messageGo to next message
Eclipse UserFriend
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 02:17 Go to previous messageGo to next message
Eclipse UserFriend
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 02:20 Go to previous messageGo to next message
Eclipse UserFriend
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 02:58 Go to previous messageGo to next message
Eclipse UserFriend
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 09:16] by Moderator

Re: Generation of field initializers and methods body at JVM Inferring [message #937699 is a reply to message #937670] Tue, 09 October 2012 03:30 Go to previous message
Eclipse UserFriend
I see, thanks!
Previous Topic:Adding Perspective to Xtext
Next Topic:Provide model at runtime
Goto Forum:
  


Current Time: Tue Jul 22 20:49:16 EDT 2025

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

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

Back to the top