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. A type token and a name. Matches the type and name (Inject.value()) at a point of injection.

For example, new Key<List<String>>("cities") {} will match:

Author:
crazybob@google.com (Bob Lee)

Field Summary
static java.lang.String DEFAULT_NAME
          Default binding name.
 
Constructor Summary
protected Key()
          Convenience method.
protected Key(java.lang.String name)
          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 a Class.
static Key<?> get(java.lang.reflect.Type type)
          Gets a key for a type.
static
<T> Key<T>
get(TypeToken<T> typeToken)
          Gets a key for a type token.
 java.lang.String getName()
          Gets the binding name.
 TypeToken<T> getTypeToken()
          Gets token representing the type to be injected.
 boolean hasDefaultName()
          Returns true if this key has the default name.
 int hashCode()
           
 Key<T> named(java.lang.String name)
          Returns a new key with the same type as this key and the given name,
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_NAME

public static final java.lang.String DEFAULT_NAME
Default binding name.

See Also:
Constant Field Values
Constructor Detail

Key

protected Key(java.lang.String name)
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 named "bar": new Key<Foo>("bar") {}.


Key

protected Key()
Convenience method. Delegates to Key(String) with DEFAULT_NAME.

Method Detail

hasDefaultName

public boolean hasDefaultName()
Returns true if this key has the default name.


named

public Key<T> named(java.lang.String name)
Returns a new key with the same type as this key and the given name,


getTypeToken

public TypeToken<T> getTypeToken()
Gets token representing the type to be injected.


getName

public java.lang.String getName()
Gets the binding name.


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 a Class. Defaults name to DEFAULT_NAME.


get

public static Key<?> get(java.lang.reflect.Type type)
Gets a key for a type. Defaults name to DEFAULT_NAME.


get

public static <T> Key<T> get(TypeToken<T> typeToken)
Gets a key for a type token. Defaults name to DEFAULT_NAME.