Class Key<T>

java.lang.Object
org.apache.maven.di.Key<T>
Type Parameters:
T - The type this key represents

public abstract class Key<T> extends Object
A binding key that uniquely identifies a dependency in the injection system.

Keys combine a type with an optional qualifier to uniquely identify dependencies within the injection system. They also serve as type tokens, allowing preservation of generic type information at runtime.

Example usage:

 // Simple key for a type
 Key<Service> simple = Key.of(Service.class);

 // Key with generic type information
 Key<List<String>> generic = new Key<List<String>>(){};

 // Key with qualifier
 Key<Service> qualified = Key.of(Service.class, "primary");
 
Since:
4.0.0
  • Constructor Details

  • Method Details

    • of

      public static <T> Key<T> of(Class<T> type)
      Creates a new Key instance for the specified type.
      Type Parameters:
      T - the type parameter
      Parameters:
      type - the Class object representing the type
      Returns:
      a new Key instance
      Throws:
      NullPointerException - if type is null
    • of

      public static <T> Key<T> of(Class<T> type, @Nullable Object qualifier)
      Creates a new Key instance for the specified type with a qualifier.
      Type Parameters:
      T - the type parameter
      Parameters:
      type - the Class object representing the type
      qualifier - the qualifier object (typically an annotation instance)
      Returns:
      a new Key instance
      Throws:
      NullPointerException - if type is null
    • 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()
      Returns the actual type represented by this key.

      This includes full generic type information if available.

      Returns:
      the type represented by this key
    • 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()
      Returns the qualifier associated with this key, if any.
      Returns:
      the qualifier object or null if none exists
    • 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