Bug in Assisted Factory when contructing an object

35 views
Skip to first unread message

Electro Type

unread,
Sep 13, 2016, 3:30:23 PM9/13/16
to google-guice
I currently do not have a test class to provide, I first want to know if this is a problem with Assisted Factories you are aware of... If it's not, I'll try to create a test class for you to check! I use Guice 4.1.0.

The pseudo-code triggering the problem is something like this :

- Object A uses 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.

Are you aware of this problem?

Thanks!

Julien


Electro Type

unread,
Sep 13, 2016, 6:43:09 PM9/13/16
to google-guice
Here's a test project : https://github.com/electrotype/guice-assisted-factory-bug

Please run /src/test/java/TestClass.java with Junit.


Tavian Barnes

unread,
Sep 14, 2016, 10:06:28 AM9/14/16
to google-guice
Seems like a bug.  You should report it here: https://github.com/google/guice/issues

Electro Type

unread,
Sep 14, 2016, 10:20:27 AM9/14/16
to google-guice
Reply all
Reply to author
Forward
0 new messages