public class

Key

extends Object
java.lang.Object
   ↳ com.google.inject.Key<T>

Class Overview

Binding key consisting of an injection type and an optional annotation. Matches the type and annotation at a point of injection.

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 correpsonding wrapper types (Integer, Character, etc.). Primitive types will be replaced with their wrapper types when keys are created.

Summary

Protected Constructors
Key()
Constructs a new key.
Key(Class<? extends Annotation> annotationType)
Constructs a new key.
Key(Annotation annotation)
Constructs a new key.
Public Methods
final boolean equals(Object o)
static <T> Key<T> get(TypeLiteral<T> typeLiteral)
Gets a key for an injection type.
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> get(TypeLiteral<T> typeLiteral, Annotation annotation)
Gets a key for an injection type and an annotation.
static <T> Key<T> get(Class<T> type)
Gets a key for an injection type.
static <T> Key<T> get(Class<T> type, Class<? extends Annotation> annotationType)
Gets a key for an injection type and an annotation type.
static <T> Key<T> get(Class<T> type, Annotation annotation)
Gets a key for an injection type and an annotation.
static Key<?> get(Type type)
Gets a key for an injection type.
static Key<?> get(Type type, Class<? extends Annotation> annotationType)
Gets a key for an injection type and an annotation type.
static Key<?> get(Type type, Annotation annotation)
Gets a key for an injection type and an annotation.
final Annotation getAnnotation()
Gets the annotation.
final Class<? extends Annotation> getAnnotationType()
Gets the annotation type.
final TypeLiteral<T> getTypeLiteral()
Gets the key type.
boolean hasAttributes()
Returns true if this key has annotation attributes.
final int hashCode()
<T> Key<T> ofType(TypeLiteral<T> type)
Returns a new key of the specified type with the same annotation as this key.
<T> Key<T> ofType(Class<T> type)
Returns a new key of the specified type with the same annotation as this key.
Key<?> ofType(Type type)
Returns a new key of the specified type with the same annotation as this key.
final String toString()
Key<T> withoutAttributes()
Returns this key without annotation attributes, i.e.
[Expand]
Inherited Methods
From class java.lang.Object

Protected Constructors

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>() {}.

protected Key (Class<? extends Annotation> annotationType)

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) {}.

protected Key (Annotation annotation)

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()) {}.

Public Methods

public final boolean equals (Object o)

public static Key<T> get (TypeLiteral<T> typeLiteral)

Gets a key for an injection type.

public static Key<T> get (TypeLiteral<T> typeLiteral, Class<? extends Annotation> annotationType)

Gets a key for an injection type and an annotation type.

public static Key<T> get (TypeLiteral<T> typeLiteral, Annotation annotation)

Gets a key for an injection type and an annotation.

public static Key<T> get (Class<T> type)

Gets a key for an injection type.

public static Key<T> get (Class<T> type, Class<? extends Annotation> annotationType)

Gets a key for an injection type and an annotation type.

public static Key<T> get (Class<T> type, Annotation annotation)

Gets a key for an injection type and an annotation.

public static Key<?> get (Type type)

Gets a key for an injection type.

public static Key<?> get (Type type, Class<? extends Annotation> annotationType)

Gets a key for an injection type and an annotation type.

public static Key<?> get (Type type, Annotation annotation)

Gets a key for an injection type and an annotation.

public final Annotation getAnnotation ()

Gets the annotation.

public final Class<? extends Annotation> getAnnotationType ()

Gets the annotation type.

public final TypeLiteral<T> getTypeLiteral ()

Gets the key type.

public boolean hasAttributes ()

Returns true if this key has annotation attributes.

public final int hashCode ()

public Key<T> ofType (TypeLiteral<T> type)

Returns a new key of the specified type with the same annotation as this key.

public Key<T> ofType (Class<T> type)

Returns a new key of the specified type with the same annotation as this key.

public Key<?> ofType (Type type)

Returns a new key of the specified type with the same annotation as this key.

public final String toString ()

public Key<T> withoutAttributes ()

Returns this key without annotation attributes, i.e. with only the annotation type.