Search in sources :

Example 1 with Scope

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

the class CheckedProviderTest method testProvisionExceptionOnDependenciesOfCxtor.

public void testProvisionExceptionOnDependenciesOfCxtor() throws Exception {
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            ThrowingProviderBinder.create(binder()).bind(RemoteProvider.class, Foo.class).providing(ProvisionExceptionFoo.class);
            bindScope(BadScope.class, new Scope() {

                @Override
                public <T> Provider<T> scope(Key<T> key, Provider<T> unscoped) {
                    return new Provider<T>() {

                        @Override
                        public T get() {
                            throw new OutOfScopeException("failure");
                        }
                    };
                }
            });
        }
    });
    try {
        injector.getInstance(Key.get(remoteProviderOfFoo)).get();
        fail();
    } catch (ProvisionException pe) {
        assertEquals(2, pe.getErrorMessages().size());
        List<Message> messages = Lists.newArrayList(pe.getErrorMessages());
        assertEquals("Error in custom provider, com.google.inject.OutOfScopeException: failure", messages.get(0).getMessage());
        assertEquals("Error in custom provider, com.google.inject.OutOfScopeException: failure", messages.get(1).getMessage());
    }
}
Also used : ProvisionException(com.google.inject.ProvisionException) Scope(com.google.inject.Scope) Injector(com.google.inject.Injector) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) OutOfScopeException(com.google.inject.OutOfScopeException) Key(com.google.inject.Key) AbstractModule(com.google.inject.AbstractModule) Provider(com.google.inject.Provider)

Example 2 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 3 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 4 with Scope

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

the class CheckedProviderTest method testProvisionExceptionOnDependenciesOfCxtor.

public void testProvisionExceptionOnDependenciesOfCxtor() throws Exception {
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            ThrowingProviderBinder.create(binder()).bind(RemoteProvider.class, Foo.class).providing(ProvisionExceptionFoo.class);
            bindScope(BadScope.class, new Scope() {

                @Override
                public <T> Provider<T> scope(final Key<T> key, Provider<T> unscoped) {
                    return new Provider<T>() {

                        @Override
                        public T get() {
                            throw new OutOfScopeException("failure: " + key.toString());
                        }
                    };
                }
            });
        }
    });
    try {
        injector.getInstance(Key.get(remoteProviderOfFoo)).get();
        fail();
    } catch (ProvisionException pe) {
        assertEquals(2, pe.getErrorMessages().size());
        List<Message> messages = Lists.newArrayList(pe.getErrorMessages());
        assertEquals("Error in custom provider, com.google.inject.OutOfScopeException: failure: " + Key.get(Unscoped1.class), messages.get(0).getMessage());
        assertEquals("Error in custom provider, com.google.inject.OutOfScopeException: failure: " + Key.get(Unscoped2.class), messages.get(1).getMessage());
    }
}
Also used : OutOfScopeException(com.google.inject.OutOfScopeException) AbstractModule(com.google.inject.AbstractModule) Provider(com.google.inject.Provider) ProvisionException(com.google.inject.ProvisionException) Scope(com.google.inject.Scope) Injector(com.google.inject.Injector) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Key(com.google.inject.Key)

Example 5 with Scope

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

the class ScopeBindingProcessor method visit.

@Override
public Boolean visit(ScopeBinding command) {
    Scope scope = checkNotNull(command.getScope(), "scope");
    Class<? extends Annotation> annotationType = checkNotNull(command.getAnnotationType(), "annotation type");
    if (!Annotations.isScopeAnnotation(annotationType)) {
        errors.missingScopeAnnotation(annotationType);
    // Go ahead and bind anyway so we don't get collateral errors.
    }
    if (!Annotations.isRetainedAtRuntime(annotationType)) {
        errors.missingRuntimeRetention(annotationType);
    // Go ahead and bind anyway so we don't get collateral errors.
    }
    ScopeBinding existing = injector.state.getScopeBinding(annotationType);
    if (existing != null) {
        if (!scope.equals(existing.getScope())) {
            errors.duplicateScopes(existing, annotationType, scope);
        }
    } else {
        injector.state.putScopeBinding(annotationType, command);
    }
    return true;
}
Also used : Scope(com.google.inject.Scope) ScopeBinding(com.google.inject.spi.ScopeBinding)

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