Search in sources :

Example 1 with ProviderMethod

use of com.google.inject.internal.ProviderMethod in project google-gin by gwtplus.

the class ProviderMethodBindingTest method createProviderMethodBinding.

private ProviderMethodBinding createProviderMethodBinding(Object instance, Method method) {
    // Ew, but the constructor of ProviderMethod is private, and this is a
    // simple way to create a custom one.
    IMocksControl control = EasyMock.createControl();
    @SuppressWarnings("unchecked") ProviderMethod<Integer> providerMethod = (ProviderMethod<Integer>) control.createMock("providerMethod", ProviderMethod.class);
    EasyMock.expect(providerMethod.getInstance()).andStubReturn(instance);
    EasyMock.expect(providerMethod.getKey()).andStubReturn(Key.get(Integer.class));
    EasyMock.expect(providerMethod.getMethod()).andStubReturn(method);
    control.replay();
    // Note: guiceUtil and methodCallUtil are used in parts of the binding class
    // that we don't test currently, so are set to null.  When tests for
    // getCreationStatements() and getDependencies() are written, concrete
    // values (mocks?) will be required.
    ProviderMethodBinding result = new ProviderMethodBinding(errorManager, null, null, providerMethod, Context.forText("dummy context"));
    control.verify();
    return result;
}
Also used : IMocksControl(org.easymock.IMocksControl) ProviderMethod(com.google.inject.internal.ProviderMethod)

Example 2 with ProviderMethod

use of com.google.inject.internal.ProviderMethod in project roboguice by roboguice.

the class ShortNameFactoryTest method testGetInstanceName_providerMethod.

public void testGetInstanceName_providerMethod() throws Exception {
    final ProviderMethod<?>[] methodHolder = new ProviderMethod[1];
    Injector injector = Guice.createInjector(new ProvidingModule());
    injector.getBinding(Integer.class).acceptTargetVisitor(new DefaultBindingTargetVisitor<Object, Void>() {

        @SuppressWarnings("unchecked")
        @Override
        public Void visit(ProviderInstanceBinding<?> binding) {
            methodHolder[0] = (ProviderMethod) binding.getUserSuppliedProvider();
            return null;
        }
    });
    assertEquals("Method provider should pretty print as the method signature", "#provideInteger(String)", nameFactory.getInstanceName(methodHolder[0]));
}
Also used : Injector(com.google.inject.Injector) ProviderMethod(com.google.inject.internal.ProviderMethod)

Example 3 with ProviderMethod

use of com.google.inject.internal.ProviderMethod in project guice by google.

the class ShortNameFactoryTest method testGetInstanceName_providerMethod.

public void testGetInstanceName_providerMethod() throws Exception {
    final List<ProviderMethod<?>> methodHolder = new ArrayList<>(1);
    Injector injector = Guice.createInjector(new ProvidingModule());
    injector.getBinding(Integer.class).acceptTargetVisitor(new DefaultBindingTargetVisitor<Object, Void>() {

        @Override
        public Void visit(ProviderInstanceBinding<?> binding) {
            methodHolder.add((ProviderMethod) binding.getUserSuppliedProvider());
            return null;
        }
    });
    assertEquals("Method provider should pretty print as the method signature", "#provideInteger(String)", nameFactory.getInstanceName(methodHolder.get(0)));
}
Also used : Injector(com.google.inject.Injector) ArrayList(java.util.ArrayList) ProviderMethod(com.google.inject.internal.ProviderMethod)

Example 4 with ProviderMethod

use of com.google.inject.internal.ProviderMethod in project guice by google.

the class ProviderMethodsTest method testNonModuleProviderMethods.

@Test
public void testNonModuleProviderMethods() {
    final Object methodsObject = new Object() {

        @Provides
        @Named("foo")
        String provideFoo() {
            return "foo-value";
        }
    };
    Module module = new AbstractModule() {

        @Override
        protected void configure() {
            install(ProviderMethodsModule.forObject(methodsObject));
        }
    };
    Injector injector = Guice.createInjector(module);
    Key<String> key = Key.get(String.class, Names.named("foo"));
    assertEquals("foo-value", injector.getInstance(key));
    // Test the provider method object itself. This makes sure getInstance works, since GIN uses it
    List<Element> elements = Elements.getElements(module);
    assertEquals(1, elements.size());
    Element element = elements.get(0);
    assertTrue(element + " instanceof ProviderInstanceBinding", element instanceof ProviderInstanceBinding);
    ProviderInstanceBinding<?> binding = (ProviderInstanceBinding<?>) element;
    javax.inject.Provider<?> provider = binding.getUserSuppliedProvider();
    assertTrue(provider instanceof ProviderMethod);
    assertEquals(methodsObject, ((ProviderMethod) provider).getInstance());
    assertSame(provider, binding.getProviderInstance());
}
Also used : ProviderMethod(com.google.inject.internal.ProviderMethod) AbstractModule(com.google.inject.AbstractModule) Injector(com.google.inject.Injector) Module(com.google.inject.Module) ProviderMethodsModule(com.google.inject.internal.ProviderMethodsModule) AbstractModule(com.google.inject.AbstractModule) Test(org.junit.Test)

Example 5 with ProviderMethod

use of com.google.inject.internal.ProviderMethod 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

ProviderMethod (com.google.inject.internal.ProviderMethod)7 Injector (com.google.inject.Injector)4 AbstractModule (com.google.inject.AbstractModule)2 Module (com.google.inject.Module)2 ProviderMethodsModule (com.google.inject.internal.ProviderMethodsModule)2 GwtDotCreateProvider (com.google.gwt.inject.rebind.adapter.GwtDotCreateProvider)1 Context (com.google.gwt.inject.rebind.binding.Context)1 Binding (com.google.inject.Binding)1 Provider (com.google.inject.Provider)1 Scope (com.google.inject.Scope)1 DefaultBindingScopingVisitor (com.google.inject.spi.DefaultBindingScopingVisitor)1 ProviderInstanceBinding (com.google.inject.spi.ProviderInstanceBinding)1 ArrayList (java.util.ArrayList)1 IMocksControl (org.easymock.IMocksControl)1 Test (org.junit.Test)1