Skip to content

Bug in Assisted Factory when constructing an object #1044

@electrotype

Description

@electrotype

(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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions