public class Key<T>
extends java.lang.Object
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.
Modifier and Type | Class and Description |
---|---|
(package private) static class |
Key.AnnotationInstanceStrategy |
(package private) static interface |
Key.AnnotationStrategy |
(package private) static class |
Key.AnnotationTypeStrategy |
(package private) static class |
Key.NullAnnotationStrategy |
Modifier and Type | Field and Description |
---|---|
private Key.AnnotationStrategy |
annotationStrategy |
private int |
hashCode |
private java.lang.String |
toString |
private TypeLiteral<T> |
typeLiteral |
Modifier | Constructor and Description |
---|---|
protected |
Key()
Constructs a new key.
|
protected |
Key(java.lang.annotation.Annotation annotation)
Constructs a new key.
|
protected |
Key(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
Constructs a new key.
|
private |
Key(java.lang.reflect.Type type,
Key.AnnotationStrategy annotationStrategy)
Unsafe.
|
private |
Key(TypeLiteral<T> typeLiteral,
Key.AnnotationStrategy annotationStrategy)
Constructs a key from a manually specified type.
|
Modifier and Type | Method and Description |
---|---|
private int |
computeHashCode()
Computes the hash code for this key.
|
private static void |
ensureIsBindingAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType) |
private static void |
ensureRetainedAtRuntime(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType) |
boolean |
equals(java.lang.Object o) |
static <T> Key<T> |
get(java.lang.Class<T> type)
Gets a key for an injection type.
|
static <T> Key<T> |
get(java.lang.Class<T> type,
java.lang.annotation.Annotation annotation)
Gets a key for an injection type and an annotation.
|
static <T> Key<T> |
get(java.lang.Class<T> type,
java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
Gets a key for an injection type and an annotation type.
|
(package private) static <T> Key<T> |
get(java.lang.Class<T> type,
Key.AnnotationStrategy annotationStrategy)
Gets a key for an injection type and an annotation strategy.
|
static Key<?> |
get(java.lang.reflect.Type type)
Gets a key for an injection type.
|
static Key<?> |
get(java.lang.reflect.Type type,
java.lang.annotation.Annotation annotation)
Gets a key for an injection type and an annotation.
|
static Key<?> |
get(java.lang.reflect.Type type,
java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
Gets a key for an injection type and an annotation type.
|
static <T> Key<T> |
get(TypeLiteral<T> typeLiteral)
Gets a key for an injection type.
|
static <T> Key<T> |
get(TypeLiteral<T> typeLiteral,
java.lang.annotation.Annotation annotation)
Gets a key for an injection type and an annotation.
|
static <T> Key<T> |
get(TypeLiteral<T> typeLiteral,
java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
Gets a key for an injection type and an annotation type.
|
java.lang.annotation.Annotation |
getAnnotation()
Gets the annotation.
|
(package private) java.lang.String |
getAnnotationName() |
java.lang.Class<? extends java.lang.annotation.Annotation> |
getAnnotationType()
Gets the annotation type.
|
(package private) java.lang.Class<? super T> |
getRawType() |
TypeLiteral<T> |
getTypeLiteral()
Gets the key type.
|
(package private) boolean |
hasAnnotationType() |
boolean |
hasAttributes()
Returns true if this key has annotation attributes.
|
int |
hashCode() |
<T> Key<T> |
ofType(java.lang.Class<T> type)
Returns a new key of the specified type with the same annotation as this
key.
|
Key<?> |
ofType(java.lang.reflect.Type type)
Returns a new key of the specified type with the same annotation as this
key.
|
<T> Key<T> |
ofType(TypeLiteral<T> type)
Returns a new key of the specified type with the same annotation as this
key.
|
(package private) Key<Provider<T>> |
providerKey()
Gets the key of this key's provider.
|
(package private) static Key.AnnotationStrategy |
strategyFor(java.lang.annotation.Annotation annotation)
Gets the strategy for an annotation.
|
(package private) static Key.AnnotationStrategy |
strategyFor(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
Gets the strategy for an annotation type.
|
java.lang.String |
toString() |
Key<T> |
withoutAttributes()
Returns this key without annotation attributes, i.e.
|
private final Key.AnnotationStrategy annotationStrategy
private final TypeLiteral<T> typeLiteral
private final int hashCode
private java.lang.String toString
protected Key(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
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) {}
.
protected Key(java.lang.annotation.Annotation annotation)
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()) {}
.
protected Key()
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>() {}
.
private Key(java.lang.reflect.Type type, Key.AnnotationStrategy annotationStrategy)
private Key(TypeLiteral<T> typeLiteral, Key.AnnotationStrategy annotationStrategy)
private int computeHashCode()
public final TypeLiteral<T> getTypeLiteral()
public final java.lang.Class<? extends java.lang.annotation.Annotation> getAnnotationType()
public final java.lang.annotation.Annotation getAnnotation()
boolean hasAnnotationType()
java.lang.String getAnnotationName()
java.lang.Class<? super T> getRawType()
public final boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public final int hashCode()
hashCode
in class java.lang.Object
public final java.lang.String toString()
toString
in class java.lang.Object
static <T> Key<T> get(java.lang.Class<T> type, Key.AnnotationStrategy annotationStrategy)
public static <T> Key<T> get(java.lang.Class<T> type)
public static <T> Key<T> get(java.lang.Class<T> type, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
public static <T> Key<T> get(java.lang.Class<T> type, java.lang.annotation.Annotation annotation)
public static Key<?> get(java.lang.reflect.Type type)
public static Key<?> get(java.lang.reflect.Type type, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
public static Key<?> get(java.lang.reflect.Type type, java.lang.annotation.Annotation annotation)
public static <T> Key<T> get(TypeLiteral<T> typeLiteral)
public static <T> Key<T> get(TypeLiteral<T> typeLiteral, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
public static <T> Key<T> get(TypeLiteral<T> typeLiteral, java.lang.annotation.Annotation annotation)
public <T> Key<T> ofType(java.lang.Class<T> type)
public Key<?> ofType(java.lang.reflect.Type type)
public <T> Key<T> ofType(TypeLiteral<T> type)
public boolean hasAttributes()
public Key<T> withoutAttributes()
static Key.AnnotationStrategy strategyFor(java.lang.annotation.Annotation annotation)
static Key.AnnotationStrategy strategyFor(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
private static void ensureRetainedAtRuntime(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
private static void ensureIsBindingAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)