Search in sources :

Example 11 with Element

use of com.google.inject.spi.Element in project guice by google.

the class ServletScopesTest method testIsRequestScopedNegative.

public void testIsRequestScopedNegative() {
    final Key<String> a = Key.get(String.class, named("A"));
    final Key<String> b = Key.get(String.class, named("B"));
    final Key<String> c = Key.get(String.class, named("C"));
    final Key<String> d = Key.get(String.class, named("D"));
    final Key<String> e = Key.get(String.class, named("E"));
    final Key<String> f = Key.get(String.class, named("F"));
    final Key<String> g = Key.get(String.class, named("G"));
    final Key<String> h = Key.get(String.class, named("H"));
    final Key<String> i = Key.get(String.class, named("I"));
    final Key<String> j = Key.get(String.class, named("J"));
    Module requestScopedBindings = new AbstractModule() {

        @Override
        protected void configure() {
            bind(a).to(b);
            bind(b).to(c);
            bind(c).toProvider(Providers.of("c")).in(Scopes.NO_SCOPE);
            bind(d).toInstance("d");
            bind(e).toProvider(Providers.of("e")).asEagerSingleton();
            bind(f).toProvider(Providers.of("f")).in(Scopes.SINGLETON);
            bind(g).toProvider(Providers.of("g")).in(Singleton.class);
            bind(h).toProvider(Providers.of("h")).in(CustomScoped.class);
            bindScope(CustomScoped.class, Scopes.NO_SCOPE);
            install(new PrivateModule() {

                @Override
                protected void configure() {
                    bind(i).toProvider(Providers.of("i")).in(CustomScoped.class);
                    expose(i);
                }
            });
        }

        @Provides
        @Named("J")
        @CustomScoped
        String provideJ() {
            return "j";
        }
    };
    // we know the module contains only bindings
    @SuppressWarnings("unchecked") List<Element> moduleBindings = Elements.getElements(requestScopedBindings);
    ImmutableMap<Key<?>, Binding<?>> map = indexBindings(moduleBindings);
    assertFalse(ServletScopes.isRequestScoped(map.get(a)));
    assertFalse(ServletScopes.isRequestScoped(map.get(b)));
    assertFalse(ServletScopes.isRequestScoped(map.get(c)));
    assertFalse(ServletScopes.isRequestScoped(map.get(d)));
    assertFalse(ServletScopes.isRequestScoped(map.get(e)));
    assertFalse(ServletScopes.isRequestScoped(map.get(f)));
    assertFalse(ServletScopes.isRequestScoped(map.get(g)));
    assertFalse(ServletScopes.isRequestScoped(map.get(h)));
    assertFalse(ServletScopes.isRequestScoped(map.get(i)));
    assertFalse(ServletScopes.isRequestScoped(map.get(j)));
    Injector injector = Guice.createInjector(requestScopedBindings);
    assertFalse(ServletScopes.isRequestScoped(injector.getBinding(a)));
    assertFalse(ServletScopes.isRequestScoped(injector.getBinding(b)));
    assertFalse(ServletScopes.isRequestScoped(injector.getBinding(c)));
    assertFalse(ServletScopes.isRequestScoped(injector.getBinding(d)));
    assertFalse(ServletScopes.isRequestScoped(injector.getBinding(e)));
    assertFalse(ServletScopes.isRequestScoped(injector.getBinding(f)));
    assertFalse(ServletScopes.isRequestScoped(injector.getBinding(g)));
    assertFalse(ServletScopes.isRequestScoped(injector.getBinding(h)));
    assertFalse(ServletScopes.isRequestScoped(injector.getBinding(i)));
    assertFalse(ServletScopes.isRequestScoped(injector.getBinding(j)));
}
Also used : Binding(com.google.inject.Binding) Element(com.google.inject.spi.Element) AbstractModule(com.google.inject.AbstractModule) Injector(com.google.inject.Injector) Module(com.google.inject.Module) PrivateModule(com.google.inject.PrivateModule) AbstractModule(com.google.inject.AbstractModule) PrivateModule(com.google.inject.PrivateModule) Key(com.google.inject.Key)

