Search in sources :

Example 21 with Provider

use of com.google.inject.Provider in project roboguice by roboguice.

the class ElementsTest method testBindToProvider.

public void testBindToProvider() {
    final Provider<String> aProvider = new Provider<String>() {

        public String get() {
            return "A";
        }
    };
    final javax.inject.Provider<Integer> intJavaxProvider = new javax.inject.Provider<Integer>() {

        public Integer get() {
            return 42;
        }
    };
    final javax.inject.Provider<Double> doubleJavaxProvider = new javax.inject.Provider<Double>() {

        @javax.inject.Inject
        String string;

        public Double get() {
            return 42.42;
        }
    };
    checkModule(new AbstractModule() {

        protected void configure() {
            bind(String.class).toProvider(aProvider);
            bind(Integer.class).toProvider(intJavaxProvider);
            bind(Double.class).toProvider(doubleJavaxProvider);
            bind(List.class).toProvider(ListProvider.class);
            bind(Collection.class).toProvider(Key.get(ListProvider.class));
            bind(Iterable.class).toProvider(new TypeLiteral<TProvider<List>>() {
            });
        }
    }, new FailingElementVisitor() {

        @Override
        public <T> Void visit(Binding<T> command) {
            assertTrue(command instanceof ProviderInstanceBinding);
            assertEquals(Key.get(String.class), command.getKey());
            command.acceptTargetVisitor(new FailingTargetVisitor<T>() {

                @Override
                public Void visit(ProviderInstanceBinding<? extends T> binding) {
                    assertSame(aProvider, binding.getUserSuppliedProvider());
                    assertSame(aProvider, binding.getProviderInstance());
                    return null;
                }
            });
            return null;
        }
    }, new FailingElementVisitor() {

        @Override
        public <T> Void visit(Binding<T> command) {
            assertTrue(command instanceof ProviderInstanceBinding);
            assertEquals(Key.get(Integer.class), command.getKey());
            command.acceptTargetVisitor(new FailingTargetVisitor<T>() {

                @Override
                public Void visit(ProviderInstanceBinding<? extends T> binding) {
                    assertSame(intJavaxProvider, binding.getUserSuppliedProvider());
                    assertEquals(42, binding.getProviderInstance().get());
                    // we don't wrap this w/ dependencies if there were none.
                    assertFalse(binding.getProviderInstance() instanceof HasDependencies);
                    return null;
                }
            });
            return null;
        }
    }, new FailingElementVisitor() {

        @Override
        public <T> Void visit(Binding<T> command) {
            assertTrue(command instanceof ProviderInstanceBinding);
            assertEquals(Key.get(Double.class), command.getKey());
            command.acceptTargetVisitor(new FailingTargetVisitor<T>() {

                @Override
                public Void visit(ProviderInstanceBinding<? extends T> binding) {
                    assertSame(doubleJavaxProvider, binding.getUserSuppliedProvider());
                    assertEquals(42.42, binding.getProviderInstance().get());
                    // we do wrap it with dependencies if there were some.
                    assertTrue(binding.getProviderInstance() instanceof HasDependencies);
                    Set<Dependency<?>> deps = ((HasDependencies) binding.getProviderInstance()).getDependencies();
                    assertEquals(1, deps.size());
                    assertEquals(String.class, deps.iterator().next().getKey().getTypeLiteral().getRawType());
                    return null;
                }
            });
            return null;
        }
    }, new FailingElementVisitor() {

        @Override
        public <T> Void visit(Binding<T> command) {
            assertTrue(command instanceof ProviderKeyBinding);
            assertEquals(Key.get(List.class), command.getKey());
            command.acceptTargetVisitor(new FailingTargetVisitor<T>() {

                @Override
                public Void visit(ProviderKeyBinding<? extends T> binding) {
                    assertEquals(Key.get(ListProvider.class), binding.getProviderKey());
                    return null;
                }
            });
            return null;
        }
    }, new FailingElementVisitor() {

        @Override
        public <T> Void visit(Binding<T> command) {
            assertTrue(command instanceof ProviderKeyBinding);
            assertEquals(Key.get(Collection.class), command.getKey());
            command.acceptTargetVisitor(new FailingTargetVisitor<T>() {

                @Override
                public Void visit(ProviderKeyBinding<? extends T> binding) {
                    assertEquals(Key.get(ListProvider.class), binding.getProviderKey());
                    return null;
                }
            });
            return null;
        }
    }, new FailingElementVisitor() {

        @Override
        public <T> Void visit(Binding<T> command) {
            assertTrue(command instanceof ProviderKeyBinding);
            assertEquals(Key.get(Iterable.class), command.getKey());
            command.acceptTargetVisitor(new FailingTargetVisitor<T>() {

                @Override
                public Void visit(ProviderKeyBinding<? extends T> binding) {
                    assertEquals(new Key<TProvider<List>>() {
                    }, binding.getProviderKey());
                    return null;
                }
            });
            return null;
        }
    });
}
Also used : TypeLiteral(com.google.inject.TypeLiteral) ArrayList(java.util.ArrayList) List(java.util.List) Provider(com.google.inject.Provider) AbstractModule(com.google.inject.AbstractModule) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 22 with Provider

use of com.google.inject.Provider in project roboguice by roboguice.

the class BinderTest method testBindingToProvider.

