Binding generated classes in gin

302 views Asked by At

I am trying to dynamically generate some set of event handlers (using com.google.gwt.core.ext.Generator subclass). The whole generation process works fine, my generated handlers accepts EventBus via setter injection (@Inject annotation) and the whole idea is that these automagically generated handlers register themselves in the (injected) EventBus under the hood. The problem is that all these handlers must be somehow instantiated. GIN does not know anything about them until they will be bound in GIN module; something like below:

bind(MyDynamicallyGeneratedHandler.class).asEagerSingleton();

The problem with this code is that I have to generate gin module also (which will contain all bind(...) instructions). It works well but I cannot install generated module (@Inject it) into another not generated module so that its bind instructions are executed (as a result of install(MyDynamicModule.class)). I tried dirty tricks with static injection and providers - all without success.

Is there any way to configure dynamically generated GIN module? Or maybe is there any way to instantiate dynamically generated handlers so that setter injection (and thus event bus registration) happens?

Here is sample code snippet:

// it is standard interface
public interace DynamicHandler {
  @Inject
  void setEventBus(EventBus eventBus);
}

// one of dynamically generated handlers
// where (how) to instantiate me?
public class MyHandler implements DynamicHandler {

  @Inject
  AnotherHandlerSpecificComponent component    

  void setEventBus(EventBus eventBus) {
    eventBus.register(...); // register myself
  }
}

Important thing to note is that all these dynamically generated handlers have some other dependencies @Injected (apart from EventBus). And these dependencies (their types) ale handler-specific (AnotherHandlerSpecificComponent in the example above). So what I need to do is to instantiate these handlers in a way that triggers GIN injection (I cannot, for example, inject dependencies manually).

Thanks in advance for any clues. I am totally stucked...

1

There are 1 answers

0
Andreas Frische On

How about a Factory where you inject the dependencies for the handlers and try to instantiate and register the actual handlers (e.g. by reflection or scanning the file system?)