-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
(Using Guice 4.1.0)
Here's a test project : https://github.com/electrotype/guice-assisted-factory-bug
Please run /src/test/java/TestClass.java with Junit.
The pseudo-code triggering the problem is something like this :
- I use the method "create()" of an assisted factory to create object B, which is of class "Widget".
- There is an @Inject annotated method (called "init()") on class Widget.
- In this "init()" method, B uses the same "create()" method from the same assisted factory to create a Object C.
And this is where the problem occures : the created "C" is not valid! Note that C is of the same class than B (Widget), but C doesn't create any new object in its own "init()" method.
I've tried to debug Guice's code a little bit, and found that this problem is caused in com.google.inject.assistedinject.FactoryProvider2#invoke(...) when data.cachedBinding is not null. I also saw that data.cachedBinding is not null when the "optimized" property is true. I searched to see how I could disable this optimization and saw that injecting the Guice Injector itself in my objects was a way to do it. I tried it and it did work!! That produced a valid "C" object...
Of course I do not want to stick with such very slow workaround.
Thanks!