michaelwiles / google-gin

Automatically exported from code.google.com/p/google-gin
Apache License 2.0
0 stars 0 forks source link

NullPointerException during injector construction related to private modules and eager singletons #156

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
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

Attachments:

GoogleCodeExporter commented 9 years ago
Code review started here:

http://codereview.appspot.com/4561042/

Original comment by dburr...@google.com on 26 May 2011 at 1:27

GoogleCodeExporter commented 9 years ago
Fixed in r214.

Original comment by aragos on 2 Jun 2011 at 7:18