Search in sources :

Example 51 with AbstractModule

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());
    }
}
Also used : CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule)

Example 52 with AbstractModule

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);
        }
    };
}
Also used : URLClassLoader(java.net.URLClassLoader) AbstractModule(com.google.inject.AbstractModule)

Example 53 with AbstractModule

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());
}
Also used : Dependency(com.google.inject.spi.Dependency) HasDependencies(com.google.inject.spi.HasDependencies) Provider(javax.inject.Provider) AbstractModule(com.google.inject.AbstractModule) Injector(com.google.inject.Injector)

Example 54 with AbstractModule

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);
}
Also used : RUNTIME(java.lang.annotation.RetentionPolicy.RUNTIME) Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Example 55 with AbstractModule

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")));
}
Also used : Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Aggregations

AbstractModule (com.google.inject.AbstractModule)845 Injector (com.google.inject.Injector)525 Module (com.google.inject.Module)232 CreationException (com.google.inject.CreationException)162 Provider (com.google.inject.Provider)64 Key (com.google.inject.Key)55 ConfigModule (co.cask.cdap.common.guice.ConfigModule)54 AuthorizationEnforcementModule (co.cask.cdap.security.authorization.AuthorizationEnforcementModule)49 DataSetsModules (co.cask.cdap.data.runtime.DataSetsModules)46 PrivateModule (com.google.inject.PrivateModule)46 AuthenticationContextModules (co.cask.cdap.security.auth.context.AuthenticationContextModules)45 AuthorizationTestModule (co.cask.cdap.security.authorization.AuthorizationTestModule)43 TypeLiteral (com.google.inject.TypeLiteral)43 DiscoveryRuntimeModule (co.cask.cdap.common.guice.DiscoveryRuntimeModule)42 BeforeClass (org.junit.BeforeClass)38 Map (java.util.Map)37 CConfiguration (co.cask.cdap.common.conf.CConfiguration)36 UnsupportedUGIProvider (co.cask.cdap.security.impersonation.UnsupportedUGIProvider)36 NonCustomLocationUnitTestModule (co.cask.cdap.common.guice.NonCustomLocationUnitTestModule)35 DefaultOwnerAdmin (co.cask.cdap.security.impersonation.DefaultOwnerAdmin)35