Search in sources :

Example 71 with Binding

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

the class ElementsTest method testBindKeysNoAnnotations.

public void testBindKeysNoAnnotations() {
    FailingElementVisitor keyChecker = new FailingElementVisitor() {

        @Override
        public <T> Void visit(Binding<T> command) {
            assertEquals(Key.get(String.class), command.getKey());
            return null;
        }
    };
    checkModule(new AbstractModule() {

        @Override
        protected void configure() {
            bind(String.class).toInstance("A");
            bind(new TypeLiteral<String>() {
            }).toInstance("B");
            bind(Key.get(String.class)).toInstance("C");
        }
    }, keyChecker, keyChecker, keyChecker);
}
Also used : Binding(com.google.inject.Binding) AbstractModule(com.google.inject.AbstractModule)

Example 72 with Binding

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

the class ElementsTest method testBindKeysWithAnnotationInstance.

public void testBindKeysWithAnnotationInstance() {
    FailingElementVisitor annotationChecker = new FailingElementVisitor() {

        @Override
        public <T> Void visit(Binding<T> command) {
            assertEquals(Key.get(String.class, Names.named("a")), command.getKey());
            return null;
        }
    };
    checkModule(new AbstractModule() {

        @Override
        protected void configure() {
            bind(String.class).annotatedWith(Names.named("a")).toInstance("B");
            bind(new TypeLiteral<String>() {
            }).annotatedWith(Names.named("a")).toInstance("C");
        }
    }, annotationChecker, annotationChecker);
}
Also used : Binding(com.google.inject.Binding) TypeLiteral(com.google.inject.TypeLiteral) AbstractModule(com.google.inject.AbstractModule)

Example 73 with Binding

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

the class ModuleRewriterTest method testRewriteBindings.

public void testRewriteBindings() {
    // create a module the binds String.class and CharSequence.class
    Module module = new AbstractModule() {

        @Override
        protected void configure() {
            bind(String.class).toInstance("Pizza");
            bind(CharSequence.class).toInstance("Wine");
        }
    };
    // record the elements from that module
    List<Element> elements = Elements.getElements(module);
    // create a rewriter that rewrites the binding to 'Wine' with a binding to 'Beer'
    List<Element> rewritten = Lists.newArrayList();
    for (Element element : elements) {
        element = element.acceptVisitor(new DefaultElementVisitor<Element>() {

            @Override
            public <T> Element visit(Binding<T> binding) {
                T target = binding.acceptTargetVisitor(Elements.<T>getInstanceVisitor());
                if ("Wine".equals(target)) {
                    return null;
                } else {
                    return binding;
                }
            }
        });
        if (element != null) {
            rewritten.add(element);
        }
    }
    // create a module from the original list of elements and the rewriter
    Module rewrittenModule = Elements.getModule(rewritten);
    // the wine binding is dropped
    Injector injector = Guice.createInjector(rewrittenModule);
    try {
        injector.getInstance(CharSequence.class);
        fail();
    } catch (ConfigurationException expected) {
    }
}
Also used : Binding(com.google.inject.Binding) ConfigurationException(com.google.inject.ConfigurationException) Injector(com.google.inject.Injector) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) AbstractModule(com.google.inject.AbstractModule)

Example 74 with Binding

use of com.google.inject.Binding 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)

Example 75 with Binding

use of com.google.inject.Binding in project ninja by ninjaframework.

the class LifecycleServiceImpl method start.

@Override
public void start() {
    startTime = System.currentTimeMillis();
    log.info("Starting Ninja application...");
    state = State.STARTING;
    // until they are instantiated that LifecycleSupport has an opportunity to register them.
    for (final Binding binding : injector.getBindings().values()) {
        binding.acceptScopingVisitor(new DefaultBindingScopingVisitor() {

            @Override
            public Object visitEagerSingleton() {
                injector.getInstance(binding.getKey());
                return null;
            }

            @Override
            public Object visitScope(Scope scope) {
                if (scope.equals(Scopes.SINGLETON)) {
                    Object target = injector.getInstance(binding.getKey());
                    if (binding instanceof ProviderInstanceBinding) {
                        Provider providerInstance = ((ProviderInstanceBinding) binding).getProviderInstance();
                        if (providerInstance instanceof ProviderMethod) {
                            // @Provides methods don't get picked up by TypeListeners, so we need to manually register them
                            if (lifecycleSupport.hasLifecycleMethod(target.getClass())) {
                                lifecycleSupport.registerLifecycle(target);
                            }
                        }
                    }
                }
                return null;
            }
        });
    }
    lifecycleRegister.start();
    long time = System.currentTimeMillis() - startTime;
    log.info("Ninja application started in {}ms", time);
    state = lifecycleRegister.isStarted() ? State.STARTED : State.STOPPED;
}
Also used : ProviderInstanceBinding(com.google.inject.spi.ProviderInstanceBinding) Binding(com.google.inject.Binding) Scope(com.google.inject.Scope) DefaultBindingScopingVisitor(com.google.inject.spi.DefaultBindingScopingVisitor) ProviderMethod(com.google.inject.internal.ProviderMethod) ProviderInstanceBinding(com.google.inject.spi.ProviderInstanceBinding) Provider(com.google.inject.Provider)

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