Example 12 with Element

use of com.google.inject.spi.Element in project guice by google.

the class ServletScopesTest method indexBindings.

private ImmutableMap<Key<?>, Binding<?>> indexBindings(Iterable<Element> elements) {
    ImmutableMap.Builder<Key<?>, Binding<?>> builder = ImmutableMap.builder();
    for (Element element : elements) {
        if (element instanceof Binding) {
            Binding<?> binding = (Binding<?>) element;
            builder.put(binding.getKey(), binding);
        } else if (element instanceof PrivateElements) {
            PrivateElements privateElements = (PrivateElements) element;
            Map<Key<?>, Binding<?>> privateBindings = indexBindings(privateElements.getElements());
            for (Key<?> exposed : privateElements.getExposedKeys()) {
                builder.put(exposed, privateBindings.get(exposed));
            }
        }
    }
    return builder.build();
}
Also used : Binding(com.google.inject.Binding) PrivateElements(com.google.inject.spi.PrivateElements) Element(com.google.inject.spi.Element) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableMap(com.google.common.collect.ImmutableMap) Key(com.google.inject.Key)

Example 13 with Element

use of com.google.inject.spi.Element in project guice by google.

the class ExtensionSpiTest method testSpiOnElements.

public final void testSpiOnElements() {
    ServletSpiVisitor visitor = new ServletSpiVisitor(false);
    int count = 0;
    for (Element element : Elements.getElements(new Module())) {
        if (element instanceof Binding) {
            assertEquals(count++, ((Binding) element).acceptTargetVisitor(visitor));
        }
    }
    validateVisitor(visitor);
}
Also used : Binding(com.google.inject.Binding) Element(com.google.inject.spi.Element)

Example 14 with Element

use of com.google.inject.spi.Element in project guice by google.

the class SpiUtils method setModuleTest.

@SuppressWarnings("unchecked")
private static <T> void setModuleTest(Key<Set<T>> setKey, TypeLiteral<?> elementType, Iterable<? extends Module> modules, boolean allowDuplicates, int otherMultibindings, BindResult... results) {
    Key<?> collectionOfProvidersKey = setKey.ofType(collectionOfProvidersOf(elementType));
    Key<?> collectionOfJavaxProvidersKey = setKey.ofType(collectionOfJavaxProvidersOf(elementType));
    List<BindResult> bindResults = Lists.newArrayList(results);
    List<Element> elements = Elements.getElements(modules);
    Visitor<T> visitor = new Visitor<T>();
    MultibinderBinding<Set<T>> multibinder = null;
    for (Element element : elements) {
        if (element instanceof Binding && ((Binding) element).getKey().equals(setKey)) {
            multibinder = (MultibinderBinding<Set<T>>) ((Binding) element).acceptTargetVisitor(visitor);
            break;
        }
    }
    assertNotNull(multibinder);
    assertEquals(elementType, multibinder.getElementTypeLiteral());
    List<Object> otherMultibinders = Lists.newArrayList();
    Set<Element> otherContains = new HashSet<Element>();
    List<Element> otherElements = Lists.newArrayList();
    int duplicates = 0;
    Set<IndexedBinding> setOfIndexed = Sets.newHashSet();
    Indexer indexer = new Indexer(null);
    boolean collectionOfProvidersMatch = false;
    boolean collectionOfJavaxProvidersMatch = false;
    for (Element element : elements) {
        boolean contains = multibinder.containsElement(element);
        if (!contains) {
            otherElements.add(element);
        }
        boolean matched = false;
        Key key = null;
        if (element instanceof Binding) {
            Binding binding = (Binding) element;
            if (indexer.isIndexable(binding) && !setOfIndexed.add((IndexedBinding) binding.acceptTargetVisitor(indexer))) {
                duplicates++;
            }
            key = binding.getKey();
            Object visited = binding.acceptTargetVisitor(visitor);
            if (visited != null) {
                matched = true;
                if (visited.equals(multibinder)) {
                    assertTrue(contains);
                } else {
                    otherMultibinders.add(visited);
                }
            }
        }
        if (collectionOfProvidersKey.equals(key)) {
            assertTrue(contains);
            assertFalse(matched);
            collectionOfProvidersMatch = true;
        } else if (collectionOfJavaxProvidersKey.equals(key)) {
            assertTrue(contains);
            assertFalse(matched);
            collectionOfJavaxProvidersMatch = true;
        } else if (!matched && contains) {
            otherContains.add(element);
        }
    }
    if (allowDuplicates) {
        assertEquals("wrong contained elements: " + otherContains, bindResults.size() + 1 + duplicates, otherContains.size());
    } else {
        assertEquals("wrong contained elements: " + otherContains, bindResults.size() + duplicates, otherContains.size());
    }
    assertEquals("other multibindings found: " + otherMultibinders, otherMultibindings, otherMultibinders.size());
    assertTrue(collectionOfProvidersMatch);
    assertTrue(collectionOfJavaxProvidersMatch);
    // Validate that we can construct an injector out of the remaining bindings.
    Guice.createInjector(Elements.getModule(otherElements));
}
Also used : MultibinderBinding(com.google.inject.multibindings.MultibinderBinding) ProviderInstanceBinding(com.google.inject.spi.ProviderInstanceBinding) IndexedBinding(com.google.inject.internal.Indexer.IndexedBinding) Binding(com.google.inject.Binding) InstanceBinding(com.google.inject.spi.InstanceBinding) MapBinderBinding(com.google.inject.multibindings.MapBinderBinding) OptionalBinderBinding(com.google.inject.multibindings.OptionalBinderBinding) ProviderKeyBinding(com.google.inject.spi.ProviderKeyBinding) LinkedKeyBinding(com.google.inject.spi.LinkedKeyBinding) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) MultibindingsTargetVisitor(com.google.inject.multibindings.MultibindingsTargetVisitor) DefaultBindingTargetVisitor(com.google.inject.spi.DefaultBindingTargetVisitor) IndexedBinding(com.google.inject.internal.Indexer.IndexedBinding) Element(com.google.inject.spi.Element) Key(com.google.inject.Key) HashSet(java.util.HashSet)

