MCPcopy Create free account
hub / github.com/google/guice / Scopes

Class Scopes

core/src/com/google/inject/Scopes.java:32–202  ·  view source on GitHub ↗

Built-in scope implementations. @author crazybob@google.com (Bob Lee)

Source from the content-addressed store, hash-verified

30 * @author crazybob@google.com (Bob Lee)
31 */
32public class Scopes {
33
34 private Scopes() {}
35
36 /** One instance per {@link Injector}. Also see {@code @}{@link Singleton}. */
37 public static final Scope SINGLETON = new SingletonScope();
38
39 /**
40 * No scope; the same as not applying any scope at all. Each time the Injector obtains an instance
41 * of an object with "no scope", it injects this instance then immediately forgets it. When the
42 * next request for the same binding arrives it will need to obtain the instance over again.
43 *
44 * <p>This exists only in case a class has been annotated with a scope annotation such as {@link
45 * Singleton @Singleton}, and you need to override this to "no scope" in your binding.
46 *
47 * @since 2.0
48 */
49 public static final Scope NO_SCOPE =
50 new Scope() {
51 @Override
52 public <T> Provider<T> scope(Key<T> key, Provider<T> unscoped) {
53 return unscoped;
54 }
55
56 @Override
57 public String toString() {
58 return "Scopes.NO_SCOPE";
59 }
60 };
61
62 private static final BindingScopingVisitor<Boolean> IS_SINGLETON_VISITOR =
63 new BindingScopingVisitor<Boolean>() {
64 @Override
65 public Boolean visitNoScoping() {
66 return false;
67 }
68
69 @Override
70 public Boolean visitScopeAnnotation(Class<? extends Annotation> scopeAnnotation) {
71 return scopeAnnotation == Singleton.class
72 || scopeAnnotation == jakarta.inject.Singleton.class;
73 }
74
75 @Override
76 public Boolean visitScope(Scope scope) {
77 return scope == Scopes.SINGLETON;
78 }
79
80 @Override
81 public Boolean visitEagerSingleton() {
82 return true;
83 }
84 };
85
86 /**
87 * Returns true if {@code binding} is singleton-scoped. If the binding is a {@link
88 * com.google.inject.spi.LinkedKeyBinding linked key binding} and belongs to an injector (ie. it
89 * was retrieved via {@link Injector#getBinding Injector.getBinding()}), then this method will

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected