Package com.cedarsoftware.io
Class TypeHolder<T>
java.lang.Object
com.cedarsoftware.io.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
forType()
and forClass()
methods 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()
methods:
// With a raw class: TypeHolder<Point> holder1 = TypeHolder.forClass(Point.class); // With a parameterized type (if you already have one): Type type = new TypeReference<List<Point>>() {}.getType(); TypeHolder<List<Point>> holder2 = TypeHolder.forType(type);
- Author:
- John DeRegnaucourt ([email protected])
Copyright (c) Cedar Software LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
License
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
Default constructor that uses anonymous subclassing to capture the type parameter.protected
TypeHolder
(Type type) Constructor used to explicitly set the type. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> TypeHolder<T>
Creates a TypeHolder instance that wraps the given Class.static <T> TypeHolder<T>
Creates a TypeHolder instance that wraps the given Type.getType()
Returns the captured Type, which may be a raw Class, a ParameterizedType, a GenericArrayType, or another Type.toString()
-
Constructor Details
-
TypeHolder
protected TypeHolder()Default constructor that uses anonymous subclassing to capture the type parameter. -
TypeHolder
Constructor used to explicitly set the type.- Parameters:
type
- the Type to be held; must not be null.
-
-
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
-
forType
Creates a TypeHolder instance that wraps the given Type. This factory method is useful when you already have a Type and wish to use the generic API without anonymous subclassing.Example usage:
// For a parameterized type: Type type = new TypeReference<List<Point>>() {}.getType(); TypeHolder<List<Point>> holder = TypeHolder.forType(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()
-
forClass
Creates a TypeHolder instance that wraps the given Class. This overload is provided for convenience when you have a Class instance.Example usage:
TypeHolder<Point> holder = TypeHolder.forClass(Point.class);
- Type Parameters:
T
- the type parameter- Parameters:
clazz
- the Class to wrap in a TypeHolder- Returns:
- a TypeHolder instance that returns the given Class as a Type via
getType()
-