Search in sources :

Example 76 with Binding

use of com.google.inject.Binding in project gerrit by GerritCodeReview.

the class PrivateInternals_DynamicTypes method dynamicMapsOf.

public static Map<TypeLiteral<?>, DynamicMap<?>> dynamicMapsOf(Injector src) {
    Map<TypeLiteral<?>, DynamicMap<?>> m = new HashMap<>();
    for (Map.Entry<Key<?>, Binding<?>> e : src.getBindings().entrySet()) {
        TypeLiteral<?> type = e.getKey().getTypeLiteral();
        if (type.getRawType() == DynamicMap.class) {
            ParameterizedType p = (ParameterizedType) type.getType();
            m.put(TypeLiteral.get(p.getActualTypeArguments()[0]), (DynamicMap<?>) e.getValue().getProvider().get());
        }
    }
    if (m.isEmpty()) {
        return Collections.emptyMap();
    }
    return Collections.unmodifiableMap(m);
}
Also used : Binding(com.google.inject.Binding) ParameterizedType(java.lang.reflect.ParameterizedType) TypeLiteral(com.google.inject.TypeLiteral) HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap) Key(com.google.inject.Key)

Example 77 with Binding

use of com.google.inject.Binding in project gerrit by GerritCodeReview.

the class PrivateInternals_DynamicTypes method dynamicSetsOf.

public static Map<TypeLiteral<?>, DynamicSet<?>> dynamicSetsOf(Injector src) {
    Map<TypeLiteral<?>, DynamicSet<?>> m = new HashMap<>();
    for (Map.Entry<Key<?>, Binding<?>> e : src.getBindings().entrySet()) {
        TypeLiteral<?> type = e.getKey().getTypeLiteral();
        if (type.getRawType() == DynamicSet.class) {
            ParameterizedType p = (ParameterizedType) type.getType();
            m.put(TypeLiteral.get(p.getActualTypeArguments()[0]), (DynamicSet<?>) e.getValue().getProvider().get());
        }
    }
    if (m.isEmpty()) {
        return Collections.emptyMap();
    }
    return Collections.unmodifiableMap(m);
}
Also used : Binding(com.google.inject.Binding) ParameterizedType(java.lang.reflect.ParameterizedType) TypeLiteral(com.google.inject.TypeLiteral) HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap) Key(com.google.inject.Key)

Example 78 with Binding

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

the class ExtensionSpiTest method testSpiOnInjector.

public final void testSpiOnInjector() {
    ServletSpiVisitor visitor = new ServletSpiVisitor(true);
    int count = 0;
    Injector injector = Guice.createInjector(new Module());
    for (Binding binding : injector.getBindings().values()) {
        assertEquals(count++, binding.acceptTargetVisitor(visitor));
    }
    validateVisitor(visitor);
}
Also used : Binding(com.google.inject.Binding) Injector(com.google.inject.Injector)

Example 79 with Binding

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

the class FilterDefinitionTest method testFilterCreateDispatchDestroySupressChain.

public final void testFilterCreateDispatchDestroySupressChain() throws ServletException, IOException {
    Injector injector = createMock(Injector.class);
    Binding binding = createMock(Binding.class);
    HttpServletRequest request = createMock(HttpServletRequest.class);
    final MockFilter mockFilter = new MockFilter() {

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) {
        //suppress rest of chain...
        }
    };
    expect(binding.acceptScopingVisitor((BindingScopingVisitor) anyObject())).andReturn(true);
    expect(injector.getBinding(Key.get(Filter.class))).andReturn(binding);
    expect(injector.getInstance(Key.get(Filter.class))).andReturn(mockFilter).anyTimes();
    expect(request.getRequestURI()).andReturn("/index.html");
    expect(request.getContextPath()).andReturn("").anyTimes();
    replay(injector, binding, request);
    String pattern = "/*";
    final FilterDefinition filterDef = new FilterDefinition(pattern, Key.get(Filter.class), UriPatternType.get(UriPatternType.SERVLET, pattern), new HashMap<String, String>(), null);
    //should fire on mockfilter now
    filterDef.init(createMock(ServletContext.class), injector, Sets.<Filter>newIdentityHashSet());
    assertTrue(filterDef.getFilter() instanceof MockFilter);
    assertTrue("init did not fire", mockFilter.isInit());
    Filter matchingFilter = filterDef.getFilterIfMatching(request);
    assertSame(mockFilter, matchingFilter);
    final boolean[] proceed = new boolean[1];
    matchingFilter.doFilter(request, null, new FilterChainInvocation(null, null, null) {

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
            proceed[0] = true;
        }
    });
    assertFalse("filter did not suppress chain", proceed[0]);
    filterDef.destroy(Sets.<Filter>newIdentityHashSet());
    assertTrue("destroy did not fire", mockFilter.isDestroy());
    verify(injector, request);
}
Also used : Binding(com.google.inject.Binding) ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletResponse(javax.servlet.ServletResponse) FilterChain(javax.servlet.FilterChain) HttpServletRequest(javax.servlet.http.HttpServletRequest) Filter(javax.servlet.Filter) Injector(com.google.inject.Injector) ServletContext(javax.servlet.ServletContext)