Example 15 with Element

use of com.google.inject.spi.Element in project guice by google.

the class SpiUtils method optionalModuleTest.

@SuppressWarnings({ "unchecked", "rawtypes" })
private static <T> void optionalModuleTest(Key<T> keyType, Iterable<? extends Module> modules, int expectedOtherOptionalBindings, BindResult<?> expectedDefault, BindResult<?> expectedActual, BindResult<?> expectedUserLinkedActual) {
    if (expectedUserLinkedActual != null) {
        assertNull("cannot have actual if expecting user binding", expectedActual);
        assertNull("cannot have default if expecting user binding", expectedDefault);
    }
    Set<Element> elements = ImmutableSet.copyOf(Elements.getElements(modules));
    Map<Key<?>, Binding<?>> indexed = index(elements);
    Key<Optional<T>> optionalKey = keyType.ofType(RealOptionalBinder.optionalOf(keyType.getTypeLiteral()));
    Key<?> javaOptionalKey = HAS_JAVA_OPTIONAL ? keyType.ofType(RealOptionalBinder.javaOptionalOf(keyType.getTypeLiteral())) : null;
    Visitor visitor = new Visitor();
    OptionalBinderBinding<Optional<T>> optionalBinder = null;
    OptionalBinderBinding<?> javaOptionalBinder = null;
    Key<?> defaultKey = null;
    Key<?> actualKey = null;
    Binding optionalBinding = indexed.get(optionalKey);
    optionalBinder = (OptionalBinderBinding<Optional<T>>) optionalBinding.acceptTargetVisitor(visitor);
    if (HAS_JAVA_OPTIONAL) {
        Binding javaOptionalBinding = indexed.get(javaOptionalKey);
        javaOptionalBinder = (OptionalBinderBinding) javaOptionalBinding.acceptTargetVisitor(visitor);
    }
    // Locate the defaultKey & actualKey
    for (Element element : elements) {
        if (optionalBinder.containsElement(element) && element instanceof Binding) {
            Binding binding = (Binding) element;
            if (isSourceEntry(binding, RealOptionalBinder.Source.DEFAULT)) {
                defaultKey = binding.getKey();
            } else if (isSourceEntry(binding, RealOptionalBinder.Source.ACTUAL)) {
                actualKey = binding.getKey();
            }
        }
    }
    assertNotNull(optionalBinder);
    if (HAS_JAVA_OPTIONAL) {
        assertNotNull(javaOptionalBinder);
    }
    assertEquals(expectedDefault == null, defaultKey == null);
    assertEquals(expectedActual == null, actualKey == null);
    Key<Optional<javax.inject.Provider<T>>> optionalJavaxProviderKey = keyType.ofType(RealOptionalBinder.optionalOfJavaxProvider(keyType.getTypeLiteral()));
    Key<?> javaOptionalJavaxProviderKey = HAS_JAVA_OPTIONAL ? keyType.ofType(RealOptionalBinder.javaOptionalOfJavaxProvider(keyType.getTypeLiteral())) : null;
    Key<Optional<Provider<T>>> optionalProviderKey = keyType.ofType(RealOptionalBinder.optionalOfProvider(keyType.getTypeLiteral()));
    Key<?> javaOptionalProviderKey = HAS_JAVA_OPTIONAL ? keyType.ofType(RealOptionalBinder.javaOptionalOfProvider(keyType.getTypeLiteral())) : null;
    boolean keyMatch = false;
    boolean optionalKeyMatch = false;
    boolean javaOptionalKeyMatch = false;
    boolean optionalJavaxProviderKeyMatch = false;
    boolean javaOptionalJavaxProviderKeyMatch = false;
    boolean optionalProviderKeyMatch = false;
    boolean javaOptionalProviderKeyMatch = false;
    boolean defaultMatch = false;
    boolean actualMatch = false;
    List<Object> otherOptionalElements = Lists.newArrayList();
    List<Element> otherContains = Lists.newArrayList();
    List<Element> nonContainedElements = Lists.newArrayList();
    for (Element element : elements) {
        boolean contains = optionalBinder.containsElement(element);
        if (HAS_JAVA_OPTIONAL) {
            assertEquals(contains, javaOptionalBinder.containsElement(element));
        }
        if (!contains) {
            nonContainedElements.add(element);
        }
        Key key = null;
        Binding b = null;
        if (element instanceof Binding) {
            b = (Binding) element;
            key = b.getKey();
            Object visited = b.acceptTargetVisitor(visitor);
            if (visited instanceof OptionalBinderBinding) {
                if (visited.equals(optionalBinder)) {
                    assertTrue(contains);
                } else if (HAS_JAVA_OPTIONAL && visited.equals(javaOptionalBinder)) {
                    assertTrue(contains);
                } else {
                    otherOptionalElements.add(visited);
                }
            }
        } else if (element instanceof ProviderLookup) {
            key = ((ProviderLookup) element).getKey();
        }
        if (key != null && key.equals(keyType)) {
            // keyType might match because a user bound it
            // (which is possible in a purely absent OptionalBinder)
            assertEquals(expectedDefault != null || expectedActual != null, contains);
            if (contains) {
                keyMatch = true;
            }
        } else if (key != null && key.equals(optionalKey)) {
            assertTrue(contains);
            optionalKeyMatch = true;
        } else if (key != null && key.equals(javaOptionalKey)) {
            assertTrue(contains);
            javaOptionalKeyMatch = true;
        } else if (key != null && key.equals(optionalJavaxProviderKey)) {
            assertTrue(contains);
            optionalJavaxProviderKeyMatch = true;
        } else if (key != null && key.equals(javaOptionalJavaxProviderKey)) {
            assertTrue(contains);
            javaOptionalJavaxProviderKeyMatch = true;
        } else if (key != null && key.equals(optionalProviderKey)) {
            assertTrue(contains);
            optionalProviderKeyMatch = true;
        } else if (key != null && key.equals(javaOptionalProviderKey)) {
            assertTrue(contains);
            javaOptionalProviderKeyMatch = true;
        } else if (key != null && key.equals(defaultKey)) {
            assertTrue(contains);
            if (b != null) {
                // otherwise it might just be a ProviderLookup into it
                assertTrue("expected: " + expectedDefault + ", but was: " + b, matches(b, expectedDefault));
                defaultMatch = true;
            }
        } else if (key != null && key.equals(actualKey)) {
            assertTrue(contains);
            if (b != null) {
                // otherwise it might just be a ProviderLookup into it
                assertTrue("expected: " + expectedActual + ", but was: " + b, matches(b, expectedActual));
                actualMatch = true;
            }
        } else if (contains) {
            otherContains.add(element);
        }
    }
    // only expect a keymatch if either default or actual are set
    assertEquals(expectedDefault != null || expectedActual != null, keyMatch);
    assertTrue(optionalKeyMatch);
    assertTrue(optionalJavaxProviderKeyMatch);
    assertTrue(optionalProviderKeyMatch);
    assertEquals(HAS_JAVA_OPTIONAL, javaOptionalKeyMatch);
    assertEquals(HAS_JAVA_OPTIONAL, javaOptionalJavaxProviderKeyMatch);
    assertEquals(HAS_JAVA_OPTIONAL, javaOptionalProviderKeyMatch);
    assertEquals(expectedDefault != null, defaultMatch);
    assertEquals(expectedActual != null, actualMatch);
    assertEquals(otherContains.toString(), 0, otherContains.size());
    assertEquals("other OptionalBindings found: " + otherOptionalElements, expectedOtherOptionalBindings, otherOptionalElements.size());
    // Validate that we can construct an injector out of the remaining bindings.
    Guice.createInjector(Elements.getModule(nonContainedElements));
}
Also used : MultibinderBinding(com.google.inject.multibindings.MultibinderBinding) ProviderInstanceBinding(com.google.inject.spi.ProviderInstanceBinding) IndexedBinding(com.google.inject.internal.Indexer.IndexedBinding) Binding(com.google.inject.Binding) InstanceBinding(com.google.inject.spi.InstanceBinding) MapBinderBinding(com.google.inject.multibindings.MapBinderBinding) OptionalBinderBinding(com.google.inject.multibindings.OptionalBinderBinding) ProviderKeyBinding(com.google.inject.spi.ProviderKeyBinding) LinkedKeyBinding(com.google.inject.spi.LinkedKeyBinding) OptionalBinderBinding(com.google.inject.multibindings.OptionalBinderBinding) Optional(com.google.common.base.Optional) MultibindingsTargetVisitor(com.google.inject.multibindings.MultibindingsTargetVisitor) DefaultBindingTargetVisitor(com.google.inject.spi.DefaultBindingTargetVisitor) Element(com.google.inject.spi.Element) ProviderLookup(com.google.inject.spi.ProviderLookup) Key(com.google.inject.Key)

Aggregations

Element (com.google.inject.spi.Element)29 Binding (com.google.inject.Binding)18 Key (com.google.inject.Key)16 AbstractModule (com.google.inject.AbstractModule)13 Injector (com.google.inject.Injector)10 Module (com.google.inject.Module)10 ImmutableMap (com.google.common.collect.ImmutableMap)7 Map (java.util.Map)7 DefaultBindingTargetVisitor (com.google.inject.spi.DefaultBindingTargetVisitor)6 InstanceBinding (com.google.inject.spi.InstanceBinding)6 LinkedKeyBinding (com.google.inject.spi.LinkedKeyBinding)6 ProviderInstanceBinding (com.google.inject.spi.ProviderInstanceBinding)6 ProviderKeyBinding (com.google.inject.spi.ProviderKeyBinding)6 ImmutableList (com.google.common.collect.ImmutableList)4 PrivateModule (com.google.inject.PrivateModule)4 PrivateElements (com.google.inject.spi.PrivateElements)4 ProviderLookup (com.google.inject.spi.ProviderLookup)4 List (java.util.List)4 IndexedBinding (com.google.inject.internal.Indexer.IndexedBinding)3 IndexedBinding (com.google.inject.multibindings.Indexer.IndexedBinding)3