Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Get CharSequence from JvmOperation object(A method is there in form of JvmOperation. How to get the java equivalent of the jvm operation)
Get CharSequence from JvmOperation object [message #1071067] Fri, 19 July 2013 05:25 Go to next message
Priya Sabut is currently offline Priya Sabut
Messages: 41
Registered: September 2012
Member
Hi All,
I am new in Xtext and struggling with it.

I have a dsl that take function where the function body is a XBlockExpression.

Function:
{Function}
'function' name=QualifiedName '(' (params+=FullJvmFormalParameter (',' params+=FullJvmFormalParameter)*)? ')'
':' type = JvmTypeReference
body=XBlockExpression
;

I am using Generator to generate java code instead of using JvmModelInferrer as my dsl does not have a direct mapping with java. I have to do lot of textual templating.

In my generator I have the function and the body as XExpression. I am not able to fetch the java equivalent of it using xbaseCompiler. If anyone can guide me how can I get the equivalent java statements using xbasecompiler, will be really helpful.

Meanwhile I am trying it the Inferrer way. Inside my Generator I injected JvmTypesBuilder and using its extension methods in following way

@Inject protected extension JvmTypesBuilder;
...
def compileFunction(Function f)'''
«var JvmOperation function = f.toMethod(f.name, f.type)[
documentation = f.documentation
for(p : f.params){
parameters += p.toParameter(p.name, p.parameterType)
}
body = f.body

«JvmOperation »
'''
Here I am getting my the whole function as a JvmOperation: getAbc(java.lang.String) ...
Is there a way to get the java equivalent statement from JvmOperation?

Thanks in advance.
Priya
Re: Get CharSequence from JvmOperation object [message #1071093 is a reply to message #1071067] Fri, 19 July 2013 06:34 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6130
Registered: July 2009
Senior Member
Hi,

i am not sure if i get your problem but you may digg into org.eclipse.xtext.xbase.compiler.JvmModelGenerator
Re: Get CharSequence from JvmOperation object [message #1071099 is a reply to message #1071093] Fri, 19 July 2013 06:50 Go to previous messageGo to next message
Priya Sabut is currently offline Priya Sabut
Messages: 41
Registered: September 2012
Member
Thanks for your reply ...
I am struggling here... This is becoming a dead-end for me...

Could you please let me know what is that information you need to understand the problem ? I mean where am I not clear in describing the problem ?

I would like to provide all the information ...

In simple I am trying to get java equivalent statements from XEpression which is my function body using MyGenerator that implements IGenerator.

DSL :
-----
function getXXX(String abc, String anc): String{
if(anc.endsWith("A")){
return "my"+anc.toLowerCase;
}
else {
return anc.toUpperCase;
}
}

Generated Code what I am expecting :
------------------------------------
public String getXXX(final String abc, final String anc) {
boolean _endsWith = anc.endsWith("A");
if (_endsWith) {
String _lowerCase = anc.toLowerCase();
return ("my" + _lowerCase);
} else {
return anc.toUpperCase();
}
}

I am getting the same using Inferrer but my requirement is to get it done through MyGenerator.

[Updated on: Fri, 19 July 2013 07:00]

Report message to a moderator

Re: Get CharSequence from JvmOperation object [message #1071110 is a reply to message #1071099] Fri, 19 July 2013 07:02 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6130
Registered: July 2009
Senior Member
Hi,

i still dont understand why you can not use JvmModelInferrer/JvmModelGenerator and your custom generator?
did you have a look at org.eclipse.xtext.xbase.compiler.JvmModelGenerator._generateMember(JvmOperation, ITreeAppendable, GeneratorConfig)
and try todo something similar or even call it?

~Christian
Re: Get CharSequence from JvmOperation object [message #1071225 is a reply to message #1071110] Fri, 19 July 2013 11:39 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6130
Registered: July 2009
Senior Member
HI,

the following works for me (if your would use jvmmodelinferrer completely it would even be more easy)

package org.xtext.example.mydsl.jvmmodel

import com.google.inject.Inject
import org.eclipse.xtext.xbase.jvmmodel.AbstractModelInferrer
import org.eclipse.xtext.xbase.jvmmodel.IJvmDeclaredTypeAcceptor
import org.eclipse.xtext.xbase.jvmmodel.JvmTypesBuilder
import org.xtext.example.mydsl.myDsl.Function
import org.xtext.example.mydsl.myDsl.Model

class MyDslJvmModelInferrer extends AbstractModelInferrer {

	@Inject extension JvmTypesBuilder

	def dispatch void infer(Model element, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
		acceptor.accept(
			element.toClass("DontCare")
		).initializeLater [
			for (f : element.functs) {
				val operation = f.toMethod(f.name, f.type ?: inferredType) [
					documentation = f.documentation
					for (p : f.params) {
						parameters += p.toParameter(p.name, p.parameterType)
					}
					body = f.body
				]
				members += operation
			}
		]
	}
}



package org.xtext.example.mydsl

import javax.inject.Inject
import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.common.types.JvmOperation
import org.eclipse.xtext.generator.IFileSystemAccess
import org.eclipse.xtext.generator.IGenerator
import org.eclipse.xtext.generator.trace.ITraceURIConverter
import org.eclipse.xtext.resource.ILocationInFileProvider
import org.eclipse.xtext.xbase.compiler.GeneratorConfig
import org.eclipse.xtext.xbase.compiler.ImportManager
import org.eclipse.xtext.xbase.compiler.JvmModelGenerator
import org.eclipse.xtext.xbase.compiler.output.ITreeAppendable
import org.eclipse.xtext.xbase.compiler.output.TreeAppendable
import org.eclipse.xtext.xbase.jvmmodel.IJvmModelAssociations
import org.xtext.example.mydsl.myDsl.Function
import org.xtext.example.mydsl.myDsl.Model

class DummyGen implements IGenerator {

	@Inject JvmModelGenerator jmg

	@Inject extension IJvmModelAssociations jma

	@Inject
	ILocationInFileProvider locationProvider

	@Inject
	ITraceURIConverter converter

	override doGenerate(Resource input, IFileSystemAccess fsa) {
		for (o : input.contents) {
			o.compile(fsa)
		}
	}

	def dispatch void compile(EObject o, IFileSystemAccess fsa) {
	}

	def dispatch void compile(Model o, IFileSystemAccess fsa) {
		val im = new ImportManager()
		val app = new TreeAppendable(im, converter, locationProvider, jma, o, "", "\n")
		for (f : o.functs) {
			f.compileFunction(app)
		}
		fsa.generateFile("test/Test.java",
			'''
				package test;
				«im.imports.map["import " + it + ";"].join(("\n"))»
				public class Test {
					
					«app.toString»
					
				}
			''')
	}

	def compileFunction(Function f, ITreeAppendable app) {
		val cfg = new GeneratorConfig
		val operation = f.jvmElements.get(0) as JvmOperation
		jmg.generateMember(operation, app, cfg)
	}

}
Re: Get CharSequence from JvmOperation object [message #1075442 is a reply to message #1071225] Mon, 29 July 2013 08:43 Go to previous messageGo to next message
Priya Sabut is currently offline Priya Sabut
Messages: 41
Registered: September 2012
Member
Hi Christian,
Thank you very much for the reply ...
The Inferrer way is working for me.

but still I get exception when I use the Generator. I have used the same code as given by you and getting an ArrayIndexOutOfBoundsException: 0 at val operation = f.jvmElements.get(0) as JvmOperation as getJvmElements(sourceElements) of JvmModelAssociator is returning an empty map (sourceToTargetMap() is retuning empty map).

I have no clue how can I get this work!! Need your help to solve this.

The grammar used is
Model:
(functs += Function)+
;

Function:
{Function}
'function' name=QualifiedName '(' (params+=FullJvmFormalParameter (',' params+=FullJvmFormalParameter)*)? ')'
':' type = JvmTypeReference
body=XBlockExpression
;

Thanking you in anticipation...
Re: Get CharSequence from JvmOperation object [message #1075472 is a reply to message #1075442] Mon, 29 July 2013 09:39 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6130
Registered: July 2009
Senior Member
Seems you did not implement the inferrer
Re: Get CharSequence from JvmOperation object [message #1075535 is a reply to message #1071067] Mon, 29 July 2013 11:43 Go to previous messageGo to next message
Priya Sabut is currently offline Priya Sabut
Messages: 41
Registered: September 2012
Member
I did. I have generated the java class using Inferrer as you have mentioned in MyDslJvmModelInferrer.xtend and the java class generated has the correct java equivalent method.

But when I try to generate the java class as you shown in DummyGen.xtend, I am getting the mentioned error and the map returned by getJvmElements() is empty.

I want to generate the same java code using the generator not inferrer.

--Priya
Re: Get CharSequence from JvmOperation object [message #1075536 is a reply to message #1075535] Mon, 29 July 2013 11:44 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6130
Registered: July 2009
Senior Member
Hi,

can you please share your complete code.

~Christian
Re: Get CharSequence from JvmOperation object [message #1075542 is a reply to message #1075536] Mon, 29 July 2013 12:00 Go to previous messageGo to next message
Priya Sabut is currently offline Priya Sabut
Messages: 41
Registered: September 2012
Member
I have tried the following as per your suggestion.

MyDsl.xtext
-----------
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.xbase.Xbase

generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"

DomainModel:
(functions += Function)+
;

Function:
{Function}
'function' name=QualifiedName '(' (params+=FullJvmFormalParameter (',' params+=FullJvmFormalParameter)*)? ')'
':' type = JvmTypeReference
body=XBlockExpression
;

MyDslGenerator.xtend //Generator
----------------------
/*
* generated by Xtext
*/
package org.xtext.example.mydsl2.generator


import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.common.types.JvmOperation
import org.eclipse.xtext.generator.IFileSystemAccess
import org.eclipse.xtext.generator.IGenerator
import org.eclipse.xtext.generator.trace.ITraceURIConverter
import org.eclipse.xtext.resource.ILocationInFileProvider
import org.eclipse.xtext.xbase.compiler.GeneratorConfig
import org.eclipse.xtext.xbase.compiler.ImportManager
import org.eclipse.xtext.xbase.compiler.JvmModelGenerator
import org.eclipse.xtext.xbase.compiler.output.ITreeAppendable
import org.eclipse.xtext.xbase.compiler.output.TreeAppendable
import org.eclipse.xtext.xbase.jvmmodel.IJvmModelAssociations
import org.xtext.example.mydsl2.myDsl.Function
import org.xtext.example.mydsl2.myDsl.DomainModel
import org.eclipse.xtext.xbase.compiler.XbaseCompiler
import com.google.inject.Inject

/**
* Generates code from your model files on save.
*
* see http://www.eclipse.org/Xtext/documentation.html#TutorialCodeGeneration
*/
class MyDslGenerator implements IGenerator {
@Inject protected XbaseCompiler compiler

@Inject JvmModelGenerator jmg

@Inject extension IJvmModelAssociations jma

@Inject
ILocationInFileProvider locationProvider

@Inject
ITraceURIConverter converter

override doGenerate(Resource input, IFileSystemAccess fsa) {
for (o : input.contents) {
o.compile(fsa)
}
}

def dispatch void compile(EObject o, IFileSystemAccess fsa) {
}

def dispatch void compile(DomainModel o, IFileSystemAccess fsa) {
val im = new ImportManager()
val app = new TreeAppendable(im, converter, locationProvider, jma, o, "", "\n")
for (f : o.functions) {
val a =f.compileFunction(app)
println(a)
}
fsa.generateFile("test/Test.java",
'''
package test;
«im.imports.map["import " + it + ";"].join(("\n"))»
public class Test {

«app.toString»

}
''')
}

def compileFunction(Function f, ITreeAppendable app) {
val cfg = new GeneratorConfig
val operation = f.jvmElements.get(0) as JvmOperation; // Exception ArrayIndexOutOfBound as jvmElements returning an empty string
jmg.generateMember(operation, app, cfg)
}
}

MyDslRuntimeModule.java //Binding IGenerator
------------------------
/*
* generated by Xtext
*/
package org.xtext.example.mydsl2;

import org.eclipse.xtext.generator.IGenerator;
import org.xtext.example.mydsl2.generator.MyDslGenerator;

/**
* Use this class to register components to be used at runtime / without the Equinox extension registry.
*/
public class MyDslRuntimeModule extends org.xtext.example.mydsl2.AbstractMyDslRuntimeModule {

@Override
public Class<? extends IGenerator> bindIGenerator() {
// TODO Auto-generated method stub
return MyDslGenerator.class;
}
}
Re: Get CharSequence from JvmOperation object [message #1075564 is a reply to message #1075542] Mon, 29 July 2013 12:37 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6130
Registered: July 2009
Senior Member
Hi sorry your inferrer is still missing. you have to use the inferrer AND the custom generator.
Re: Get CharSequence from JvmOperation object [message #1075579 is a reply to message #1075564] Mon, 29 July 2013 13:16 Go to previous messageGo to next message
Priya Sabut is currently offline Priya Sabut
Messages: 41
Registered: September 2012
Member
oooo Shocked ... I got it . Thank you for your patience Smile

So here I have to use both! What is the idea behind using both ? (I can also generate my java code using any one of them) I am confused Confused Would you like to share some understanding ?
Any link with this regard will also be helpful.

Now my code is getting generated. Thanks to you!

One thing I noticed that the code generated is not in proper format!! But when I am generating the same code only using Inferrer, the code is in proper format!!
Re: Get CharSequence from JvmOperation object [message #1075586 is a reply to message #1075579] Mon, 29 July 2013 13:39 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6130
Registered: July 2009
Senior Member
Hi,

the JvmModelInferrer by default works together With JvmModelGenerator. they work hand in hand. (each does its job at a specific point in time)
if you come up with a own version of jvmModelgenerator and reuse its inferred model (ijvmmodelassoc.) you have to keep
of course the inferrernce to keep it working
Re: Get CharSequence from JvmOperation object [message #1075907 is a reply to message #1075586] Tue, 30 July 2013 05:59 Go to previous messageGo to next message
Priya Sabut is currently offline Priya Sabut
Messages: 41
Registered: September 2012
Member
okay. I will read more about it. Can you refer me to some document ?

Can I do something to get the generated functions java code in proper format ? In the generated code all method statements are starting at the same vertical line Sad

Re: Get CharSequence from JvmOperation object [message #1076098 is a reply to message #1075907] Tue, 30 July 2013 13:58 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6130
Registered: July 2009
Senior Member
Hi, sry i dont know of any docs on this.
Re: Get CharSequence from JvmOperation object [message #1076294 is a reply to message #1076098] Wed, 31 July 2013 01:52 Go to previous messageGo to next message
Priya Sabut is currently offline Priya Sabut
Messages: 41
Registered: September 2012
Member
Hi Christian,

How about generated java operations formatting ? In the generated code all the statements are at the same vertical line. Can this be solved ?

--Priya
Re: Get CharSequence from JvmOperation object [message #1076300 is a reply to message #1076294] Wed, 31 July 2013 02:07 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6130
Registered: July 2009
Senior Member
Hmmm sorry have no idea, guess you have to digg into the appendable.
Re: Get CharSequence from JvmOperation object [message #1076332 is a reply to message #1076300] Wed, 31 July 2013 03:27 Go to previous messageGo to next message
Priya Sabut is currently offline Priya Sabut
Messages: 41
Registered: September 2012
Member
Hhmm... Thank You for your help...


I will digg deep into appendable and will post my findings(if any) Rolling Eyes .
Re: Get CharSequence from JvmOperation object [message #1076443 is a reply to message #1076332] Wed, 31 July 2013 08:05 Go to previous message
Priya Sabut is currently offline Priya Sabut
Messages: 41
Registered: September 2012
Member
Hi Christian,

Finally I got the solution for formatting Smile

The formatting is done as per the value passed for Indentation argument while constructing TreeAppendable(... String indentation, String lineSeparator). Depending on the number of spaces put for indentation, the same will be put into the generated code. If you want the indentation to be 4-spaces, pass 4-sapces(" ") as value to the argument indentation, then indentation will be taken care of...
Previous Topic:Getting value of parser rules during runtime
Next Topic:Xtext: Include "enum"
Goto Forum:
  


Current Time: Thu Jul 24 23:42:33 EDT 2014

Powered by FUDForum. Page generated in 0.02790 seconds