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

Class Key

core/src/com/google/inject/Key.java:51–519  ·  view source on GitHub ↗

Guice uses Key objects to identify a dependency that can be resolved by the Guice Injector. A Guice key consists of an injection type and an optional annotation. For example, Key.get(Service.class, Transactional.class) will match: {@literal @}Inject public void setServ

Source from the content-addressed store, hash-verified

49 * @author crazybob@google.com (Bob Lee)
50 */
51@CheckReturnValue
52public class Key<T> {
53
54 private final AnnotationStrategy annotationStrategy;
55
56 private final TypeLiteral<T> typeLiteral;
57 private final int hashCode;
58 // This field is updated using the 'Data-Race-Ful' lazy intialization pattern
59 // See http://jeremymanson.blogspot.com/2008/12/benign-data-races-in-java.html for a detailed
60 // explanation.
61 private String toString;
62
63 /**
64 * Constructs a new key. Derives the type from this class's type parameter.
65 *
66 * <p>Clients create an empty anonymous subclass. Doing so embeds the type parameter in the
67 * anonymous class's type hierarchy so we can reconstitute it at runtime despite erasure.
68 *
69 * <p>Example usage for a binding of type {@code Foo} annotated with {@code @Bar}:
70 *
71 * <p>{@code new Key<Foo>(Bar.class) {}}.
72 */
73 @SuppressWarnings("unchecked")
74 protected Key(Class<? extends Annotation> annotationType) {
75 this.annotationStrategy = strategyFor(annotationType);
76 this.typeLiteral =
77 MoreTypes.canonicalizeForKey(
78 (TypeLiteral<T>) TypeLiteral.fromSuperclassTypeParameter(getClass()));
79 this.hashCode = computeHashCode();
80 }
81
82 /**
83 * Constructs a new key. Derives the type from this class's type parameter.
84 *
85 * <p>Clients create an empty anonymous subclass. Doing so embeds the type parameter in the
86 * anonymous class's type hierarchy so we can reconstitute it at runtime despite erasure.
87 *
88 * <p>Example usage for a binding of type {@code Foo} annotated with {@code @Bar}:
89 *
90 * <p>{@code new Key<Foo>(new Bar()) {}}.
91 */
92 @SuppressWarnings("unchecked")
93 protected Key(Annotation annotation) {
94 // no usages, not test-covered
95 this.annotationStrategy = strategyFor(annotation);
96 this.typeLiteral =
97 MoreTypes.canonicalizeForKey(
98 (TypeLiteral<T>) TypeLiteral.fromSuperclassTypeParameter(getClass()));
99 this.hashCode = computeHashCode();
100 }
101
102 /**
103 * Constructs a new key. Derives the type from this class's type parameter.
104 *
105 * <p>Clients create an empty anonymous subclass. Doing so embeds the type parameter in the
106 * anonymous class's type hierarchy so we can reconstitute it at runtime despite erasure.
107 *
108 * <p>Example usage for a binding of type {@code Foo}:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected