Class TypeHolder<T>

java.lang.Object
com.cedarsoftware.util.TypeHolder<T>
Type Parameters:
T - the type that is being captured

public class TypeHolder<T> extends Object
TypeHolder captures a generic Type (including parameterized types) at runtime. It is typically used via anonymous subclassing to capture generic type information. However, when you already have a Type (such as a raw Class or a fully parameterized type), you can use the static of() method to create a TypeHolder instance.

Example usage via anonymous subclassing:

     TypeHolder<List<Point>> holder = new TypeHolder<List<Point>>() {};
     Type captured = holder.getType();
 

Example usage using the of() method:

     // With a raw class:
     TypeHolder<Point> holder = TypeHolder.of(Point.class);

     // With a parameterized type (if you already have one):
     Type type = new TypeReference<List<Point>>() {}.getType();
     TypeHolder<List<Point>> holder2 = TypeHolder.of(type);
 
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Default constructor that uses anonymous subclassing to capture the type parameter.
    protected
    New constructor used to explicitly set the type.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns the captured Type, which may be a raw Class, a ParameterizedType, a GenericArrayType, or another Type.
    static <T> TypeHolder<T>
    of(Type type)
    Creates a TypeHolder instance that wraps the given Type.
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • TypeHolder

      protected TypeHolder()
      Default constructor that uses anonymous subclassing to capture the type parameter.
    • TypeHolder

      protected TypeHolder(Type type)
      New constructor used to explicitly set the type.
      Parameters:
      type - the Type to be held
  • Method Details

    • getType

      public Type getType()
      Returns the captured Type, which may be a raw Class, a ParameterizedType, a GenericArrayType, or another Type.
      Returns:
      the captured Type
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • of

      public static <T> TypeHolder<T> of(Type type)
      Creates a TypeHolder instance that wraps the given Type. This factory method is useful when you already have a Type (or Class) and wish to use the generic API without anonymous subclassing.

      Example usage:

       // For a raw class:
       TypeHolder<Point> holder = TypeHolder.of(Point.class);
      
       // For a parameterized type:
       Type type = new TypeReference<List<Point>>() {}.getType();
       TypeHolder<List<Point>> holder2 = TypeHolder.of(type);
       
      Type Parameters:
      T - the type parameter
      Parameters:
      type - the Type to wrap in a TypeHolder
      Returns:
      a TypeHolder instance that returns the given type via getType()