public void testBindingToProvider() {
    try {
        Guice.createInjector(new AbstractModule() {

            @Override
            protected void configure() {
                bind(new TypeLiteral<Provider<String>>() {
                }).toInstance(Providers.of("A"));
            }
        });
        fail();
    } catch (CreationException expected) {
        assertContains(expected.getMessage(), "1) Binding to Provider is not allowed.", "at " + BinderTest.class.getName(), getDeclaringSourcePart(getClass()));
    }
}
Also used : CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule) Provider(com.google.inject.Provider)

Example 23 with Provider

use of com.google.inject.Provider in project roboguice by roboguice.

the class Providers method guicify.

/**
   * Returns a Guice-friendly {@code com.google.inject.Provider} for the given
   * JSR-330 {@code javax.inject.Provider}. The converse method is unnecessary,
   * since Guice providers directly implement the JSR-330 interface.
   * 
   * @since 3.0
   */
public static <T> Provider<T> guicify(javax.inject.Provider<T> provider) {
    if (provider instanceof Provider) {
        return (Provider<T>) provider;
    }
    final javax.inject.Provider<T> delegate = checkNotNull(provider, "provider");
    // Ensure that we inject all injection points from the delegate provider.
    Set<InjectionPoint> injectionPoints = InjectionPoint.forInstanceMethodsAndFields(provider.getClass());
    if (injectionPoints.isEmpty()) {
        return new GuicifiedProvider<T>(delegate);
    } else {
        Set<Dependency<?>> mutableDeps = Sets.newHashSet();
        for (InjectionPoint ip : injectionPoints) {
            mutableDeps.addAll(ip.getDependencies());
        }
        final Set<Dependency<?>> dependencies = ImmutableSet.copyOf(mutableDeps);
        return new GuicifiedProviderWithDependencies<T>(dependencies, delegate);
    }
}
Also used : InjectionPoint(com.google.inject.spi.InjectionPoint) Dependency(com.google.inject.spi.Dependency) Provider(com.google.inject.Provider)

Example 24 with Provider

use of com.google.inject.Provider in project roboguice by roboguice.

the class BoundFieldModuleTest method testExplicitlyBoundRawProviderCanBeBound.

public void testExplicitlyBoundRawProviderCanBeBound() {
    final Integer testValue = 1024;
    Object instance = new Object() {

        @Bind(to = Integer.class)
        private Provider anIntProvider = new Provider() {

            @Override
            public Object get() {
                return testValue;
            }
        };
    };
    BoundFieldModule module = BoundFieldModule.of(instance);
    Injector injector = Guice.createInjector(module);
    assertEquals(testValue, injector.getInstance(Integer.class));
}
Also used : Injector(com.google.inject.Injector) Provider(com.google.inject.Provider)

Example 25 with Provider

use of com.google.inject.Provider in project roboguice by roboguice.

the class CheckedProviderTest method testProvisionExceptionOnDependenciesOfCxtor.

public void testProvisionExceptionOnDependenciesOfCxtor() throws Exception {
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            ThrowingProviderBinder.create(binder()).bind(RemoteProvider.class, Foo.class).providing(ProvisionExceptionFoo.class);
            bindScope(BadScope.class, new Scope() {

                @Override
                public <T> Provider<T> scope(Key<T> key, Provider<T> unscoped) {
                    return new Provider<T>() {

                        @Override
                        public T get() {
                            throw new OutOfScopeException("failure");
                        }
                    };
                }
            });
        }
    });
    try {
        injector.getInstance(Key.get(remoteProviderOfFoo)).get();
        fail();
    } catch (ProvisionException pe) {
        assertEquals(2, pe.getErrorMessages().size());
        List<Message> messages = Lists.newArrayList(pe.getErrorMessages());
        assertEquals("Error in custom provider, com.google.inject.OutOfScopeException: failure", messages.get(0).getMessage());
        assertEquals("Error in custom provider, com.google.inject.OutOfScopeException: failure", messages.get(1).getMessage());
    }
}
Also used : ProvisionException(com.google.inject.ProvisionException) Scope(com.google.inject.Scope) Injector(com.google.inject.Injector) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) OutOfScopeException(com.google.inject.OutOfScopeException) Key(com.google.inject.Key) AbstractModule(com.google.inject.AbstractModule) Provider(com.google.inject.Provider)

Aggregations

Provider (com.google.inject.Provider)133 Injector (com.google.inject.Injector)72 AbstractModule (com.google.inject.AbstractModule)63 Module (com.google.inject.Module)44 Key (com.google.inject.Key)17 TypeLiteral (com.google.inject.TypeLiteral)10 ArrayList (java.util.ArrayList)10 ProvisionException (com.google.inject.ProvisionException)9 Dependency (com.google.inject.spi.Dependency)9 Map (java.util.Map)8 Before (org.junit.Before)7 CurrentUser (com.google.gerrit.server.CurrentUser)6 RequestContext (com.google.gerrit.server.util.RequestContext)6 ThreadLocalRequestContext (com.google.gerrit.server.util.ThreadLocalRequestContext)6 InMemoryModule (com.google.gerrit.testutil.InMemoryModule)6 PrivateModule (com.google.inject.PrivateModule)6 Scope (com.google.inject.Scope)6 List (java.util.List)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 Annotation (java.lang.annotation.Annotation)5