Top Description Inners Fields Constructors Methods
com.google.inject

public Class Key<T>

extends Object
Class Inheritance
Annotations
@CheckReturnValue
Static Imports
com.google.common.base.Preconditions.checkArgument, .Preconditions.checkNotNull, com.google.inject.internal.Annotations.generateAnnotation, .Annotations.isAllDefaultMethods

Guice uses Key objects to identify a dependency that can be resolved by the Guice Injector. A Guice key consists of an injection type and an optional annotation.

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.

Author
[email protected] (Bob Lee)

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
pack-priv static interface
pack-priv static class
pack-priv static enum

Field Summary

Modifier and TypeField and Description
private final Key.AnnotationStrategy
private final int
private String
private final TypeLiteral<T>

Constructor Summary

AccessConstructor and Description
protected
Key(Class<? extends Annotation> annotationType)

Constructs a new key.

protected
Key(Annotation annotation)

Constructs a new key.

protected
Key()

Constructs a new key.

private
Key(Type type, Key.AnnotationStrategy annotationStrategy)

Unsafe.

private
Key(TypeLiteral<T> typeLiteral, Key.AnnotationStrategy annotationStrategy)

Constructs a key from a manually specified type.

Method Summary

Modifier and TypeMethod and Description
private int
computeHashCode()

Computes the hash code for this key.

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

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

