Package com.cedarsoftware.util
Class TypeHolder<T>
java.lang.Object
com.cedarsoftware.util.TypeHolder<T>
- Type Parameters:
T
- the type that is being captured
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
ConstructorsModifierConstructorDescriptionprotected
Default constructor that uses anonymous subclassing to capture the type parameter.protected
TypeHolder
(Type type) New constructor used to explicitly set the type. -
Method Summary
-
Constructor Details
-
TypeHolder
protected TypeHolder()Default constructor that uses anonymous subclassing to capture the type parameter. -
TypeHolder
New constructor used to explicitly set the type.- Parameters:
type
- the Type to be held
-
-
Method Details
-
getType
Returns the captured Type, which may be a raw Class, a ParameterizedType, a GenericArrayType, or another Type.- Returns:
- the captured Type
-
toString
-
of
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()
-