Class Key<T>
Injector
. A Guice key consists of an injection type and an optional annotation.
For example, Key.get(Service.class, Transactional.class)
will match:
@Inject public void setService(@Transactional Service service) { ... }
Key
supports generic types via subclassing just like TypeLiteral
.
Keys do not differentiate between primitive types (int, char, etc.) and their corresponding wrapper types (Integer, Character, etc.). Primitive types will be replaced with their wrapper types when keys are created.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static class
(package private) static interface
(package private) static class
(package private) static enum
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Key.AnnotationStrategy
private final int
private String
private final TypeLiteral
<T> -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
Key()
Constructs a new key.private
Key
(TypeLiteral<T> typeLiteral, Key.AnnotationStrategy annotationStrategy) Constructs a key from a manually specified type.protected
Key
(Annotation annotation) Constructs a new key.protected
Key
(Class<? extends Annotation> annotationType) Constructs a new key.private
Key
(Type type, Key.AnnotationStrategy annotationStrategy) Unsafe. -
Method Summary
Modifier and TypeMethodDescriptionprivate int
Computes the hash code for this key.private static void
ensureIsBindingAnnotation
(Class<? extends Annotation> annotationType) private static void
ensureRetainedAtRuntime
(Class<? extends Annotation> annotationType) final boolean
static <T> Key
<T> get
(TypeLiteral<T> typeLiteral) Gets a key for an injection type.static <T> Key
<T> get
(TypeLiteral<T> typeLiteral, Annotation annotation) Gets a key for an injection type and an annotation.static <T> Key
<T> get
(TypeLiteral<T> typeLiteral, Class<? extends Annotation> annotationType) Gets a key for an injection type and an annotation type.static <T> Key
<T> Gets a key for an injection type.(package private) static <T> Key
<T> get
(Class<T> type, Key.AnnotationStrategy annotationStrategy) Gets a key for an injection type and an annotation strategy.static <T> Key
<T> get
(Class<T> type, Annotation annotation) Gets a key for an injection type and an annotation.static <T> Key
<T> get
(Class<T> type, Class<? extends Annotation> annotationType) Gets a key for an injection type and an annotation type.static Key
<?> Gets a key for an injection type.static Key
<?> get
(Type type, Annotation annotation) Gets a key for an injection type and an annotation.static Key
<?> get
(Type type, Class<? extends Annotation> annotationType) Gets a key for an injection type and an annotation type.final Annotation
Gets the annotation instance if available.(package private) String
final Class
<? extends Annotation> Gets the annotation type.final TypeLiteral
<T> Gets the key type.(package private) boolean
boolean
Returns true if this key has annotation attributes.final int
hashCode()
<U> Key
<U> ofType
(TypeLiteral<U> type) Returns a new key of the specified type with the same annotation as this key.<U> Key
<U> Returns a new key of the specified type with the same annotation as this key.Key
<?> Returns a new key of the specified type with the same annotation as this key.Gets the key of this key's provider.(package private) static Key.AnnotationStrategy
strategyFor
(Annotation annotation) Gets the strategy for an annotation.(package private) static Key.AnnotationStrategy
strategyFor
(Class<? extends Annotation> annotationType) Gets the strategy for an annotation type.final String
toString()
withAnnotation
(Annotation annotation) Returns a new key of the same type with the specified annotation.withAnnotation
(Class<? extends Annotation> annotationType) Returns a new key of the same type with the specified annotation.Returns this key without annotation attributes, i.e.
-
Field Details
-
annotationStrategy
-
typeLiteral
-
hashCode
private final int hashCode -
toString
-
-
Constructor Details
-
Key
Constructs a new key. Derives the type from this class's type parameter.Clients create an empty anonymous subclass. Doing so embeds the type parameter in the anonymous class's type hierarchy so we can reconstitute it at runtime despite erasure.
Example usage for a binding of type
Foo
annotated with@Bar
:new Key<Foo>(Bar.class) {}
. -
Key
Constructs a new key. Derives the type from this class's type parameter.Clients create an empty anonymous subclass. Doing so embeds the type parameter in the anonymous class's type hierarchy so we can reconstitute it at runtime despite erasure.
Example usage for a binding of type
Foo
annotated with@Bar
:new Key<Foo>(new Bar()) {}
. -
Key
protected Key()Constructs a new key. Derives the type from this class's type parameter.Clients create an empty anonymous subclass. Doing so embeds the type parameter in the anonymous class's type hierarchy so we can reconstitute it at runtime despite erasure.
Example usage for a binding of type
Foo
:new Key<Foo>() {}
. -
Key
Unsafe. Constructs a key from a manually specified type. -
Key
Constructs a key from a manually specified type.
-
-
Method Details
-
computeHashCode
private int computeHashCode()Computes the hash code for this key. -
getTypeLiteral
Gets the key type. -
getAnnotationType
Gets the annotation type. Will benull
if this key lacks an annotation. -
getAnnotation
Gets the annotation instance if available. Will benull
if this key lacks an annotation or the key was constructed with aClass<Annotation>
.Warning: this can return null even if this key is annotated. To check whether a
Key
has an annotation usehasAnnotationType()
instead. -
hasAnnotationType
boolean hasAnnotationType() -
getAnnotationName
String getAnnotationName() -
getRawType
-
providerKey
Gets the key of this key's provider. -
equals
-
hashCode
public final int hashCode() -
toString
-
get
Gets a key for an injection type and an annotation strategy. -
get
Gets a key for an injection type. -
get
Gets a key for an injection type and an annotation type. -
get
Gets a key for an injection type and an annotation. -
get
Gets a key for an injection type. -
get
Gets a key for an injection type and an annotation type. -
get
Gets a key for an injection type and an annotation. -
get
Gets a key for an injection type. -
get
public static <T> Key<T> get(TypeLiteral<T> typeLiteral, Class<? extends Annotation> annotationType) Gets a key for an injection type and an annotation type. -
get
Gets a key for an injection type and an annotation. -
ofType
Returns a new key of the specified type with the same annotation as this key.- Since:
- 3.0
-
ofType
Returns a new key of the specified type with the same annotation as this key.- Since:
- 3.0
-
ofType
Returns a new key of the specified type with the same annotation as this key.- Since:
- 3.0
-
withAnnotation
Returns a new key of the same type with the specified annotation.This is equivalent to
Key.get(key.getTypeLiteral(), annotation)
but may be more convenient to use in certain cases.- Since:
- 5.0
-
withAnnotation
Returns a new key of the same type with the specified annotation.This is equivalent to
Key.get(key.getTypeLiteral(), annotation)
but may be more convenient to use in certain cases.- Since:
- 5.0
-
hasAttributes
public boolean hasAttributes()Returns true if this key has annotation attributes.- Since:
- 3.0
-
withoutAttributes
Returns this key without annotation attributes, i.e. with only the annotation type.- Since:
- 3.0
-
strategyFor
Gets the strategy for an annotation. -
strategyFor
Gets the strategy for an annotation type. -
ensureRetainedAtRuntime
-
ensureIsBindingAnnotation
-