use of com.google.inject.AbstractModule in project guice by google.
the class FactoryModuleBuilderTest method testImplicitForwardingAssistedBindingFailsWithInterface.
public void testImplicitForwardingAssistedBindingFailsWithInterface() {
try {
Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(Car.class).to(Golf.class);
install(new FactoryModuleBuilder().build(ColoredCarFactory.class));
}
});
fail();
} catch (CreationException ce) {
assertContains(ce.getMessage(), "1) " + Car.class.getName() + " is an interface, not a concrete class.", "Unable to create AssistedInject factory.", "while locating " + Car.class.getName(), "at " + ColoredCarFactory.class.getName() + ".create(");
assertEquals(1, ce.getErrorMessages().size());
}
}
use of com.google.inject.AbstractModule in project guice by google.
the class BytecodeGenTest method setUp.
@Override
@SuppressWarnings("unchecked")
protected void setUp() throws Exception {
super.setUp();
ClassLoader testClassLoader = new TestVisibilityClassLoader(true);
proxyTestClass = (Class<ProxyTest>) testClassLoader.loadClass(ProxyTest.class.getName());
realClass = (Class<ProxyTestImpl>) testClassLoader.loadClass(ProxyTestImpl.class.getName());
testModule = new AbstractModule() {
@Override
public void configure() {
bind(proxyTestClass).to(realClass);
}
};
}
use of com.google.inject.AbstractModule in project guice by google.
the class Jsr330Test method testGuicifyWithDependencies.
public void testGuicifyWithDependencies() {
Provider<String> jsr330Provider = new Provider<String>() {
@Inject
double d;
int i;
@Inject
void injectMe(int i) {
this.i = i;
}
@Override
public String get() {
return d + "-" + i;
}
};
final com.google.inject.Provider<String> guicified = Providers.guicify(jsr330Provider);
assertTrue(guicified instanceof HasDependencies);
Set<Dependency<?>> actual = ((HasDependencies) guicified).getDependencies();
validateDependencies(actual, jsr330Provider.getClass());
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(String.class).toProvider(guicified);
bind(int.class).toInstance(1);
bind(double.class).toInstance(2.0d);
}
});
Binding<String> binding = injector.getBinding(String.class);
assertEquals("2.0-1", binding.getProvider().get());
validateDependencies(actual, jsr330Provider.getClass());
}
use of com.google.inject.AbstractModule in project guice by google.
the class Jsr330Test method testQualifiedInject.
public void testQualifiedInject() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(B.class).annotatedWith(Names.named("jodie")).toInstance(b);
bind(C.class).annotatedWith(Red.class).toInstance(c);
bind(D.class).annotatedWith(RED).toInstance(d);
bind(E.class).annotatedWith(Names.named("jesse")).toInstance(e);
bind(F.class);
}
});
F f = injector.getInstance(F.class);
assertSame(b, f.b);
assertSame(c, f.c);
assertSame(d, f.d);
assertSame(e, f.e);
}
use of com.google.inject.AbstractModule in project guice by google.
the class Jsr330Test method testBindProviderClass.
/**
* This test verifies that we can compile bindings to provider instances whose compile-time type
* implements javax.inject.Provider but not com.google.inject.Provider. For binary compatibility,
* we don't (and won't) support binding to instances of javax.inject.Provider.
*/
public void testBindProviderClass() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(B.class).toProvider(BProvider.class);
bind(B.class).annotatedWith(Names.named("1")).toProvider(BProvider.class);
bind(B.class).annotatedWith(Names.named("2")).toProvider(Key.get(BProvider.class));
bind(B.class).annotatedWith(Names.named("3")).toProvider(TypeLiteral.get(BProvider.class));
}
});
injector.getInstance(Key.get(B.class));
injector.getInstance(Key.get(B.class, Names.named("1")));
injector.getInstance(Key.get(B.class, Names.named("2")));
injector.getInstance(Key.get(B.class, Names.named("3")));
}
Aggregations