frjaeger220 / google-guice

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

NPE when lazily using a scoped provider #136

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
For detailed info, see: http://groups.google.com/group/google-
guice/browse_thread/thread/44f422a50f5b7991?hl=en .

Basically, a meta-scope would be the ability to tell any scope that you 
want people requesting an interface X to receive a lazy version of X.  
It'd take the feature that Guice already provides (allowing Provider<X> to 
be handed when X is bound) and extend it so that a new implementation of X 
internally delegates to something like a Provider<X> whenever a method is 
called.  This lets developers receive an X directly (avoiding the 
propogation of Provider<X> throughout the code) but still get the benefits 
of lazy construction.

Example syntax might be:
 @Singleton class X; and
 @Injected public User(@Lazy X x); or
 bind(X.class).to(XImpl.class).be(Lazy.class); or
 bind(X.class).to(XImpl.class).as(Scopes.RequestScope).be(Lazy.class);

Original issue reported on code.google.com by sberlin on 2 Aug 2007 at 2:29

GoogleCodeExporter commented 9 years ago
I think we should start by making scopes Guice "managed" (injectable); and then
there's perhaps some AOP stuff possible to solve this issue.

Original comment by robbie.v...@gmail.com on 23 Aug 2007 at 8:57

GoogleCodeExporter commented 9 years ago
I have already implemented this feature using a scope wrapper, but 
unfortunately this
meta-scope was not able to retrieve the dependency from the original scope due 
to a
NullPointerException.

Just have a look at issue #190 and
http://groups.google.com/group/google-guice/browse_thread/thread/fd1c65570f0f07c
a for
a sample implementation.

Original comment by sven.lin...@gmail.com on 21 Apr 2008 at 10:27

GoogleCodeExporter commented 9 years ago
This is crazy reflective code you're writing, but generally the problem you're 
reporting is a real one.

The problem is that the Provider provided to Scope.scope() doesn't work when 
being executed somewhere 
other than a get() method. I'll try to write a simplified testcase that 
demonstrates the problem...

Original comment by limpbizkit on 4 Jun 2008 at 1:50

GoogleCodeExporter commented 9 years ago
As far as I remember, I attached an implementation and of course failing junit 
test
in issue #190. 

I digged a little bit into the problem, but unfortunately I am not very 
familiar with
the internals of Guice. It looks like the method call Scope.scope() is done 
using a
thread local instance of InternalContext located in the InjectorImpl, which is 
unset
after the scope() call. This rather looks like a design issue to me than a 
simple
programming bug.

Original comment by sven.lin...@gmail.com on 4 Jun 2008 at 10:05

GoogleCodeExporter commented 9 years ago

Original comment by limpbizkit on 5 Jun 2008 at 6:51

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago

Original comment by limpbizkit on 18 Nov 2008 at 7:33

GoogleCodeExporter commented 9 years ago
Solved with extension-like code (search the groups for LazyProvider & 
@LazySingleton).

Original comment by sberlin on 2 May 2010 at 12:40