Guice seems unable to inject Clazz<?> values. The example below
demonstrates a use case. The code was run with guice-1.0.
A possible fix as discussed with Kevin is to treat bindings of
Clazz.class as providing @Inject Clazz<?>.
package com.example;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.TypeLiteral;
import junit.framework.TestCase;
public class GuiceCaptureTest extends TestCase {
public void testAnyTypeCapture() throws Exception {
Injector injector = Guice.createInjector(new Module());
assertNotNull(injector.getInstance( // this works
Key.<Parameterized<String>>get(new
TypeLiteral<Parameterized<String>>() {})));
assertNotNull(injector.getInstance(Foo.class).p); // this bombs,
exception below
}
static class Module extends AbstractModule {
@Override
protected void configure() {
// This binding is useless because a TypeLiteral has to be fully
specified.
bind(new TypeLiteral<Parameterized<?>>() {})
.to(ParameterizedImpl.class);
bind(new TypeLiteral<Parameterized<String>>() {})
.to(ParameterizedImpl.class);
// This is the proposed binding which doesn't work yet.
bind(Parameterized.class).to(ParameterizedImpl.class);
}
}
interface Parameterized<T> {
T check();
}
static class ParameterizedImpl implements Parameterized<String> {
@Override
public String check() {
return null;
}
}
static class Foo {
private Parameterized<?> p;
@Inject Foo(Parameterized<?> p) {
this.p = p;
}
}
}
/*
com.google.inject.ConfigurationException:
Error at com.example.GuiceCaptureTest$Foo.<init>(GuiceCaptureTest.java:47)
Binding to
com.example.GuiceCaptureTest.com.example.GuiceCaptureTest$Parameterized<?>
not found.
No bindings to that type were found.
at
com.google.inject.BinderImpl$RuntimeErrorHandler.handle(BinderImpl.java:426)
at com.google.inject.AbstractErrorHandler.handle(AbstractErrorHandler.java:30)
at com.google.inject.ErrorMessages.handleMissingBinding(ErrorMessages.java:46)
at
com.google.inject.InjectorImpl$MissingDependencyException.handle(InjectorImpl.ja
va:791)
at
com.google.inject.ConstructorInjector.createParameterInjector(ConstructorInjecto
r.java:66)
at com.google.inject.ConstructorInjector.<init>(ConstructorInjector.java:38)
at com.google.inject.InjectorImpl$7.create(InjectorImpl.java:601)
at com.google.inject.InjectorImpl$7.create(InjectorImpl.java:594)
at com.google.inject.util.ReferenceCache.create(ReferenceCache.java:53)
at
com.google.inject.util.AbstractReferenceCache.internalCreate(AbstractReferenceCa
che.java:59)
at
com.google.inject.util.AbstractReferenceCache.get(AbstractReferenceCache.java:11
6)
at com.google.inject.InjectorImpl.getConstructor(InjectorImpl.java:765)
at com.google.inject.InjectorImpl.getImplicitBinding(InjectorImpl.java:973)
at com.google.inject.InjectorImpl.getInternalFactory(InjectorImpl.java:308)
at com.google.inject.InjectorImpl.getProvider(InjectorImpl.java:693)
at com.google.inject.InjectorImpl.getProvider(InjectorImpl.java:689)
at com.google.inject.InjectorImpl.getInstance(InjectorImpl.java:728)
at com.example.GuiceCaptureTest.testAnyTypeCapture(GuiceCaptureTest.java:17)
*/
Original issue reported on code.google.com by blackgne...@gmail.com on 24 Oct 2007 at 8:47
Original issue reported on code.google.com by
blackgne...@gmail.com
on 24 Oct 2007 at 8:47