pombreda / google-guice

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

NPE in com.google.inject.ProviderToInternalFactoryAdapter$1.call() #249

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Guice 1.0... I have the following binding:

binder.bind(Key.get(PermanentRedirect.class,
Names.named("RedirectMain"))).in(Scopes.SINGLETON);

and the following warp-persist code:

Map<String, String> redirectMain = new HashMap<String, String>();
redirectMain.put("source", "/Main");
redirectMain.put("target", "/main");
Servlets.configure().filters().filterRegex("/Main/*").through(Key.get(PermanentR
edirect.class,
Names.named("RedirectMain")), redirectMain);

Guice throws the following exception:

java.lang.NullPointerException
        at
com.google.inject.ProviderToInternalFactoryAdapter$1.call(ProviderToInternalFact
oryAdapter.java:37)
        at com.google.inject.InjectorImpl.callInContext(InjectorImpl.java:756)
        at
com.google.inject.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactory
Adapter.java:35)
        at com.google.inject.Scopes$1$1.get(Scopes.java:53)
        at
com.google.inject.InternalFactoryToProviderAdapter.get(InternalFactoryToProvider
Adapter.java:41)
        at com.google.inject.InjectorImpl$9$1.call(InjectorImpl.java:708)
        at com.google.inject.InjectorImpl.callInContext(InjectorImpl.java:747)
        at com.google.inject.InjectorImpl$9.get(InjectorImpl.java:702)
        at com.google.inject.InjectorImpl.getInstance(InjectorImpl.java:724)
        at
com.wideplay.warp.servlet.FilterDefinition.init(FilterDefinition.java:53)
        at
com.wideplay.warp.servlet.ManagedFilterPipeline.initPipeline(ManagedFilterPipeli
ne.java:39)
        at com.wideplay.warp.servlet.WebFilter.init(WebFilter.java:55)
        at
org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConf
ig.java:257)
        at
org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterC
onfig.java:369)
        at
org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.
java:103)
        at
org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4389)
        at
org.apache.catalina.core.StandardContext.start(StandardContext.java:5189)
        at com.sun.enterprise.web.WebModule.start(WebModule.java:326)
        at
com.sun.enterprise.web.LifecycleStarter.doRun(LifecycleStarter.java:58)
        at
com.sun.appserv.management.util.misc.RunnableBase.runSync(RunnableBase.java:304)
        at
com.sun.appserv.management.util.misc.RunnableBase.run(RunnableBase.java:341)
        at
java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
        at java.util.concurrent.FutureTask.run(FutureTask.java:138)
        at
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:8
86)
        at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
        at java.lang.Thread.run(Thread.java:619)

Original issue reported on code.google.com by gili.tza...@gmail.com on 23 Sep 2008 at 1:24

GoogleCodeExporter commented 9 years ago
See related discussion:
http://groups.google.com/group/warp-core/tree/browse_frm/thread/6aef135cd349f62b
/71572508e4640fce?rnum=11&_done=%2Fgroup%2Fwarp-core%2Fbrowse_frm%2Fthread%2F6ae
f135cd349f62b%3F#doc_b3c92603aeab6453

Original comment by gili.tza...@gmail.com on 23 Sep 2008 at 1:26

GoogleCodeExporter commented 9 years ago
Turns out this is caused by fact that Key.get() was not bound to() anything. If 
you
remove in(Scopes.SINGLETON) Guice will notice this and issue an error:

com.google.inject.ConfigurationException: Missing binding to
PermanentRedirect annotated with
@com.google.inject.name.Named(value=RedirectMain).

It seems that Scopes.SINGLETON does not expect or check for this kind of error 
so it
dies with a cryptic NPE instead.

Please add this error checking into Scopes.SINGLETON.

Original comment by gili.tza...@gmail.com on 24 Sep 2008 at 4:36

GoogleCodeExporter commented 9 years ago
Better yet, the in() method should require to() to have been invoked 
beforehand. This
has the added benefit of fixing the problem across all (future) Scope 
implementations.

Original comment by gili.tza...@gmail.com on 24 Sep 2008 at 5:26

GoogleCodeExporter commented 9 years ago
As mentioned above, for an "AbstractModule" subclass this is equivalent to 

bind(PermanentRedirect.class)
  .annotatedWith(Names.named("RedirectMain"))
  .in(Scopes.SINGLETON);

This is not a problem with Scopes.SINGLETON, but rather that the provider 
passed into
the scope is a "ProviderToInternalFactoryAdapter" with a null internalFactory.

The same thing without the annotation does work OK:
bind(PermanentRedirect.class)
  .in(Scopes.SINGLETON);

So it seems that this is a bug.

As a work-around, you may want something like this:
bind(PermanentRedirect.class)
  .annotatedWith(Names.named("RedirectMain"))
  .to(PermanentRedirect.class)
  .in(Scopes.SINGLETON);

However, that also produces an error. The one work-around I have found that 
does work
is as follows:

bind(PermanentRedirect.class)
  .annotatedWith(Names.named("RedirectMain"))
  .to(new TypeLiteral<PermanentRedirect>() {})
  .in(Scopes.SINGLETON);

Original comment by tonyand...@gmail.com on 2 Oct 2008 at 2:37

GoogleCodeExporter commented 9 years ago
I believe this is fixed in current snapshots.

Original comment by limpbizkit on 30 Dec 2008 at 11:57

GoogleCodeExporter commented 9 years ago
Confirmed against r748.

Original comment by gili.tza...@gmail.com on 31 Dec 2008 at 12:15