Class Key<T>

java.lang.Object
org.apache.maven.di.Key<T>
Type Parameters:
T - binding type

public abstract class Key<T> extends Object
The key defines an identity of a binding. In any DI, a key is usually a type of the object along with some optional tag to distinguish between bindings which make objects of the same type.

In Maven Inject, a key is also a type token - special abstract class that can store type information with the shortest syntax possible in Java.

For example, to create a key of type Map<String, List<Integer>>, you can just use this syntax: new Key<Map<String, List<Integer>>>(){}.

If your types are not known at compile time, you can use Types.parameterizedType(java.lang.reflect.Type, java.lang.reflect.Type, java.lang.reflect.Type[]) to make a parameterized type and give it to a Key.ofType constructor.

  • Constructor Details

  • Method Details

    • of

      public static <T> Key<T> of(Class<T> type)
    • of

      public static <T> Key<T> of(Class<T> type, @Nullable Object qualifier)
    • ofType

      public static <T> Key<T> ofType(Type type)
    • ofType

      public static <T> Key<T> ofType(Type type, @Nullable Object qualifier)
    • getType

      public Type getType()
    • getRawType

      public Class<T> getRawType()
      A shortcut for Types.getRawType(Type)(key.getType()). Also casts the result to a properly parameterized class.
    • getTypeParameter

      public <U> Key<U> getTypeParameter(int index)
      Returns a type parameter of the underlying type wrapped as a key with no qualifier.
      Throws:
      IllegalStateException - when underlying type is not a parameterized one.
    • getQualifier

      @Nullable public Object getQualifier()
    • getDisplayString

      public String getDisplayString()
      Returns an underlying type with display string formatting (package names stripped) and prepended qualifier display string if this key has a qualifier.
    • equals

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

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

      public String toString()
      Overrides:
      toString in class Object