Class TypeLiteral<T>

java.lang.Object
build.codemodel.dependency.injection.TypeLiteral<T>
Type Parameters:
T - the type being captured

public abstract class TypeLiteral<T> extends Object
Captures a fully-parameterized generic Type (e.g. List<Person>) that would otherwise be lost to erasure, allowing it to be used as a binding key.

Create a TypeLiteral using an anonymous subclass, so the compiler retains the type argument in the generated class's Class.getGenericSuperclass():

List<Person> people = new ArrayList<>();
// ... adding some people ...

binder.bind(new TypeLiteral<List<Person>>() {}).to(people);
Since:
Apr-2026
Author:
brian.oliver
See Also:
  • Constructor Details

    • TypeLiteral

      protected TypeLiteral()
      Constructs a TypeLiteral, capturing the type argument supplied by an anonymous subclass.
      Throws:
      IllegalArgumentException - if constructed directly (not as an anonymous subclass) or without a concrete type argument
  • Method Details

    • type

      public Type type()
      Obtains the captured generic Type.
      Returns:
      the Type