Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Inject extension into newly generated object(How do I inject an extension into an object generated by a factory with the default guice injector?)
Inject extension into newly generated object [message #1832117] Wed, 09 September 2020 11:34 Go to next message
Jan Hermes is currently offline Jan HermesFriend
Messages: 25
Registered: September 2020
Junior Member
I want to augment my DSL framework to proved a factory for specific types that I want to be created.
To do this I configued a FactoryModuleBuilder in the runtime module like so:

class MyDslRuntimeModule extends AbstractMyDslRuntimeModule {

  def configureMyDslFactory(Binder binder) {
    binder.install(new FactoryModuleBuilder()
      .build(IMyClassFactory)
    )
  }
}


The class that gets built in this example is declared like so, which is also using
the IQualifiedNameProvider as an extension that should be injected.

class MyClass {
  
  @Inject extension IQualifiedNameProvider

  String fqn

  @Inject
  new(@Assisted MyDslSymbol symbol) {
    
    // at this point there is a problem later, because 
    // IQualifiedNameProvider does not get injected (_iQualifiedNameProvider remains "null"),
    // so the method "fullyQualifiedName" generates a NullPointerException

    this.fqn = symbol.fullyQualifiedName.toString(".")
  }
}


The factory-interface to be used for the module-installation is defined like this:

interface IMyClassFactory {

  def MyClass create(MyDslSymbol symbol)
}


In the end I would like use the factory e.g. in a validation method like so:

class MyDslValidator extends AbstractMyDslValidator {

  @Inject IMyClassFactory factory

  @Check
  def void checkSymbol(MyDslSymbol symbol) {
    val myClass = factory.create(symbol)

  }
}


Now I'm encountering a huge problem upon creating objects with the factory:

When the MyClass object gets created by the factory,
the guice-injector does not inject the extensions as specified, but they remain null.

How can I make the default injector be used for the classes created by the factory configured by configureMyDslFactory?
Re: Inject extension into newly generated object [message #1832181 is a reply to message #1832117] Fri, 11 September 2020 05:53 Go to previous messageGo to next message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 739
Registered: July 2009
Location: Dortmund, Germany
Senior Member

The factory needs to create MyClass with the injector. Inject an Injector into your factory and create MyClass with it, or inject members into the created instance.
Re: Inject extension into newly generated object [message #1832245 is a reply to message #1832181] Mon, 14 September 2020 08:06 Go to previous message
Jan Hermes is currently offline Jan HermesFriend
Messages: 25
Registered: September 2020
Junior Member
Thank you! I somehow thought, as FactoryModuleBuilder is described to use a child-injector for the creation of the objects of the factory, this child-injector might be using the same injection-bindings as the ones configured in the runtime-module.
Previous Topic:xtext-maven-plugin and DSL-Files located in src/main/resources
Next Topic:Errors in Xtext 15 min extended tutorial
Goto Forum:
  


Current Time: Mon Oct 19 16:43:39 GMT 2020

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

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

Back to the top