Class Key<T>

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

public class Key<T> extends Object
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 corresponding wrapper types (Integer, Character, etc.). Primitive types will be replaced with their wrapper types when keys are created.

  • Field Details

    • annotationStrategy

      private final Key.AnnotationStrategy annotationStrategy
    • typeLiteral

      private final TypeLiteral<T> typeLiteral
    • hashCode

      private final int hashCode
    • toString

      private String toString
  • Constructor Details

    • Key

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

    • Key

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

    • 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

      private Key(Type type, Key.AnnotationStrategy annotationStrategy)
      Unsafe. Constructs a key from a manually specified type.
    • Key

      private Key(TypeLiteral<T> typeLiteral, Key.AnnotationStrategy annotationStrategy)
      Constructs a key from a manually specified type.
  • Method Details

    • computeHashCode

      private int computeHashCode()
      Computes the hash code for this key.
    • getTypeLiteral

      public final TypeLiteral<T> getTypeLiteral()
      Gets the key type.
    • getAnnotationType

      public final Class<? extends Annotation> getAnnotationType()
      Gets the annotation type. Will be null if this key lacks an annotation.
    • getAnnotation

      public final Annotation getAnnotation()
      Gets the annotation instance if available. Will be null if this key lacks an annotation or the key was constructed with a Class<Annotation>.

      Warning: this can return null even if this key is annotated. To check whether a Key has an annotation use hasAnnotationType() instead.

    • hasAnnotationType

      boolean hasAnnotationType()
    • getAnnotationName

      String getAnnotationName()
    • getRawType

      Class<? super T> getRawType()
    • providerKey

      Key<Provider<T>> providerKey()
      Gets the key of this key's provider.
    • equals

      public final boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public final int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public final String toString()
      Overrides:
      toString in class Object
    • get

      static <T> Key<T> get(Class<T> type, Key.AnnotationStrategy annotationStrategy)
      Gets a key for an injection type and an annotation strategy.
    • get

      public static <T> Key<T> get(Class<T> type)
      Gets a key for an injection type.
    • get

      public static <T> Key<T> get(Class<T> type, Class<? extends Annotation> annotationType)
      Gets a key for an injection type and an annotation type.
    • get

      public static <T> Key<T> get(Class<T> type, Annotation annotation)
      Gets a key for an injection type and an annotation.
    • get

      public static Key<?> get(Type type)
      Gets a key for an injection type.
    • get

      public static Key<?> get(Type type, Class<? extends Annotation> annotationType)
      Gets a key for an injection type and an annotation type.
    • get

      public static Key<?> get(Type type, Annotation annotation)
      Gets a key for an injection type and an annotation.
    • get

      public static <T> Key<T> get(TypeLiteral<T> typeLiteral)
      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

      public static <T> Key<T> get(TypeLiteral<T> typeLiteral, Annotation annotation)
      Gets a key for an injection type and an annotation.
    • ofType

      public <T> Key<T> ofType(Class<T> type)
      Returns a new key of the specified type with the same annotation as this key.
      Since:
      3.0
    • ofType

      public Key<?> ofType(Type type)
      Returns a new key of the specified type with the same annotation as this key.
      Since:
      3.0
    • ofType

      public <T> Key<T> ofType(TypeLiteral<T> type)
      Returns a new key of the specified type with the same annotation as this key.
      Since:
      3.0
    • hasAttributes

      public boolean hasAttributes()
      Returns true if this key has annotation attributes.
      Since:
      3.0
    • withoutAttributes

      public Key<T> withoutAttributes()
      Returns this key without annotation attributes, i.e. with only the annotation type.
      Since:
      3.0
    • strategyFor

      static Key.AnnotationStrategy strategyFor(Annotation annotation)
      Gets the strategy for an annotation.
    • strategyFor

      static Key.AnnotationStrategy strategyFor(Class<? extends Annotation> annotationType)
      Gets the strategy for an annotation type.
    • ensureRetainedAtRuntime

      private static void ensureRetainedAtRuntime(Class<? extends Annotation> annotationType)
    • ensureIsBindingAnnotation

      private static void ensureIsBindingAnnotation(Class<? extends Annotation> annotationType)