google / google/guice

Bug in Assisted Factory when constructing an object

Open
#1,044 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
12.7k
Forks
1.7k
Avg merge
11m
Merged PRs (30d)
2

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!

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.