Example 80 with Binding

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

the class FilterDefinitionTest method testFilterCreateDispatchDestroy.

public final void testFilterCreateDispatchDestroy() throws ServletException, IOException {
    Injector injector = createMock(Injector.class);
    Binding binding = createMock(Binding.class);
    HttpServletRequest request = createMock(HttpServletRequest.class);
    final MockFilter mockFilter = new MockFilter();
    expect(binding.acceptScopingVisitor((BindingScopingVisitor) anyObject())).andReturn(true);
    expect(injector.getBinding(Key.get(Filter.class))).andReturn(binding);
    expect(injector.getInstance(Key.get(Filter.class))).andReturn(mockFilter).anyTimes();
    expect(request.getRequestURI()).andReturn("/index.html");
    expect(request.getContextPath()).andReturn("").anyTimes();
    replay(injector, binding, request);
    String pattern = "/*";
    final FilterDefinition filterDef = new FilterDefinition(pattern, Key.get(Filter.class), UriPatternType.get(UriPatternType.SERVLET, pattern), new HashMap<String, String>(), null);
    //should fire on mockfilter now
    filterDef.init(createMock(ServletContext.class), injector, Sets.<Filter>newIdentityHashSet());
    assertTrue(filterDef.getFilter() instanceof MockFilter);
    assertTrue("Init did not fire", mockFilter.isInit());
    Filter matchingFilter = filterDef.getFilterIfMatching(request);
    assertSame(mockFilter, matchingFilter);
    final boolean[] proceed = new boolean[1];
    matchingFilter.doFilter(request, null, new FilterChainInvocation(null, null, null) {

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
            proceed[0] = true;
        }
    });
    assertTrue("Filter did not proceed down chain", proceed[0]);
    filterDef.destroy(Sets.<Filter>newIdentityHashSet());
    assertTrue("Destroy did not fire", mockFilter.isDestroy());
    verify(injector, request);
}
Also used : Binding(com.google.inject.Binding) ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletResponse(javax.servlet.ServletResponse) HttpServletRequest(javax.servlet.http.HttpServletRequest) Filter(javax.servlet.Filter) Injector(com.google.inject.Injector) ServletContext(javax.servlet.ServletContext)

Aggregations

Binding (com.google.inject.Binding)92 Injector (com.google.inject.Injector)58 Key (com.google.inject.Key)36 AbstractModule (com.google.inject.AbstractModule)33 InstanceBinding (com.google.inject.spi.InstanceBinding)23 Map (java.util.Map)21 HttpServletRequest (javax.servlet.http.HttpServletRequest)21 Module (com.google.inject.Module)18 Element (com.google.inject.spi.Element)18 ProviderInstanceBinding (com.google.inject.spi.ProviderInstanceBinding)17 LinkedKeyBinding (com.google.inject.spi.LinkedKeyBinding)16 HttpServlet (javax.servlet.http.HttpServlet)14 HttpServletResponse (javax.servlet.http.HttpServletResponse)13 DefaultBindingTargetVisitor (com.google.inject.spi.DefaultBindingTargetVisitor)12 ProviderKeyBinding (com.google.inject.spi.ProviderKeyBinding)12 ServletContext (javax.servlet.ServletContext)12 ImmutableMap (com.google.common.collect.ImmutableMap)11 HashMap (java.util.HashMap)11 TypeLiteral (com.google.inject.TypeLiteral)10 MapBinderBinding (com.google.inject.multibindings.MapBinderBinding)10