Search in sources :

Example 1 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 2 with Element

use of com.google.inject.spi.Element in project shiro by apache.

the class ShiroAopModuleTest method testGetAnnotationResolver.

@Test
public void testGetAnnotationResolver() {
    final AnnotationResolver annotationResolver = new DefaultAnnotationResolver();
    ShiroAopModule underTest = new ShiroAopModule() {

        @Override
        protected AnnotationResolver createAnnotationResolver() {
            return annotationResolver;
        }

        @Override
        protected void configureDefaultInterceptors(AnnotationResolver resolver) {
            assertSame(annotationResolver, resolver);
            bind(Object.class).annotatedWith(Names.named("configureDefaultInterceptors"));
        }

        @Override
        protected void configureInterceptors(AnnotationResolver resolver) {
            assertSame(annotationResolver, resolver);
            bind(Object.class).annotatedWith(Names.named("configureInterceptors"));
        }
    };
    boolean calledDefault = false;
    boolean calledCustom = false;
    for (Element e : Elements.getElements(underTest)) {
        if (e instanceof Binding) {
            Key key = ((Binding) e).getKey();
            if (Named.class.isAssignableFrom(key.getAnnotation().annotationType()) && "configureInterceptors".equals(((Named) key.getAnnotation()).value()) && key.getTypeLiteral().getRawType().equals(Object.class)) {
                calledCustom = true;
            }
            if (Named.class.isAssignableFrom(key.getAnnotation().annotationType()) && "configureDefaultInterceptors".equals(((Named) key.getAnnotation()).value()) && key.getTypeLiteral().getRawType().equals(Object.class)) {
                calledDefault = true;
            }
        }
    }
}
Also used : Binding(com.google.inject.Binding) InterceptorBinding(com.google.inject.spi.InterceptorBinding) Element(com.google.inject.spi.Element) Key(com.google.inject.Key) Test(org.junit.Test)

Example 3 with Element

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

the class PrivateElementsImpl method applyTo.

@Override
public void applyTo(Binder binder) {
    PrivateBinder privateBinder = binder.withSource(source).newPrivateBinder();
    for (Element element : getElements()) {
        element.applyTo(privateBinder);
    }
    // ensure exposedKeysToSources is populated
    getExposedKeys();
    for (Map.Entry<Key<?>, Object> entry : exposedKeysToSources.entrySet()) {
        privateBinder.withSource(entry.getValue()).expose(entry.getKey());
    }
}
Also used : PrivateBinder(com.google.inject.PrivateBinder) Element(com.google.inject.spi.Element) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) Key(com.google.inject.Key)

Example 4 with Element

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

the class MethodInterceptionTest method testGetElements_interceptorBindings.

@Test
public void testGetElements_interceptorBindings() throws Exception {
    @SuppressWarnings("rawtypes") Matcher<Class> classMatcher = Matchers.subclassesOf(List.class);
    Matcher<Method> methodMatcher = Matchers.returns(Matchers.identicalTo(int.class));
    MethodInterceptor interceptor = new MethodInterceptor() {

        @Override
        public Object invoke(MethodInvocation methodInvocation) throws Throwable {
            return null;
        }
    };
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bindInterceptor(classMatcher, methodMatcher, interceptor);
        }
    });
    final List<InterceptorBinding> interceptorBindings = new ArrayList<>();
    for (Element element : injector.getElements()) {
        element.acceptVisitor(new DefaultElementVisitor<Void>() {

            @Override
            public Void visit(InterceptorBinding interceptorBinding) {
                interceptorBindings.add(interceptorBinding);
                return null;
            }
        });
    }
    assertThat(interceptorBindings).hasSize(1);
    InterceptorBinding extractedBinding = interceptorBindings.get(0);
    assertSame(classMatcher, extractedBinding.getClassMatcher());
    assertSame(methodMatcher, extractedBinding.getMethodMatcher());
    assertSame(interceptor, extractedBinding.getInterceptors().get(0));
}
Also used : Element(com.google.inject.spi.Element) ArrayList(java.util.ArrayList) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Method(java.lang.reflect.Method) InterceptorBinding(com.google.inject.spi.InterceptorBinding) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) Test(org.junit.Test)

Example 5 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)

Aggregations

Element (com.google.inject.spi.Element)33 Binding (com.google.inject.Binding)20 Key (com.google.inject.Key)17 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 DefaultBindingTargetVisitor (com.google.inject.spi.DefaultBindingTargetVisitor)7 InstanceBinding (com.google.inject.spi.InstanceBinding)7 LinkedKeyBinding (com.google.inject.spi.LinkedKeyBinding)7 ProviderInstanceBinding (com.google.inject.spi.ProviderInstanceBinding)7 ProviderKeyBinding (com.google.inject.spi.ProviderKeyBinding)7 Map (java.util.Map)7 ImmutableList (com.google.common.collect.ImmutableList)4 PrivateModule (com.google.inject.PrivateModule)4 IndexedBinding (com.google.inject.internal.Indexer.IndexedBinding)4 MapBinderBinding (com.google.inject.multibindings.MapBinderBinding)4 MultibinderBinding (com.google.inject.multibindings.MultibinderBinding)4 MultibindingsTargetVisitor (com.google.inject.multibindings.MultibindingsTargetVisitor)4 OptionalBinderBinding (com.google.inject.multibindings.OptionalBinderBinding)4