Search in sources :

Example 6 with Scope

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

the class OverrideModuleTest method testOverrideScopeAnnotation.

public void testOverrideScopeAnnotation() {
    final Scope scope = new Scope() {

        public <T> Provider<T> scope(Key<T> key, Provider<T> unscoped) {
            throw new AssertionError("Should not be called");
        }
    };
    final SingleUseScope replacementScope = new SingleUseScope();
    Module original = new AbstractModule() {

        @Override
        protected void configure() {
            bindScope(TestScopeAnnotation.class, scope);
            bind(Date.class).in(TestScopeAnnotation.class);
        }
    };
    Module replacements = new AbstractModule() {

        @Override
        protected void configure() {
            bindScope(TestScopeAnnotation.class, replacementScope);
        }
    };
    Injector injector = createInjector(Modules.override(original).with(replacements));
    injector.getInstance(Date.class);
    assertTrue(replacementScope.used);
}
Also used : Scope(com.google.inject.Scope) Guice.createInjector(com.google.inject.Guice.createInjector) Injector(com.google.inject.Injector) Module(com.google.inject.Module) PrivateModule(com.google.inject.PrivateModule) AbstractModule(com.google.inject.AbstractModule) Key(com.google.inject.Key) Date(java.util.Date) Provider(com.google.inject.Provider) AbstractModule(com.google.inject.AbstractModule)

Example 7 with Scope

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

the class OverrideModuleTest method testFailsIfOverridenScopeInstanceHasBeenUsed.

public void testFailsIfOverridenScopeInstanceHasBeenUsed() {
    final Scope scope = new Scope() {

        public <T> Provider<T> scope(Key<T> key, Provider<T> unscoped) {
            return unscoped;
        }

        @Override
        public String toString() {
            return "ORIGINAL SCOPE";
        }
    };
    final Module original = new AbstractModule() {

        @Override
        protected void configure() {
            bindScope(TestScopeAnnotation.class, scope);
            bind(Date.class).in(scope);
            bind(String.class).in(scope);
        }
    };
    Module originalWrapper = new AbstractModule() {

        @Override
        protected void configure() {
            install(original);
        }
    };
    Module replacements = new AbstractModule() {

        @Override
        protected void configure() {
            bindScope(TestScopeAnnotation.class, new SingleUseScope());
        }
    };
    try {
        createInjector(Modules.override(originalWrapper).with(replacements));
        fail("Exception expected");
    } catch (CreationException e) {
        assertContains(e.getMessage(), "1) The scope for @TestScopeAnnotation is bound directly and cannot be overridden.", "original binding at " + original.getClass().getName() + ".configure(", asModuleChain(originalWrapper.getClass(), original.getClass()), "bound directly at " + original.getClass().getName() + ".configure(", asModuleChain(originalWrapper.getClass(), original.getClass()), "bound directly at " + original.getClass().getName() + ".configure(", asModuleChain(originalWrapper.getClass(), original.getClass()), "at ", replacements.getClass().getName() + ".configure(", asModuleChain(Modules.OverrideModule.class, replacements.getClass()));
    }
}
Also used : Scope(com.google.inject.Scope) Modules(com.google.inject.util.Modules) CreationException(com.google.inject.CreationException) Module(com.google.inject.Module) PrivateModule(com.google.inject.PrivateModule) AbstractModule(com.google.inject.AbstractModule) Key(com.google.inject.Key) Date(java.util.Date) Provider(com.google.inject.Provider) AbstractModule(com.google.inject.AbstractModule)

Example 8 with Scope

use of com.google.inject.Scope in project camel by apache.

the class Injectors method getScopeAnnotation.

/**
     * Returns the scope annotation for the given binding or null if there is no
     * scope
     */
public static Class<? extends Annotation> getScopeAnnotation(Binding<?> binding) {
    Class<? extends Annotation> scopeAnnotation = null;
    if (binding instanceof BindingImpl) {
        BindingImpl bindingImpl = (BindingImpl) binding;
        Scoping scoping = bindingImpl.getScoping();
        if (scoping != null) {
            scopeAnnotation = scoping.getScopeAnnotation();
            // TODO not sure why we need this hack???
            if (scopeAnnotation == null) {
                Scope scope = scoping.getScopeInstance();
                if (scope instanceof HasScopeAnnotation) {
                    HasScopeAnnotation hasScopeAnnotation = (HasScopeAnnotation) scope;
                    scopeAnnotation = hasScopeAnnotation.getScopeAnnotation();
                }
                if (scopeAnnotation == null && (scoping == Scoping.EAGER_SINGLETON || scoping == Scoping.SINGLETON_ANNOTATION || scoping == Scoping.SINGLETON_INSTANCE)) {
                    scopeAnnotation = Singleton.class;
                }
            }
        }
    }
    return scopeAnnotation;
}
Also used : BindingImpl(com.google.inject.internal.BindingImpl) HasScopeAnnotation(org.apache.camel.guice.support.HasScopeAnnotation) Scoping(com.google.inject.internal.Scoping) Scope(com.google.inject.Scope)

Example 9 with Scope

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

the class Scoping method scope.

/** Scopes an internal factory. */
static <T> InternalFactory<? extends T> scope(Key<T> key, InjectorImpl injector, InternalFactory<? extends T> creator, Object source, Scoping scoping) {
    if (scoping.isNoScope()) {
        return creator;
    }
    Scope scope = scoping.getScopeInstance();
    // NOTE: SingletonScope relies on the fact that we are passing a
    // ProviderToInternalFactoryAdapter here.  If you change the type make sure to update
    // SingletonScope as well.
    Provider<T> scoped = scope.scope(key, new ProviderToInternalFactoryAdapter<T>(injector, creator));
    return new InternalFactoryToProviderAdapter<T>(scoped, source);
}
Also used : Scope(com.google.inject.Scope)

Example 10 with Scope

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

Scope (com.google.inject.Scope)12 Provider (com.google.inject.Provider)7 AbstractModule (com.google.inject.AbstractModule)6 Key (com.google.inject.Key)6 Injector (com.google.inject.Injector)4 Module (com.google.inject.Module)4 PrivateModule (com.google.inject.PrivateModule)4 Date (java.util.Date)4 ImmutableList (com.google.common.collect.ImmutableList)2 CreationException (com.google.inject.CreationException)2 Guice.createInjector (com.google.inject.Guice.createInjector)2 OutOfScopeException (com.google.inject.OutOfScopeException)2 ProvisionException (com.google.inject.ProvisionException)2 ScopeBinding (com.google.inject.spi.ScopeBinding)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Binding (com.google.inject.Binding)1 BindingImpl (com.google.inject.internal.BindingImpl)1 ProviderMethod (com.google.inject.internal.ProviderMethod)1 Scoping (com.google.inject.internal.Scoping)1