com.google.inject
Class Key<T>

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

public abstract class Key<T>
extends java.lang.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.

Author:
crazybob@google.com (Bob Lee)

Constructor Summary
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.
 
Method Summary
 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.
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.
 java.lang.Class<? extends java.lang.annotation.Annotation> getAnnotationType()
          Gets the annotation type.
 TypeLiteral<T> getTypeLiteral()
          Gets the key type.
 int hashCode()
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Key

protected Key(java.lang.Class<? extends java.lang.annotation.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(java.lang.annotation.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>() {}.

Method Detail

getTypeLiteral

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


getAnnotationType

public java.lang.Class<? extends java.lang.annotation.Annotation> getAnnotationType()
Gets the annotation type.


getAnnotation

public java.lang.annotation.Annotation getAnnotation()
Gets the annotation.


hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

equals

public boolean equals(java.lang.Object o)
Overrides:
equals in class java.lang.Object

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

get

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


get

public 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.


get

public 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.


get

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


get

public 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.


get

public static Key<?> get(java.lang.reflect.Type type,
                         java.lang.annotation.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,
                             java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
Gets a key for an injection type and an annotation type.


get

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



Copyright © 2008. All Rights Reserved.