Search in sources :

Example 81 with Key

use of com.google.inject.Key in project guice by google.

the class ProvidesIntoTest method testDoubleScannerIsIgnored.

public void testDoubleScannerIsIgnored() {
    Injector injector = Guice.createInjector(MultibindingsScanner.asModule(), MultibindingsScanner.asModule(), new AbstractModule() {

        @Override
        protected void configure() {
        }

        @ProvidesIntoSet
        String provideFoo() {
            return "foo";
        }
    });
    assertEquals(ImmutableSet.of("foo"), injector.getInstance(new Key<Set<String>>() {
    }));
}
Also used : Injector(com.google.inject.Injector) Key(com.google.inject.Key) AbstractModule(com.google.inject.AbstractModule)

Example 82 with Key

use of com.google.inject.Key in project guice by google.

the class SpiBindingsTest method testProviderBinding.

public void testProviderBinding() {
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(String.class).toInstance("A");
        }
    });
    Key<Provider<String>> providerOfStringKey = new Key<Provider<String>>() {
    };
    Binding<Provider<String>> binding = injector.getBinding(providerOfStringKey);
    assertEquals(providerOfStringKey, binding.getKey());
    checkBindingSource(binding);
    assertTrue(binding instanceof ProviderBinding);
    binding.acceptTargetVisitor(new FailingTargetVisitor<Provider<String>>() {

        @Override
        public Void visit(ProviderBinding<? extends Provider<String>> binding) {
            assertEquals(Key.get(String.class), binding.getProvidedKey());
            return null;
        }
    });
}
Also used : Injector(com.google.inject.Injector) Key(com.google.inject.Key) AbstractModule(com.google.inject.AbstractModule) Provider(com.google.inject.Provider)

Example 83 with Key

use of com.google.inject.Key in project guice by google.

the class ProviderMethodsTest method testProviderMethods.

@SuppressWarnings("unchecked")
public void testProviderMethods() {
    Injector injector = Guice.createInjector(this);
    Bob bob = injector.getInstance(Bob.class);
    assertEquals("A Bob", bob.getName());
    Bob clone = injector.getInstance(Bob.class);
    assertEquals("A Bob", clone.getName());
    assertNotSame(bob, clone);
    assertSame(bob.getDaughter(), clone.getDaughter());
    Key soleBobKey = Key.get(Bob.class, Sole.class);
    assertSame(injector.getInstance(soleBobKey), injector.getInstance(soleBobKey));
}
Also used : Injector(com.google.inject.Injector) Key(com.google.inject.Key)

Example 84 with Key

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

the class ProviderMethodsTest method testWildcardProviderMethods.

public void testWildcardProviderMethods() {
    final List<String> strings = ImmutableList.of("A", "B", "C");
    final List<Number> numbers = ImmutableList.<Number>of(1, 2, 3);
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            @SuppressWarnings("unchecked") Key<List<? super Integer>> listOfSupertypesOfInteger = (Key<List<? super Integer>>) Key.get(Types.listOf(Types.supertypeOf(Integer.class)));
            bind(listOfSupertypesOfInteger).toInstance(numbers);
        }

        @Provides
        public List<? extends CharSequence> provideCharSequences() {
            return strings;
        }

        @Provides
        public Class<?> provideType() {
            return Float.class;
        }
    });
    assertSame(strings, injector.getInstance(HasWildcardInjection.class).charSequences);
    assertSame(numbers, injector.getInstance(HasWildcardInjection.class).numbers);
    assertSame(Float.class, injector.getInstance(HasWildcardInjection.class).type);
}
Also used : Provides(com.google.inject.Provides) AbstractModule(com.google.inject.AbstractModule) Injector(com.google.inject.Injector) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Key(com.google.inject.Key)

Example 85 with Key

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

the class ElementsTest method testNewPrivateBinder.

public void testNewPrivateBinder() {
    final Key<Collection> collection = Key.get(Collection.class, SampleAnnotation.class);
    final Key<ArrayList> arrayList = Key.get(ArrayList.class);
    final ImmutableSet<Key<?>> collections = ImmutableSet.<Key<?>>of(arrayList, collection);
    final Key<?> a = Key.get(String.class, Names.named("a"));
    final Key<?> b = Key.get(String.class, Names.named("b"));
    final ImmutableSet<Key<?>> ab = ImmutableSet.of(a, b);
    checkModule(new AbstractModule() {

        protected void configure() {
            PrivateBinder one = binder().newPrivateBinder();
            one.expose(ArrayList.class);
            one.expose(Collection.class).annotatedWith(SampleAnnotation.class);
            one.bind(List.class).to(ArrayList.class);
            PrivateBinder two = binder().withSource("1 FooBar").newPrivateBinder().withSource("2 FooBar");
            two.expose(String.class).annotatedWith(Names.named("a"));
            two.expose(b);
            two.bind(List.class).to(ArrayList.class);
        }
    }, new FailingElementVisitor() {

        @Override
        public Void visit(PrivateElements one) {
            assertEquals(collections, one.getExposedKeys());
            checkElements(one.getElements(), new FailingElementVisitor() {

                @Override
                public <T> Void visit(Binding<T> binding) {
                    assertEquals(Key.get(List.class), binding.getKey());
                    return null;
                }
            });
            return null;
        }
    }, new ExternalFailureVisitor() {

        @Override
        public Void visit(PrivateElements two) {
            assertEquals(ab, two.getExposedKeys());
            assertEquals("1 FooBar", two.getSource().toString());
            checkElements(two.getElements(), new ExternalFailureVisitor() {

                @Override
                public <T> Void visit(Binding<T> binding) {
                    assertEquals("2 FooBar", binding.getSource().toString());
                    assertEquals(Key.get(List.class), binding.getKey());
                    return null;
                }
            });
            return null;
        }
    });
}
Also used : Binding(com.google.inject.Binding) ArrayList(java.util.ArrayList) AbstractModule(com.google.inject.AbstractModule) PrivateBinder(com.google.inject.PrivateBinder) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) Key(com.google.inject.Key)

Aggregations

Key (com.google.inject.Key)106 AbstractModule (com.google.inject.AbstractModule)55 Injector (com.google.inject.Injector)52 Binding (com.google.inject.Binding)36 Module (com.google.inject.Module)20 Provider (com.google.inject.Provider)18 Element (com.google.inject.spi.Element)16 Map (java.util.Map)16 HasDependencies (com.google.inject.spi.HasDependencies)14 InstanceBinding (com.google.inject.spi.InstanceBinding)13 List (java.util.List)13 TypeLiteral (com.google.inject.TypeLiteral)12 LinkedKeyBinding (com.google.inject.spi.LinkedKeyBinding)10 ProviderInstanceBinding (com.google.inject.spi.ProviderInstanceBinding)9 PrivateModule (com.google.inject.PrivateModule)8 DefaultBindingTargetVisitor (com.google.inject.spi.DefaultBindingTargetVisitor)8 Dependency (com.google.inject.spi.Dependency)8 ProviderKeyBinding (com.google.inject.spi.ProviderKeyBinding)8 ImmutableList (com.google.common.collect.ImmutableList)7 InjectionPoint (com.google.inject.spi.InjectionPoint)7