Search in sources :

Example 6 with ProviderMethod

use of com.google.inject.internal.ProviderMethod in project google-gin by gwtplus.

the class GuiceBindingVisitor method visit.

public Void visit(ProviderInstanceBinding<? extends T> providerInstanceBinding) {
    // Detect provider methods and handle them
    // TODO(bstoler): Update this when the SPI explicitly has a case for
    // provider methods
    Provider<? extends T> provider = providerInstanceBinding.getProviderInstance();
    if (provider instanceof ProviderMethod) {
        Context context = Context.forElement(providerInstanceBinding);
        bindingsCollection.addBinding(targetKey, bindingFactory.getProviderMethodBinding((ProviderMethod<?>) provider, context));
        return null;
    }
    if (provider instanceof GwtDotCreateProvider) {
        addImplicitBinding(providerInstanceBinding);
        return null;
    }
    // OTt, use the normal default handler (and error)
    return super.visit(providerInstanceBinding);
}
Also used : Context(com.google.gwt.inject.rebind.binding.Context) GwtDotCreateProvider(com.google.gwt.inject.rebind.adapter.GwtDotCreateProvider) ProviderMethod(com.google.inject.internal.ProviderMethod)

Example 7 with ProviderMethod

use of com.google.inject.internal.ProviderMethod in project roboguice by roboguice.

the class ProviderMethodsTest method testNonModuleProviderMethods.

public void testNonModuleProviderMethods() {
    final Object methodsObject = new Object() {

        @Provides
        @Named("foo")
        String provideFoo() {
            return "foo-value";
        }
    };
    Module module = new AbstractModule() {

        @Override
        protected void configure() {
            install(ProviderMethodsModule.forObject(methodsObject));
        }
    };
    Injector injector = Guice.createInjector(module);
    Key<String> key = Key.get(String.class, Names.named("foo"));
    assertEquals("foo-value", injector.getInstance(key));
    // Test the provider method object itself. This makes sure getInstance works, since GIN uses it
    List<Element> elements = Elements.getElements(module);
    assertEquals(1, elements.size());
    Element element = elements.get(0);
    assertTrue(element + " instanceof ProviderInstanceBinding", element instanceof ProviderInstanceBinding);
    ProviderInstanceBinding binding = (ProviderInstanceBinding) element;
    javax.inject.Provider provider = binding.getUserSuppliedProvider();
    assertTrue(provider instanceof ProviderMethod);
    assertEquals(methodsObject, ((ProviderMethod) provider).getInstance());
    assertSame(provider, binding.getProviderInstance());
}
Also used : Injector(com.google.inject.Injector) ProviderMethod(com.google.inject.internal.ProviderMethod) Module(com.google.inject.Module) ProviderMethodsModule(com.google.inject.internal.ProviderMethodsModule) AbstractModule(com.google.inject.AbstractModule) AbstractModule(com.google.inject.AbstractModule)

Aggregations

ProviderMethod (com.google.inject.internal.ProviderMethod)7 Injector (com.google.inject.Injector)4 AbstractModule (com.google.inject.AbstractModule)2 Module (com.google.inject.Module)2 ProviderMethodsModule (com.google.inject.internal.ProviderMethodsModule)2 GwtDotCreateProvider (com.google.gwt.inject.rebind.adapter.GwtDotCreateProvider)1 Context (com.google.gwt.inject.rebind.binding.Context)1 Binding (com.google.inject.Binding)1 Provider (com.google.inject.Provider)1 Scope (com.google.inject.Scope)1 DefaultBindingScopingVisitor (com.google.inject.spi.DefaultBindingScopingVisitor)1 ProviderInstanceBinding (com.google.inject.spi.ProviderInstanceBinding)1 ArrayList (java.util.ArrayList)1 IMocksControl (org.easymock.IMocksControl)1 Test (org.junit.Test)1