What steps will reproduce the problem?
1. Create a private module that exposes a binding for A.
2. Create a second private module that binds a class B as an eager singleton.
3. Make B depend on A.
This does not reproduce reliably, probably due to the injector not being
constructed the same way every time Gin runs (haven't verified this for sure).
The attached code does seem to be reliable, by making each of the two private
modules depend on the other module (so whichever one is created first, there's
still a problem).
What is the expected output? What do you see instead?
B should be created at client startup, with an instance of A. Instead, the
client crashes with a NullPointerException.
We believe that this is because eager singletons are initialized during the
injector's construction, and sub-injectors seem to be created wherever their
fields are initialized. So if a sub-injector defines eager singletons, those
singletons are created when the parent injector's fields are in an undefined
state, and if the eager singletons try to call into the parent ... *boom*!
Proposed solution: separate eager singleton construction from construction of
the injector. Singletons should be created in an initialization method
(.createEagerSingletons() or perhaps .initialize()) which is propagated through
the injector hierarchy. It might be reasonable to have the top-level
injector's constructor invoke that method after all its other work (if any) is
complete.
Original issue reported on code.google.com by dburr...@google.com on 25 May 2011 at 5:51
Original issue reported on code.google.com by
dburr...@google.com
on 25 May 2011 at 5:51Attachments: