Java 类com.google.inject.Scope 实例源码

项目:guiceberry    文件:ModuleWriter.java   
protected void applyScoping(Binding<?> binding, final ScopedBindingBuilder scopedBindingBuilder) {
  binding.acceptScopingVisitor(new BindingScopingVisitor<Void>() {
    public Void visitEagerSingleton() {
      if (scopedBindingBuilder != null) {
        scopedBindingBuilder.asEagerSingleton();
      }
      return null;
    }

    public Void visitScope(Scope scope) {
      scopedBindingBuilder.in(scope);
      return null;
    }

    public Void visitScopeAnnotation(Class<? extends Annotation> scopeAnnotation) {
      scopedBindingBuilder.in(scopeAnnotation);
      return null;
    }

    public Void visitNoScoping() {
      // do nothing
      return null;
    }
  });
}
项目:guiceberry    文件:ModuleWriter.java   
protected void applyScoping(Binding<?> binding, final ScopedBindingBuilder scopedBindingBuilder) {
  binding.acceptScopingVisitor(new BindingScopingVisitor<Void>() {
    public Void visitEagerSingleton() {
      if (scopedBindingBuilder != null) {
        scopedBindingBuilder.asEagerSingleton();
      }
      return null;
    }

    public Void visitScope(Scope scope) {
      scopedBindingBuilder.in(scope);
      return null;
    }

    public Void visitScopeAnnotation(Class<? extends Annotation> scopeAnnotation) {
      scopedBindingBuilder.in(scopeAnnotation);
      return null;
    }

    public Void visitNoScoping() {
      // do nothing
      return null;
    }
  });
}
项目:deguicifier    文件:Generators.java   
private static String scoped(final Binding<?> binding, final String statement) {
  return binding.acceptScopingVisitor(new BindingScopingVisitor<String>() {
    @Override
    public String visitEagerSingleton() {
      if (binding instanceof InstanceBinding<?>) {
        return statement;
      }
      throw new DeguicifierException();
    }

    @Override
    public String visitScope(Scope scope) {
      return scopeFieldName(scope) + ".scope(null, " + guicify(statement) + ")";
    }

    @Override
    public String visitScopeAnnotation(Class<? extends Annotation> scopeAnnotation) {
      throw new DeguicifierException();
    }

    @Override
    public String visitNoScoping() {
      return statement;
    }
  });
}
项目:guice-old    文件:ScopeBindingProcessor.java   
@Override public Boolean visit(ScopeBinding command) {
  Scope scope = command.getScope();
  Class<? extends Annotation> annotationType = command.getAnnotationType();

  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(checkNotNull(annotationType, "annotation type"));
  if (existing != null) {
    errors.duplicateScopes(existing, annotationType, scope);
  } else {
    checkNotNull(scope, "scope");
    injector.state.putScopeBinding(annotationType, command);
  }

  return true;
}
项目:guice-old    文件:Scoping.java   
public static Scoping forInstance(final Scope scope) {
  if (scope == Scopes.SINGLETON) {
    return SINGLETON_INSTANCE;
  }

  return new Scoping() {
    @Override public <V> V acceptVisitor(BindingScopingVisitor<V> visitor) {
      return visitor.visitScope(scope);
    }

    @Override public Scope getScopeInstance() {
      return scope;
    }

    @Override public String toString() {
      return scope.toString();
    }

    @Override public void applyTo(ScopedBindingBuilder scopedBindingBuilder) {
      scopedBindingBuilder.in(scope);
    }
  };
}
项目:guice-old    文件:OverrideModuleTest.java   
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);
}
项目:google-guice    文件:ScopeBindingProcessor.java   
@Override public Boolean visit(ScopeBinding command) {
  Scope scope = command.getScope();
  Class<? extends Annotation> annotationType = command.getAnnotationType();

  if (!Annotations.isScopeAnnotation(annotationType)) {
    errors.withSource(annotationType).missingScopeAnnotation();
    // Go ahead and bind anyway so we don't get collateral errors.
  }

  if (!Annotations.isRetainedAtRuntime(annotationType)) {
    errors.withSource(annotationType)
        .missingRuntimeRetention(command.getSource());
    // Go ahead and bind anyway so we don't get collateral errors.
  }

  Scope existing = injector.state.getScope(checkNotNull(annotationType, "annotation type"));
  if (existing != null) {
    errors.duplicateScopes(existing, annotationType, scope);
  } else {
    injector.state.putAnnotation(annotationType, checkNotNull(scope, "scope"));
  }

  return true;
}
项目:google-guice    文件:Scoping.java   
public static Scoping forInstance(final Scope scope) {
  if (scope == Scopes.SINGLETON) {
    return SINGLETON_INSTANCE;
  }

  return new Scoping() {
    public <V> V acceptVisitor(BindingScopingVisitor<V> visitor) {
      return visitor.visitScope(scope);
    }

    @Override public Scope getScopeInstance() {
      return scope;
    }

    @Override public String toString() {
      return scope.toString();
    }

    public void applyTo(ScopedBindingBuilder scopedBindingBuilder) {
      scopedBindingBuilder.in(scope);
    }
  };
}
项目:parabuild-ci    文件:OutOfScopeException.java   
public OutOfScopeException(Scope scope, Key<?> key, Throwable cause) 
{
    super(String.format(
        "Not in scope %s for key %s: caused by %s",
        scope, key, cause
    ), cause);
}
项目:dwr    文件:OutOfScopeException.java   
public OutOfScopeException(Scope scope, Key<?> key, Throwable cause)
{
    super(String.format(
        "Not in scope %s for key %s: caused by %s",
        scope, key, cause
    ), cause);
}
项目:wava    文件:Scopes.java   
@SuppressWarnings({"unchecked"})
public static void bindScopeInstance(Binder binder, Scope scope, Class<? extends Annotation> annotationType)
{
    requireNonNull(scope);
    binder.bindScope(annotationType, scope);
    binder.bind((Class) scope.getClass()).toInstance(scope);
}
项目:Camel    文件:Injectors.java   
/**
 * 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;
}
项目:gerrit    文件:RequestScopePropagator.java   
protected RequestScopePropagator(
    Scope scope,
    ThreadLocalRequestContext local,
    Provider<RequestScopedReviewDbProvider> dbProviderProvider) {
  this.scope = scope;
  this.local = local;
  this.dbProviderProvider = dbProviderProvider;
}
项目:gerrit    文件:ThreadLocalRequestScopePropagator.java   
protected ThreadLocalRequestScopePropagator(
    Scope scope,
    ThreadLocal<C> threadLocal,
    ThreadLocalRequestContext local,
    Provider<RequestScopedReviewDbProvider> dbProviderProvider) {
  super(scope, local, dbProviderProvider);
  this.threadLocal = threadLocal;
}
项目:dropwizardry    文件:GuiceContainer.java   
@Override
public Map<Scope, ComponentScope> createScopeMap() {
    Map<Scope, ComponentScope> m = super.createScopeMap();

    m.put(ServletScopes.REQUEST, ComponentScope.PerRequest);
    return m;
}
项目:deguicifier    文件:Generators.java   
private static String scopeInstance(Scope scope) {
  if (scope == Scopes.SINGLETON) {
    return Scopes.class.getCanonicalName() + ".SINGLETON";
  } else {
    return "new " + scope.getClass().getCanonicalName() + "()";
  }
}
项目:guice    文件:CheckedProviderTest.java   
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) {
    Message message = Iterables.getOnlyElement(pe.getErrorMessages());
    assertEquals(
        "Error in custom provider, com.google.inject.OutOfScopeException: failure: "
            + Key.get(Unscoped1.class),
        message.getMessage());
  }
}
项目:guice    文件:ScopeBindingProcessor.java   
@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;
}
项目:guice    文件:InheritingState.java   
@Override
public Map<Class<? extends Annotation>, Scope> getScopes() {
  ImmutableMap.Builder<Class<? extends Annotation>, Scope> builder = ImmutableMap.builder();
  for (Map.Entry<Class<? extends Annotation>, ScopeBinding> entry : scopes.entrySet()) {
    builder.put(entry.getKey(), entry.getValue().getScope());
  }
  return builder.build();
}
项目:guice    文件:Scoping.java   
public static Scoping forInstance(final Scope scope) {
  if (scope == Scopes.SINGLETON) {
    return SINGLETON_INSTANCE;
  } else if (scope == Scopes.NO_SCOPE) {
    return EXPLICITLY_UNSCOPED;
  }

  return new Scoping() {
    @Override
    public <V> V acceptVisitor(BindingScopingVisitor<V> visitor) {
      return visitor.visitScope(scope);
    }

    @Override
    public Scope getScopeInstance() {
      return scope;
    }

    @Override
    public String toString() {
      return scope.toString();
    }

    @Override
    public void applyTo(ScopedBindingBuilder scopedBindingBuilder) {
      scopedBindingBuilder.in(scope);
    }
  };
}
项目:guice    文件:Modules.java   
private Scope getScopeInstanceOrNull(Binding<?> binding) {
  return binding.acceptScopingVisitor(
      new DefaultBindingScopingVisitor<Scope>() {
        @Override
        public Scope visitScope(Scope scope) {
          return scope;
        }
      });
}
项目:guice    文件:OverrideModuleTest.java   
public void testOverrideScopeAnnotation() {
  final Scope scope =
      new Scope() {
        @Override
        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);
}
项目:papaya    文件:GuiceDependencyGraph.java   
public static ServiceDependencyManager buildServiceDependencyManager(
        final Injector injector,
        Iterable<Key<? extends BaseService<?>>> serviceKeys
) {
  final Multimap<ResolvedBinding<BaseService<?>>, ResolvedBinding<? extends BaseService<?>>> graph = computeInterdependencies(
          serviceKeys,
          injector);
  for (ResolvedBinding<? extends BaseService<?>> binding : graph.values()) {
    checkArgument(binding.getBinding().acceptScopingVisitor(new BindingScopingVisitor<Boolean>() {
      public Boolean visitEagerSingleton() { return true; }

      public Boolean visitScope(Scope scope) { return scope == Scopes.SINGLETON; }

      public Boolean visitScopeAnnotation(Class<? extends Annotation> scopeAnnotation) {
        return scopeAnnotation.equals(Singleton.class);
      }

      public Boolean visitNoScoping() { return false; }
    }), "Managed service must be bound as Singleton", binding);
  }

  return ServiceDependencyManager.buildServicesWithDependencies(
          ResolvedBinding.<BaseService<?>>resolver(injector)
                         .andThen(ResolvedBinding::get)
                         .transform(serviceKeys)
                         .toList(),
          Functional.funPairs(graph.entries())
                  .mapKeys(ResolvedBinding::get)
                  .mapValues((F<ResolvedBinding<? extends BaseService<?>>, BaseService<?>>) binding -> binding.get())
                  .toList()
  );
}
项目:guice-old    文件:CheckedProviderTest.java   
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());
  }
}
项目:guice-old    文件:InheritingState.java   
public Map<Class<? extends Annotation>, Scope> getScopes() {
  ImmutableMap.Builder<Class<? extends Annotation>, Scope> builder = ImmutableMap.builder();
  for (Map.Entry<Class<? extends Annotation>, ScopeBinding> entry : scopes.entrySet()) {
    builder.put(entry.getKey(), entry.getValue().getScope());
  }
  return builder.build();
}
项目:guice-old    文件:Modules.java   
private Scope getScopeInstanceOrNull(Binding<?> binding) {
  return binding.acceptScopingVisitor(new DefaultBindingScopingVisitor<Scope>() {
    @Override public Scope visitScope(Scope scope) {
      return scope;
    }
  });
}
项目:jfunk    文件:JFunkBaseModule.java   
private <T extends Scope> void bindScope(final Class<T> scopeClass, final T scope,
        final Class<? extends Annotation> scopeAnnotation) {
    // We need a custom thread scope for things related to test runs, because each test runs in
    // its own thread. This gives us thread-local Guice singletons.
    bindScope(scopeAnnotation, scope);

    // We also need to get a hold of the ThreadScope instance via Guice in order to be able to
    // call its cleanUp method after a thread is done. We need to do a clean-up in order to
    // avoid memory leaks.
    bind(scopeClass).toInstance(scope);
}
项目:google-guice    文件:CheckedProviderTest.java   
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());
  }
}
项目:google-guice    文件:Scoping.java   
/**
 * Replaces annotation scopes with instance scopes using the Injector's annotation-to-instance
 * map. If the scope annotation has no corresponding instance, an error will be added and unscoped
 * will be retuned.
 */
static Scoping makeInjectable(Scoping scoping, InjectorImpl injector, Errors errors) {
  Class<? extends Annotation> scopeAnnotation = scoping.getScopeAnnotation();
  if (scopeAnnotation == null) {
    return scoping;
  }

  Scope scope = injector.state.getScope(scopeAnnotation);
  if (scope != null) {
    return forInstance(scope);
  }

  errors.scopeNotFound(scopeAnnotation);
  return UNSCOPED;
}
项目:beadledom    文件:DelegatingInjector.java   
@Override
public Map<Class<? extends Annotation>, Scope> getScopeBindings() {
  return injector.getScopeBindings();
}
项目:ProjectAres    文件:AbstractBindingBuilder.java   
@Override
public void in(Scope scope) {
    applyScoping(Scoping.forInstance(scope));
}
项目:violet    文件:ForwardingBinder.java   
@Override
default void bindScope(final Class<? extends Annotation> annotationType, final Scope scope) {
  this.binder().bindScope(annotationType, scope);
}
项目:violet    文件:ForwardingLinkedBindingBuilder.java   
@Override
default void in(final Scope scope) {
  this.builder().in(scope);
}
项目:wava    文件:ScopeListeners.java   
@Override
public void onEnter(Class<? extends Scope> scopeClass)
{
}
项目:wava    文件:ScopeListeners.java   
@Override
public void onExit(Class<? extends Scope> scopeClass)
{
}
项目:wava    文件:ScopeListeners.java   
@Override
public void onEnter(Class<? extends Scope> scopeClass)
{
    injector.getInstance(key);
}
项目:wava    文件:ScopeListeners.java   
public static void bindScopeListeners(Binder binder, Class<? extends Scope> scopeCls)
{
    binder.bind(Key.get(Types.newParameterizedType(ScopeListeners.class, scopeCls))).asEagerSingleton();
}
项目:guice-scoped-proxy-extension    文件:ScopedProxyBinder.java   
@Override
public void in(Scope scope) {
    this.bindRewritten().in(scope);
}
项目:salta    文件:ScopedBindingBuilderImpl.java   
@Override
public void in(Scope scope) {
    delegate.in(new ScopeImpl(new GuiceScopeAdapter(scope)));
}
项目:salta    文件:BinderImpl.java   
@Override
public void bindScope(Class<? extends Annotation> annotationType,
        Scope scope) {
    delegate.bindScope(annotationType,
            new ScopeImpl(new GuiceScopeAdapter(scope)));
}