Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Constructor Inference (JvmConstructor)(Creating a Java Constructor via Inferrence, JvmConstructor)
Constructor Inference (JvmConstructor) [message #772699] Fri, 30 December 2011 11:31 Go to next message
Eugene Wagner is currently offline Eugene WagnerFriend
Messages: 2
Registered: December 2011
Junior Member
Hi,

I'm trying to add a constructor to a Java class generated by model inference. Unfortunately the constructor fails to appear in the generated Java source code.

I've tried two variations without success:
1) using TypesFactory#createJvmConstructor
2) using JvmTpesBuilder#toConstructor

I'm not sure if I've come across a bug or just using the API incorrectly. Any help would be much appreciated.

While debugging the calls appear to work successfully (i.e. I could see that an instance of "JvmConstructorImplCustom" was sitting in the members attribute of the "JvmGenericTypeImplCustom" instance. During debugging the Constructor appears as follows:

org.eclipse.xtext.common.types.impl.JvmConstructorImplCustom@67f236db (visibility: DEFAULT, simpleName: win_p1_with_T1, identifier: null) (varArgs: false)


And the JvmGenericTypeImplCustom appears as follows:

org.eclipse.xtext.common.types.impl.JvmGenericTypeImplCustom@2b9758e4 (visibility: PUBLIC, simpleName: win_p1_with_T1, identifier: null) (abstract: false, static: false, final: false, packageName: p1) (interface: false)

Below are the relevant parts of the XTend inference code:

class GuiJvmModelInferrer extends AbstractModelInferrer {

@Inject extension JvmTypesBuilder
@Inject extension TypeReferences
@Inject extension IQualifiedNameProvider
@Inject extension TypesFactory


def dispatch void infer(Window w, IAcceptor<JvmDeclaredType> acceptor, boolean isPrelinkingPhase){
val newClass = w.toClass(w.fullyQualifiedName)
//create the initializer strategy that will be invoked to set the details of the new class
[
it.superTypes += mkTypeRef(typeof(javax.swing.JFrame), w)
/* following line does not work */
it.members += mkConstructor(w)
it.members += w.toField("name", mkTypeRef(typeof(String), w), null)
it.members += w.toSetter("name", mkTypeRef(typeof(String),w))
]
acceptor.accept(newClass)
}

/* both variations of 'mkConsturctor' fail to produce a constructor in the *.java file */
def JvmConstructor mkConstructor2(Window w){
val op = createJvmConstructor()
op.simpleName = w.name
op.setBody["{ /*TODO */ }"]
associate(w, op)
return op
}

def JvmConstructor mkConstructor(Window w){
return toConstructor(w, w.name, null)
}



def dispatch void infer(PackageDecl p, IAcceptor<JvmDeclaredType> acceptor, boolean isPrelinkingPhase){

for(t : p.elements.filter([it instanceof Window])){
t.infer(acceptor, isPrelinkingPhase)
}
}



def JvmTypeReference mkTypeRef(Class<?> type, EObject context){
val typeDecl = findDeclaredType(type, context)
if(typeDecl==null)
throw new RuntimeException("could not find declaration for the type: " + type.name)
return createTypeRef(typeDecl)
}


def dispatch void infer(Model m, IAcceptor<JvmDeclaredType> acceptor, boolean isPrelinkingPhase) {
for(e : m.elements){
e.infer(acceptor, isPrelinkingPhase)
}
}

}

[Updated on: Fri, 30 December 2011 11:32]

Report message to a moderator

Re: Constructor Inference (JvmConstructor) [message #772707 is a reply to message #772699] Fri, 30 December 2011 11:44 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
Hi,

this is what has worked for me back in times where there was not builder api for the inferrer
http://www.eclipse.org/forums/index.php/mv/msg/238889/723364/#msg_723364

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Constructor Inference (JvmConstructor) [message #772710 is a reply to message #772707] Fri, 30 December 2011 11:54 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
P.S: default constructors with an empty body are optimized to /dev/null by the generator

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Constructor Inference (JvmConstructor) [message #772714 is a reply to message #772710] Fri, 30 December 2011 12:06 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
could you please file a ticket against the JvmTypesBuilder/JvmModelGenerator if it not already exists
it guess this should work somehow out of the box

but the JvmTypesBuilder creates a situation
that is not expected by the JvmModelGenerator


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Constructor Inference (JvmConstructor) [message #772719 is a reply to message #772714] Fri, 30 December 2011 12:15 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
hi,

i found this one: https://bugs.eclipse.org/bugs/show_bug.cgi?id=367487

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Constructor Inference (JvmConstructor) [message #772730 is a reply to message #772719] Fri, 30 December 2011 13:02 Go to previous message
Eugene Wagner is currently offline Eugene WagnerFriend
Messages: 2
Registered: December 2011
Junior Member
Hi Christain

Wow, many thanks for the super-helpful info!

The bug report that you found seems to cover my situation and has narrowed the symptomes, so I won't file a new bug report unless you say otherwise.

Thanks again
Eugene
Previous Topic:Document Partioning
Next Topic:arbitrary returns
Goto Forum:
  


Current Time: Tue Sep 24 13:23:12 GMT 2024

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

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

Back to the top