Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » JVM Model Infer Append String to XBlockExpression
JVM Model Infer Append String to XBlockExpression [message #1833563] Sun, 18 October 2020 00:26 Go to next message
Ben Holland is currently offline Ben HollandFriend
Messages: 28
Registered: April 2019
Junior Member
Hi I am trying to do something similar to https://www.eclipse.org/forums/index.php/t/501520/, but I think the advice in the last post is now outdated.

I have an XBlockExpression that I would like to use for the body of a method. After the XBlockExpression I'd like to append a string of Java code.

Here is what I tried, but can't seem to get it to work. The original example expects a LightweightTypeReference that I don't know how to create and the other compile signature gets a null pointer. Any advice would be very much appreciated!

MyOperation op = ...
XBlockExpression block = op.body

@Inject XbaseCompiler xbaseCompiler
...
it.members += toMethod(op, op.name, typeRef(Void.TYPE))[
	it.visibility = JvmVisibility.PUBLIC;
	it.body = [
		xbaseCompiler.compile(block, it, typeRef(Void.TYPE), Set.of());
		append('''\n\n//TEST''')
	];
]


java.lang.NullPointerException
	at org.eclipse.xtext.xbase.compiler.LiteralsCompiler._toJavaExpression(LiteralsCompiler.java:135)
	at org.eclipse.xtext.xbase.compiler.LiteralsCompiler.internalToConvertedExpression(LiteralsCompiler.java:44)
	at org.eclipse.xtext.xbase.compiler.FeatureCallCompiler.internalToConvertedExpression(FeatureCallCompiler.java:108)
	at org.eclipse.xtext.xbase.compiler.XbaseCompiler.internalToConvertedExpression(XbaseCompiler.java:352)
	at org.eclipse.xtext.xbase.compiler.TypeConvertingCompiler.internalToConvertedExpression(TypeConvertingCompiler.java:107)
	at org.eclipse.xtext.xbase.compiler.XbaseCompiler._toJavaStatement(XbaseCompiler.java:754)
	at org.eclipse.xtext.xbase.compiler.XbaseCompiler.doInternalToJavaStatement(XbaseCompiler.java:385)
	at org.eclipse.xtext.xbase.compiler.AbstractXbaseCompiler.internalToJavaStatement(AbstractXbaseCompiler.java:479)
	at org.eclipse.xtext.xbase.compiler.XbaseCompiler._toJavaStatement(XbaseCompiler.java:418)
	at org.eclipse.xtext.xbase.compiler.XbaseCompiler.doInternalToJavaStatement(XbaseCompiler.java:359)
	at org.eclipse.xtext.xbase.compiler.AbstractXbaseCompiler.internalToJavaStatement(AbstractXbaseCompiler.java:479)
	at org.eclipse.xtext.xbase.compiler.AbstractXbaseCompiler.compile(AbstractXbaseCompiler.java:300)
	at org.eclipse.xtext.xbase.compiler.AbstractXbaseCompiler.compile(AbstractXbaseCompiler.java:280)
Re: JVM Model Infer Append String to XBlockExpression [message #1833564 is a reply to message #1833563] Sun, 18 October 2020 07:03 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13976
Registered: July 2009
Senior Member
This won't work. You have to infer two methods
One with body = theblock
One with body = '''calll other method here and so your stuff '''


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: JVM Model Infer Append String to XBlockExpression [message #1833569 is a reply to message #1833564] Sun, 18 October 2020 15:35 Go to previous messageGo to next message
Ben Holland is currently offline Ben HollandFriend
Messages: 28
Registered: April 2019
Junior Member
I'm not sure I can split this into two methods, my block has some variables declared that would be in scope that I would like to have accessible to the code that I wanted to append. I want the two code segments to live in the same local scope.
Re: JVM Model Infer Append String to XBlockExpression [message #1833573 is a reply to message #1833569] Mon, 19 October 2020 05:49 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13976
Registered: July 2009
Senior Member
i dont understand.
the part you want to add at the end is hardcoded?!?


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: JVM Model Infer Append String to XBlockExpression [message #1833992 is a reply to message #1833573] Thu, 29 October 2020 14:49 Go to previous messageGo to next message
Ben Holland is currently offline Ben HollandFriend
Messages: 28
Registered: April 2019
Junior Member
Sorry I didn't see this reply. I ended up restructuring my codegen design to not need this, but the part at the end was a string I planned to generate (an implicit initialization step I wanted to live in the same scope as the variables declared in the XBlockExpression). I have two follow up questions.

1) Perhaps a better question would be is it possible to concatenate two XBlockExpressions into a single body? I imagine I could try to create a new XBlockExpression and populate it with the contents of the other two XBlockExpressions.

2) I see the append(string) function is discouraged. What is the implication of using that function in place of an XExpression? Say for instance I wanted to create and initialize a field. Is there a good way to generate the XExpression to initialize the field and is it worth it to do that?

it.members += toField(myField, "myField", typeRef("com.foo.MyField"))[
it.visibility = JvmVisibility.PRIVATE;
it. final = true;
]

// CAN I DO THIS BY GENERATING AN XEXPRESSION THAT DID NOT COME FROM THE PARSER?
it.members += toConstructor(myObj)[
it.body = [append("this.myField = new com.foo.MyField();")];
]

[Updated on: Thu, 29 October 2020 14:50]

Report message to a moderator

Re: JVM Model Infer Append String to XBlockExpression [message #1833996 is a reply to message #1833992] Thu, 29 October 2020 16:27 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13976
Registered: July 2009
Senior Member
you would do

it.members += toConstructor(myObj)[
it.body ='''this.myField = new com.foo.MyField();'''
]

or better
it.members += toConstructor(myObj)[
it.body ='''this.myField = new<<< typeRef("com.foo.MyField")>>>();''' //TODO use proper french quotes here
]



Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Parallel Serialization
Next Topic:Can I bundle grammar and genmodel into a jar?
Goto Forum:
  


Current Time: Tue Jun 15 00:39:29 GMT 2021

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

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

Back to the top