1

My Issue

I am migrating from Tapestry IOC to Guice and I would like to for Guice to forward all unresolved injection requests to Tapestry IOC. This works in the individual case for example:

 bind(DSLContext::class.java).toProvider (Provider {
        tapestryRegistry.getService(DSLContext::class.java)
  })

With this, when Guice is asked for a DSLContext it will take the instance constructed by tapestry from the tapestry registry.

My Question

I would like to do this generically, essentially something like Provider.get() except it should be Provider.get(Class). How can I accomplish this with Guice?

Wulf
  • 393
  • 2
  • 10

2 Answers2

1

I've never done it, so this is probably a bad answer, but you may be able to use https://github.com/google/guice/wiki/CustomInjections where you:

Implement TypeListener to test the injector for the field type having a guice binding instead of looking at their annotation like the example. (example checks for @injectlogger on the field) you'd need to check the injector for a binding

Implement the members injector to set the field value from tapestry instead of creating the object yourself. (example creates and sets a new logger instance)

When you bind the listener you'd have to bind to an instance while providing the injector and tapestry instance.

kendavidson
  • 1,430
  • 1
  • 11
  • 18
  • Thanks for your answer. I had a look and it's almost what I needed but it's not workable because I can't tell from annotations whether Tapestry IOC or Guice should do the injection. – Wulf Jun 21 '19 at 10:08
  • Right on. Glad you sorted it out. – kendavidson Jun 21 '19 at 10:30
0

So I figured out that I can use Tapestry's "Service Activity Scoreboard" Service to figure out which Services are bound to Tapestry IOC.

tapestryRegistry.getService(ServiceActivityScoreboard::class.java)
.serviceActivity.forEach { serviceActivity ->

        bindOnce(serviceActivity.serviceInterface)?.toProvider( Provider {
            tapestryRegistry.getService(serviceActivity.serviceInterface)
        })

}

bindOnce here is simply a module local method that makes sure interfaces are not bound twice.

Wulf
  • 393
  • 2
  • 10