TypeLiteral
Date: 2017-02-08

org.apache.commons.lang3.reflect

## Class TypeLiteral<T>

- java.lang.Object
- - org.apache.commons.lang3.reflect.TypeLiteral<T>

- All Implemented Interfaces:
    Typed<T>

      public abstract class TypeLiteral<T>
      extends java.lang.Object
      implements Typed<T>

  Type literal comparable to `javax.enterprise.util.TypeLiteral`, made generally available outside the JEE context. Allows the passing around of a "token" that represents a type in a typesafe manner, as opposed to passing the (non-parameterized) `Type` object itself. Consider:

  You might see such a typesafe API as:

       class Typesafe {
         <T> T obtain(Class<T> type, ...);
       }

  Consumed in the manner of:

       Foo foo = typesafe.obtain(Foo.class, ...);

  Yet, you run into problems when you want to do this with a parameterized type:

       List<String> listOfString = typesafe.obtain(List.class, ...); // could only give us a raw List

  `java.lang.reflect.Type` might provide some value:

       Type listOfStringType = ...; // firstly, how to obtain this? Doable, but not straightforward.
       List<String> listOfString = (List<String>) typesafe.obtain(listOfStringType, ...); // nongeneric Type would necessitate a cast

  The "type literal" concept was introduced to provide an alternative, i.e.:

       class Typesafe {
         <T> T obtain(TypeLiteral<T> type, ...);
       }

  Consuming code looks like:

       List<String> listOfString = typesafe.obtain(new TypeLiteral<List<String>>() {}, ...);

  This has the effect of "jumping up" a level to tie a `java.lang.reflect.Type` to a type variable while simultaneously making it short work to obtain a `Type` instance for any given type, inline.

  Additionally `TypeLiteral` implements the `Typed` interface which is a generalization of this concept, and which may be implemented in custom classes. It is suggested that APIs be defined in terms of the interface, in the following manner:

         <T> T obtain(Typed<T> typed, ...);

  Since:
    3.2

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `java.lang.reflect.Type` | `value` Represented type.

  - ### Constructor Summary

    Constructors
    Modifier | Constructor and Description
    --- | ---
    `protected` | `TypeLiteral()` The default constructor.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `boolean` | `equals(java.lang.Object obj)`
    `java.lang.reflect.Type` | `getType()` Get the `Type` represented by this entity.
    `int` | `hashCode()`
    `java.lang.String` | `toString()`

    - ### Methods inherited from class java.lang.Object

      `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

- - ### Field Detail

    - #### value

          public final java.lang.reflect.Type value

      Represented type.

  - ### Constructor Detail

    - #### TypeLiteral

          protected TypeLiteral()

      The default constructor.

  - ### Method Detail

    - #### equals

          public final boolean equals(java.lang.Object obj)

      Overrides:
        `equals` in class `java.lang.Object`

    - #### hashCode

          public int hashCode()

      Overrides:
        `hashCode` in class `java.lang.Object`

    - #### toString

          public java.lang.String toString()

      Overrides:
        `toString` in class `java.lang.Object`

    - #### getType

          public java.lang.reflect.Type getType()

      Description copied from interface: `Typed`
      Get the `Type` represented by this entity.
      Specified by:
        `getType` in interface `Typed<T>`
      Returns:
        Type