Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » problems with JvmTypesBuilder, JvmModelGenerator and "this"
problems with JvmTypesBuilder, JvmModelGenerator and "this" [message #778426] Thu, 12 January 2012 13:37 Go to next message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1812
Registered: July 2009
Location: Firenze, Italy
Senior Member
Hi

in a DSL I'm developing I have these rules which refer to
XbaseExpression (I'll try to simplify things):

Manager:
'manager' name=ValidID '{'
view = ViewField
bindings += Binding*
'}'
;

ViewField:
'view' type=JvmTypeReference name=ValidID
;

Binding:
'bind' from=XExpression
;

I generate Java code using a JvmTypesBuilder to create the JVM
corresponding class and JvmModelGenerator to actually generate Java files.

I then have this method to create the JvmGenericType corresponding to
Manager:

def inferrClassForManager(Manager e) {
e.toClass( e.fullyQualifiedName ) [
documentation = e.documentation

val f = e.view
members += f.toField(f.name, f.type)
members += f.toGetter(f.name, f.type)
members += f.toSetter(f.name, f.type)

// some code skipped, t is the return type
members += e.toMethod("myMethod", t) [

body = [
val a = new StringBuilderBasedAppendable(it)

e.bindings.forEach([
xbaseCompiler.toJavaStatement(it.from,a, true)
])
'''
«a»

return "foo";
'''
]
]

inferredClass
]
}

where the xbaseCompiler is Injected

suppose I have this program

manager MyManager {
view ViewPart1 viewPart
bind this
bind viewPart.myField
}

this is accepted by the compiler, and "this" is correctly bound to
MyManager (I also have a JvmModelInferrer which infers a simpler JVM
class with only fields and getter and setters); however, when the Java
code is generated I get the following

// imports skipped
public class MyManager {
private ViewPart1 viewPart;

public ViewPart1 getViewPart() {
return this.viewPart;
}

public void setViewPart(final ViewPart1 viewPart) {
this.viewPart = viewPart;
}

public t myMethod() {
MyManager _MyManager = MyManager; <-- corresponds to "bind this"
MyManager _MyManager_1 = MyManager;
ViewPart1 _viewPart = _MyManager_1.getViewPart();
Text _myField = _viewPart.getMyField();
^-- corresponds to "bind viewPart.myField"

return "foo";
}
}

so it looks like during the generation "this" is not correctly bound:
instead, the name of type of "this" is generated...

am I missing something?

thanks in advance
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: problems with JvmTypesBuilder, JvmModelGenerator and &quot;this&quot; [message #778541 is a reply to message #778426] Fri, 13 January 2012 07:55 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi Lorenzo,

something along these lines seems to be missing in your implementation:

// initialize the appendable
appendable.declareVariable(method.getDeclaringType(), "this");
if (method.getDeclaringType() instanceof JvmGenericType) {
JvmTypeReference superClass =
((JvmGenericType)method.getDeclaringType()).getExtendedClass();
if (superClass != null) {
appendable.declareVariable(superClass.getType(), "super");
}
}

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

Am 12.01.12 14:37, schrieb Lorenzo Bettini:
> Hi
>
> in a DSL I'm developing I have these rules which refer to
> XbaseExpression (I'll try to simplify things):
>
> Manager:
> 'manager' name=ValidID '{'
> view = ViewField
> bindings += Binding*
> '}'
> ;
>
> ViewField:
> 'view' type=JvmTypeReference name=ValidID
> ;
>
> Binding:
> 'bind' from=XExpression
> ;
>
> I generate Java code using a JvmTypesBuilder to create the JVM
> corresponding class and JvmModelGenerator to actually generate Java files.
>
> I then have this method to create the JvmGenericType corresponding to
> Manager:
>
> def inferrClassForManager(Manager e) {
> e.toClass( e.fullyQualifiedName ) [
> documentation = e.documentation
>
> val f = e.view
> members += f.toField(f.name, f.type)
> members += f.toGetter(f.name, f.type)
> members += f.toSetter(f.name, f.type)
>
> // some code skipped, t is the return type
> members += e.toMethod("myMethod", t) [
>
> body = [
> val a = new StringBuilderBasedAppendable(it)
>
> e.bindings.forEach([
> xbaseCompiler.toJavaStatement(it.from,a, true)
> ])
> '''
> «a»
>
> return "foo";
> '''
> ]
> ]
>
> inferredClass
> ]
> }
>
> where the xbaseCompiler is Injected
>
> suppose I have this program
>
> manager MyManager {
> view ViewPart1 viewPart
> bind this
> bind viewPart.myField
> }
>
> this is accepted by the compiler, and "this" is correctly bound to
> MyManager (I also have a JvmModelInferrer which infers a simpler JVM
> class with only fields and getter and setters); however, when the Java
> code is generated I get the following
>
> // imports skipped
> public class MyManager {
> private ViewPart1 viewPart;
>
> public ViewPart1 getViewPart() {
> return this.viewPart;
> }
>
> public void setViewPart(final ViewPart1 viewPart) {
> this.viewPart = viewPart;
> }
>
> public t myMethod() {
> MyManager _MyManager = MyManager; <-- corresponds to "bind this"
> MyManager _MyManager_1 = MyManager;
> ViewPart1 _viewPart = _MyManager_1.getViewPart();
> Text _myField = _viewPart.getMyField();
> ^-- corresponds to "bind viewPart.myField"
>
> return "foo";
> }
> }
>
> so it looks like during the generation "this" is not correctly bound:
> instead, the name of type of "this" is generated...
>
> am I missing something?
>
> thanks in advance
> Lorenzo
>
Re: problems with JvmTypesBuilder, JvmModelGenerator and &quot;this&quot; [message #779724 is a reply to message #778541] Mon, 16 January 2012 12:04 Go to previous message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1812
Registered: July 2009
Location: Firenze, Italy
Senior Member
On 01/13/2012 08:55 AM, Sebastian Zarnekow wrote:
> Hi Lorenzo,
>
> something along these lines seems to be missing in your implementation:
>
> // initialize the appendable
> appendable.declareVariable(method.getDeclaringType(), "this");
> if (method.getDeclaringType() instanceof JvmGenericType) {
> JvmTypeReference superClass =
> ((JvmGenericType)method.getDeclaringType()).getExtendedClass();
> if (superClass != null) {
> appendable.declareVariable(superClass.getType(), "super");
> }
> }
>

Hi Sebastian

I don't understand your answer completely: what is method referring to?

Let me report my original post, with some additional explanations and
some additional attempts which all fail:

in a DSL I'm developing I have these rules which refer to
XbaseExpression (I'll try to simplify things):

Manager:
'manager' name=ValidID '{'
view = ViewField
bindings += Binding*
'}'
;

ViewField:
'view' type=JvmTypeReference name=ValidID
;

Binding:
'bind' from=XExpression
;

I generate Java code using a JvmTypesBuilder to create the JVM
corresponding class and JvmModelGenerator to actually generate Java files.

I then have this method to create the JvmGenericType corresponding to
Manager; I now tried to add the declaration for this explicitly;
Sebastian, if your "method" refers to the created method with toMethod,
its containingType is null...

I then tried to create the appendable with
JvmModelGenerator.createAppendable passing the context EObject... which
context should I pass? I tried to pass the Manager itself

@Inject extension JvmTypesBuilder

@Inject extension JvmModelGenerator

@Inject XbaseCompiler xbaseCompiler

def inferrClassForManager(Manager e) {
e.toClass( e.fullyQualifiedName ) [
documentation = e.documentation

val f = e.view
members += f.toField(f.name, f.type)
members += f.toGetter(f.name, f.type)
members += f.toSetter(f.name, f.type)

// some code skipped, t is the return type
members += e.toMethod("myMethod", t) [

body = [
// now create the appendable using JvmModelGenerator
val a = e.createAppendable(it)

e.bindings.forEach([
xbaseCompiler.toJavaStatement(it.from,a, true)
])
'''
«a»

return "foo";
'''
]
]

inferredClass
]
}

suppose I have this program

manager MyManager {
view ViewPart1 viewPart
bind this
bind viewPart.myField
}

this is accepted by the compiler, and "this" is correctly bound to
MyManager (I also have a JvmModelInferrer which infers a simpler JVM
class with only fields and getter and setters); however, when the Java
code is generated I get the following

// imports skipped
public class MyManager {
private ViewPart1 viewPart;

public ViewPart1 getViewPart() {
return this.viewPart;
}

public void setViewPart(final ViewPart1 viewPart) {
this.viewPart = viewPart;
}

public t myMethod() {
MyManager _MyManager = MyManager; <-- corresponds to "bind this"
MyManager _MyManager_1 = MyManager;
ViewPart1 _viewPart = _MyManager_1.getViewPart();
Text _myField = _viewPart.getMyField();
^-- corresponds to "bind viewPart.myField"

return "foo";
}
}

so again, this is not correctly bound...

The actual generation takes place as this

def generateClassForManager(Manager manager) {
manager.classForManager.generateType
}

override doGenerate(Resource resource, IFileSystemAccess fsa) {
for(e: resource.allContents.filter(typeof(Manager)).toList) {
internalDoGenerate(e.classForManager, fsa)
}
}

internalDoGenerate comes from JvmModelGenerator...

so basically I first create a model with JvmTypesBuilder and then
generate it with JvmModelGenerator, but "this" is still correctly not
bound during the generation...

from what I understand when I run JvmModelGenerator.internalDoGenerate
this will create a new IAppendable which will surely not be the same of
the one I create when I build the model with JvmTypesBuilder?

Ideally, when I create the IAppendable, what should I bind "this" to?
To an EObject of my AST or to an EObject of the JVM model I'm creating
with JvmTypesBuilder?

thanks in advance
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


Previous Topic:Embedding a language in Java comments/annotations
Next Topic:[Xtext 2.2] How to support multi-line qualified names?
Goto Forum:
  


Current Time: Sat Apr 27 05:13:15 GMT 2024

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

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

Back to the top