public final boolean
equals(Object
the reference object with which to compare.
o
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
pack-priv static <T> Key<T>
get(Class<T> type, Key.AnnotationStrategy annotationStrategy)

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

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

Gets a key for an injection type.

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

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

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

Gets a key for an injection type.

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

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

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

Gets a key for an injection type and an annotation.

public final Annotation
getAnnotation()

Gets the annotation instance if available.

pack-priv String
public final Class<? extends Annotation>
getAnnotationType()

Gets the annotation type.

pack-priv Class<? super T>
public final TypeLiteral<T>
getTypeLiteral()

Gets the key type.

pack-priv boolean
public boolean
hasAttributes()

Returns true if this key has annotation attributes.

public final int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.
public <U> Key<U>
ofType(Class<U> 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 <U> Key<U>
ofType(TypeLiteral<U> type)

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

pack-priv Key<Provider<T>>
providerKey()

Gets the key of this key's provider.

pack-priv static Key.AnnotationStrategy
strategyFor(Annotation annotation)

Gets the strategy for an annotation.

pack-priv static Key.AnnotationStrategy
strategyFor(Class<? extends Annotation> annotationType)

Gets the strategy for an annotation type.

public final String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
public Key<T>
withAnnotation(Class<? extends Annotation> annotationType)

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

public Key<T>
withAnnotation(Annotation annotation)

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

public Key<T>
withoutAttributes()

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

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

annotationStrategyback to summary
private final Key.AnnotationStrategy annotationStrategy
hashCodeback to summary
private final int hashCode
toStringback to summary
private String toString
typeLiteralback to summary
private final TypeLiteral<T> typeLiteral

Constructor Detail

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

Annotations
@SuppressWarnings:unchecked
Keyback to summary
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()) {}.

Annotations
@SuppressWarnings:unchecked
Keyback to summary
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>() {}.

Annotations
@SuppressWarnings:unchecked
Keyback to summary
private Key(Type type, Key.AnnotationStrategy annotationStrategy)

Unsafe. Constructs a key from a manually specified type.

Annotations
@SuppressWarnings:unchecked
Keyback to summary
private Key(TypeLiteral<T> typeLiteral, Key.AnnotationStrategy annotationStrategy)

Constructs a key from a manually specified type.

Method Detail

computeHashCodeback to summary
private int computeHashCode()

Computes the hash code for this key.

ensureIsBindingAnnotationback to summary
private static void ensureIsBindingAnnotation(Class<? extends Annotation> annotationType)
ensureRetainedAtRuntimeback to summary
private static void ensureRetainedAtRuntime(Class<? extends Annotation> annotationType)
equalsback to summary
public final boolean equals(Object o)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
o:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
getback to summary
pack-priv static <T> Key<T> get(Class<T> type, Key.AnnotationStrategy annotationStrategy)

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

getback to summary
public static <T> Key<T> get(Class<T> type)

Gets a key for an injection type.

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

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

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

Gets a key for an injection type and an annotation.

getback to summary
public static Key<?> get(Type type)

Gets a key for an injection type.

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

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

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

Gets a key for an injection type and an annotation.

getback to summary
public static <T> Key<T> get(TypeLiteral<T> typeLiteral)

Gets a key for an injection type.

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

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

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

Gets a key for an injection type and an annotation.

getAnnotationback to summary
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.

getAnnotationNameback to summary
pack-priv String getAnnotationName()
getAnnotationTypeback to summary
public final Class<? extends Annotation> getAnnotationType()

Gets the annotation type. Will be null if this key lacks an annotation.

getRawTypeback to summary
pack-priv Class<? super T> getRawType()
getTypeLiteralback to summary
public final TypeLiteral<T> getTypeLiteral()

Gets the key type.

hasAnnotationTypeback to summary
pack-priv boolean hasAnnotationType()
hasAttributesback to summary
public boolean hasAttributes()

Returns true if this key has annotation attributes.

Since
3.0
hashCodeback to summary
public final int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override
ofTypeback to summary
public <U> Key<U> ofType(Class<U> type)

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

Since
3.0
ofTypeback to summary
public Key<?> ofType(Type type)

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

Since
3.0
ofTypeback to summary
public <U> Key<U> ofType(TypeLiteral<U> type)

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

Since
3.0
providerKeyback to summary
pack-priv Key<Provider<T>> providerKey()

Gets the key of this key's provider.

strategyForback to summary
pack-priv static Key.AnnotationStrategy strategyFor(Annotation annotation)

Gets the strategy for an annotation.

strategyForback to summary
pack-priv static Key.AnnotationStrategy strategyFor(Class<? extends Annotation> annotationType)

Gets the strategy for an annotation type.

toStringback to summary
public final String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object

Annotations
@Override
withAnnotationback to summary
public Key<T> withAnnotation(Class<? extends Annotation> annotationType)

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

This is equivalent to Key.get(key.getTypeLiteral(), annotation) but may be more convenient to use in certain cases.

Since
5.0
withAnnotationback to summary
public Key<T> withAnnotation(Annotation annotation)

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

This is equivalent to Key.get(key.getTypeLiteral(), annotation) but may be more convenient to use in certain cases.

Since
5.0
withoutAttributesback to summary
public Key<T> withoutAttributes()

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

Since
3.0
com.google.inject back to summary

pack-priv Class Key.AnnotationInstanceStrategy

extends Object
implements AnnotationStrategy
Class Inheritance
All Implemented Interfaces
com.google.inject.Key.AnnotationStrategy

Field Summary

Modifier and TypeField and Description
pack-priv final Annotation

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public boolean
equals(Object
the reference object with which to compare.
o
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
public Annotation
public Class<? extends Annotation>
public boolean
public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
public Key.AnnotationStrategy
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

annotationback to summary
pack-priv final Annotation annotation

Constructor Detail

AnnotationInstanceStrategyback to summary
pack-priv AnnotationInstanceStrategy(Annotation annotation)

Method Detail

equalsback to summary
public boolean equals(Object o)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
o:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
getAnnotationback to summary
public Annotation getAnnotation()

Implements com.google.inject.Key.AnnotationStrategy.getAnnotation.

Annotations
@Override
getAnnotationTypeback to summary
public Class<? extends Annotation> getAnnotationType()

Implements com.google.inject.Key.AnnotationStrategy.getAnnotationType.

Annotations
@Override
hasAttributesback to summary
public boolean hasAttributes()

Implements com.google.inject.Key.AnnotationStrategy.hasAttributes.

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object

Annotations
@Override
withoutAttributesback to summary
public Key.AnnotationStrategy withoutAttributes()

Implements com.google.inject.Key.AnnotationStrategy.withoutAttributes.

Annotations
@Override
com.google.inject back to summary

pack-priv Interface Key.AnnotationStrategy

Known Direct Implementers
com.google.inject.Key.NullAnnotationStrategy, com.google.inject.Key.AnnotationInstanceStrategy, com.google.inject.Key.AnnotationTypeStrategy

Method Summary

Modifier and TypeMethod and Description
public Annotation
public Class<? extends Annotation>
public boolean
public Key.AnnotationStrategy

Method Detail

getAnnotationback to summary
public Annotation getAnnotation()
getAnnotationTypeback to summary
public Class<? extends Annotation> getAnnotationType()
hasAttributesback to summary
public boolean hasAttributes()
withoutAttributesback to summary
public Key.AnnotationStrategy withoutAttributes()
com.google.inject back to summary

pack-priv Class Key.AnnotationTypeStrategy

extends Object
implements AnnotationStrategy
Class Inheritance
All Implemented Interfaces
com.google.inject.Key.AnnotationStrategy

Field Summary

Modifier and TypeField and Description
pack-priv final Annotation
pack-priv final Class<? extends Annotation>

Constructor Summary

AccessConstructor and Description
pack-priv
AnnotationTypeStrategy(Class<? extends Annotation> annotationType, Annotation annotation)

Method Summary

Modifier and TypeMethod and Description
public boolean
equals(Object
the reference object with which to compare.
o
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
public Annotation
public Class<? extends Annotation>
public boolean
public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
public Key.AnnotationStrategy
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

annotationback to summary
pack-priv final Annotation annotation
annotationTypeback to summary
pack-priv final Class<? extends Annotation> annotationType

Constructor Detail

AnnotationTypeStrategyback to summary
pack-priv AnnotationTypeStrategy(Class<? extends Annotation> annotationType, Annotation annotation)

Method Detail

equalsback to summary
public boolean equals(Object o)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
o:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
getAnnotationback to summary
public Annotation getAnnotation()

Implements com.google.inject.Key.AnnotationStrategy.getAnnotation.

Annotations
@Override
getAnnotationTypeback to summary
public Class<? extends Annotation> getAnnotationType()

Implements com.google.inject.Key.AnnotationStrategy.getAnnotationType.

Annotations
@Override
hasAttributesback to summary
public boolean hasAttributes()

Implements com.google.inject.Key.AnnotationStrategy.hasAttributes.

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object

Annotations
@Override
withoutAttributesback to summary
public Key.AnnotationStrategy withoutAttributes()

Implements com.google.inject.Key.AnnotationStrategy.withoutAttributes.

Annotations
@Override
com.google.inject back to summary

pack-priv final Enum Key.NullAnnotationStrategy

extends Enum<Key.NullAnnotationStrategy>
implements AnnotationStrategy
Class Inheritance
All Implemented Interfaces
com.google.inject.Key.AnnotationStrategy

Field Summary

Modifier and TypeField and Description
public static final Key.NullAnnotationStrategy

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public Annotation
public Class<? extends Annotation>
public boolean
public String
toString()

Overrides java.lang.Enum.toString.

Returns the name of this enum constant, as contained in the declaration.
public static Key.NullAnnotationStrategy
public static Key.NullAnnotationStrategy[]
public Key.AnnotationStrategy
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinalvalueOf

Field Detail

INSTANCEback to summary
public static final Key.NullAnnotationStrategy INSTANCE

Constructor Detail

NullAnnotationStrategyback to summary
private NullAnnotationStrategy()

Method Detail

getAnnotationback to summary
public Annotation getAnnotation()

Implements com.google.inject.Key.AnnotationStrategy.getAnnotation.

Annotations
@Override
getAnnotationTypeback to summary
public Class<? extends Annotation> getAnnotationType()

Implements com.google.inject.Key.AnnotationStrategy.getAnnotationType.

Annotations
@Override
hasAttributesback to summary
public boolean hasAttributes()

Implements com.google.inject.Key.AnnotationStrategy.hasAttributes.

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Enum.toString.

Doc from java.lang.Enum.toString.

Returns the name of this enum constant, as contained in the declaration. This method may be overridden, though it typically isn't necessary or desirable. An enum class should override this method when a more "programmer-friendly" string form exists.

Returns:String

the name of this enum constant

Annotations
@Override
valueOfback to summary
public static Key.NullAnnotationStrategy valueOf(String name)
valuesback to summary
public static Key.NullAnnotationStrategy[] values()
withoutAttributesback to summary
public Key.AnnotationStrategy withoutAttributes()

Implements com.google.inject.Key.AnnotationStrategy.withoutAttributes.

Annotations
@Override