Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Combining the IJvmModelInferrer with custom code generation
Combining the IJvmModelInferrer with custom code generation [message #755749] Wed, 09 November 2011 13:21 Go to next message
Goran   is currently offline Goran Friend
Messages: 45
Registered: November 2011
Member
Hi,

I am extending Xbase and use the IJvmModelInferrer to map my language to JVM Concepts. This works quite amazing with the resulting generated Java code.
However there are some things that need custom code and cannot be simply mapped.
So my question is how can I customize the code generation already done by Xtext?

~Goran
Re: Combining the IJvmModelInferrer with custom code generation [message #755770 is a reply to message #755749] Wed, 09 November 2011 14:34 Go to previous messageGo to next message
Jan Koehnlein is currently offline Jan KoehnleinFriend
Messages: 760
Registered: July 2009
Location: Hamburg
Senior Member
What kind of things are that?

You can add method bodies and field initializers using
JvmTypesBuilder.setBody(JvmExecutable, Function1<ImportManager, ?
extends CharSequence>)
JvmTypesBuilder.initialization(JvmField, Function1<ImportManager, ?
extends CharSequence>)


Am 09.11.11 14:21, schrieb Goran:
> Hi,
>
> I am extending Xbase and use the IJvmModelInferrer to map my language to
> JVM Concepts. This works quite amazing with the resulting generated Java
> code.
> However there are some things that need custom code and cannot be simply
> mapped.
> So my question is how can I customize the code generation already done
> by Xtext?
>
> ~Goran


--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com


---
Get professional support from the Xtext committers at www.typefox.io
Re: Combining the IJvmModelInferrer with custom code generation [message #758372 is a reply to message #755770] Tue, 22 November 2011 22:03 Go to previous messageGo to next message
Goran   is currently offline Goran Friend
Messages: 45
Registered: November 2011
Member
What I mean is the following. I am using xtend2 for code generation for example:
	return element.toMethod(name, returnType)[
		for(p : element.advice.parameters.parameters) {
			parameters += p.toParameter(p.name, p.parameterType)
		}
		documentation = element.documentation
		final = element.^final
		body = element.body
	]


This causes a method like below to be generated:
  public void before_advice_method() {
      System.out.println("Advice 1");
      this.authenticator.authenticate();
  }

The body of the method can contain arbitrary Java statements from my DSL. I want to include my own code beside the already generated body of Java statements for example instead of "body = element.advice.body" I tried the following:

	body = ['''
		«element.body»
		System.out.println("Some generated custom statement besides the automatically generated statements");
	''']

However the «element.body» doesn't cause the proper generation of Java statements. The above leads to the following:
  public void before_advice_method() {
    org.eclipse.xtext.xbase.impl.XBlockExpressionImpl@4772dd46
    System.out.println("Some generated custom statement besides the automatically generated statements");
  }

instead of
  public void before_advice_method() {
      System.out.println("Advice 1");
      this.authenticator.authenticate();
      System.out.println("Some generated custom statement besides the automatically generated statements");
  }

How do I cause the XBlockExpressionImpl to be properly converted to Java statements?
Re: Combining the IJvmModelInferrer with custom code generation [message #758377 is a reply to message #758372] Tue, 22 November 2011 22:50 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13423
Registered: July 2009
Senior Member
Have a look at JvmModelGenerator and/or xbasecompiler

Regards Christian


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Combining the IJvmModelInferrer with custom code generation [message #758504 is a reply to message #758372] Wed, 23 November 2011 13:48 Go to previous messageGo to next message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1762
Registered: July 2009
Location: Firenze, Italy
Senior Member
On 11/22/2011 11:03 PM, Goran wrote:
> How do I cause the XBlockExpressionImpl to be properly converted to Java
> statements?

Hi

I've just blogged something similar/related:

"Xtext 2.1: using Xbase expressions"

http://www.rcp-vision.com/?p=1640

hope it helps
cheers
Lorenzo

--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
ICQ# lbetto, 16080134 (GNU/Linux User # 158233)
HOME: http://www.lorenzobettini.it MUSIC: http://www.purplesucker.com
http://www.myspace.com/supertrouperabba
BLOGS: http://tronprog.blogspot.com http://longlivemusic.blogspot.com
http://www.gnu.org/software/src-highlite
http://www.gnu.org/software/gengetopt
http://www.gnu.org/software/gengen http://doublecpp.sourceforge.net


Re: Combining the IJvmModelInferrer with custom code generation [message #758849 is a reply to message #758504] Thu, 24 November 2011 17:41 Go to previous messageGo to next message
Goran   is currently offline Goran Friend
Messages: 45
Registered: November 2011
Member
Thanks Lorenzo,

That's a great blog post indeed. You described a switch to your custom generator instead of the JvmModelGenerator. What was not very clear to me is if we could still use the JvmModelInferrer when using the custom code generator you described. Do you know if this is possible?

I was able to put the following into the JvmModelInferrer which helped:

...
return element.toMethod(name, returnType)[
	for(p : element.advice.parameters.parameters) {
		parameters += p.toParameter(p.name, p.parameterType)
	}
	documentation = element.documentation
	final = element.^final

	val importManager = new ImportManager(true, getJvmType(aspectFQN))
        val mainMethod = compile(element.advice.body, importManager)
        body = ['''
		«mainMethod»
		System.out.println("Some generated custom statement besides the automatically generated statements");
	''']
)]
....
def compile(XExpression xExpression, ImportManager importManager) {
	val result = new StringBuilderBasedAppendable(importManager)
	compiler.toJavaStatement(xExpression, result, true)
	result
}


However now I also would like to customize the import statements. I know how to do that according to your post however I would like to avoid throwing away my JvmModelInferrer xtend class.
Re: Combining the IJvmModelInferrer with custom code generation [message #758940 is a reply to message #758849] Fri, 25 November 2011 11:04 Go to previous messageGo to next message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1762
Registered: July 2009
Location: Firenze, Italy
Senior Member
On 11/24/2011 06:41 PM, Goran wrote:
> Thanks Lorenzo,
>
> That's a great blog post indeed. You described a switch to your custom

glad you enjoyed it :)

> generator instead of the JvmModelGenerator. What was not very clear to
> me is if we could still use the JvmModelInferrer when using the custom
> code generator you described. Do you know if this is possible?
>

well, I still haven't switched to JvmModelInferrer, but I think that
could be possible... actually I had pointed you to my post just for the
part concerning calling XbaseCompiler explicitly: I didn't mean to point
you to the part concerning using a custom code generator :)

> I was able to put the following into the JvmModelInferrer which helped:
>
>
> ..
> return element.toMethod(name, returnType)[
> for(p : element.advice.parameters.parameters) {
> parameters += p.toParameter(p.name, p.parameterType)
> }
> documentation = element.documentation
> final = element.^final
>
> val importManager = new ImportManager(true, getJvmType(aspectFQN))
> val mainMethod = compile(element.advice.body, importManager)
> body = ['''
> «mainMethod»
> System.out.println("Some generated custom statement besides the
> automatically generated statements");
> ''']
> )]
> ...
> def compile(XExpression xExpression, ImportManager importManager) {
> val result = new StringBuilderBasedAppendable(importManager)
> compiler.toJavaStatement(xExpression, result, true)
> result
> }
>
>
> However now I also would like to customize the import statements. I know
> how to do that according to your post however I would like to avoid
> throwing away my JvmModelInferrer xtend class.


--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
ICQ# lbetto, 16080134 (GNU/Linux User # 158233)
HOME: http://www.lorenzobettini.it MUSIC: http://www.purplesucker.com
http://www.myspace.com/supertrouperabba
BLOGS: http://tronprog.blogspot.com http://longlivemusic.blogspot.com
http://www.gnu.org/software/src-highlite
http://www.gnu.org/software/gengetopt
http://www.gnu.org/software/gengen http://doublecpp.sourceforge.net


Re: Combining the IJvmModelInferrer with custom code generation [message #759105 is a reply to message #758940] Sat, 26 November 2011 11:56 Go to previous message
Goran   is currently offline Goran Friend
Messages: 45
Registered: November 2011
Member
Thanks Lorenzo,

I was able to use both my JvmModelInferrer and custom generator.
I extended the org.eclipse.xtext.xbase.compiler.JvmModelGenerator with a custom implementation. Then I override relevant methods.

public class CustomJvmModelGenerator extends
		org.eclipse.xtext.xbase.compiler.JvmModelGenerator {

	@Inject
	private TypeReferences references;

//override methods
....
}


And for the runtime module:

 @SuppressWarnings("all")
public abstract class AbstractAspectJRuntimeModule extends DefaultRuntimeModule {
....
	public Class<? extends org.eclipse.xtext.generator.IGenerator> bindIGenerator() {
//		return org.eclipse.xtext.xbase.compiler.JvmModelGenerator.class;
		return CustomJvmModelGenerator.class;
	}
}
Previous Topic:Validator Check Unique Name
Next Topic:[Jvm Model Inferrer]Creating a field from a CrossReference
Goto Forum:
  


Current Time: Sun Apr 05 20:53:22 GMT 2020

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

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

Back to the top