java.lang.Object
io.helidon.common.GenericType<T>
- Type Parameters:
T
- the generic type parameter
- All Implemented Interfaces:
Type
Represents a full type including generics declaration, to avoid information loss due to type erasure.
Supports in-line instantiation of objects that represent generic types with
actual type parameters. An object that represents any parameterized type may
be obtained by sub-classing
GenericType
. Alternatively, an object
representing a concrete parameterized type can be created using a
create(Type)
and manually specifying
the type()
actual (parameterized) type}.
For example:
GenericType<List<String>> stringListType = new GenericType<List<String>>() {};
Or:
public class MyGenericType extends GenericType<List<String>> { ... } ... MyGenericType stringListType = new MyGenericType();
Note that due to the Java type erasure limitations the parameterized type information
must be specified on a subclass, not just during the instance creation. For example,
the following case would throw an IllegalArgumentException
:
public class MyGenericType<T> extends GenericType<T> { ... } ... // The type is only specified on instance, not in a sub-class MyGenericType<List<String>> stringListType = new MyGenericType<List<String>>();
-
Constructor Summary
ModifierConstructorDescriptionprotected
Constructs a new generic type, deriving the generic type and class from type parameter. -
Method Summary
Modifier and TypeMethodDescriptionCasts the parameter to the type of this generic type.static <N> GenericType<N>
Constructs a new generic type instance representing the given class.static <N> GenericType<N>
Constructs a new generic type, using the provided generic type information and deriving the class.static <N> GenericType<N>
create
(N object) Constructs a new generic type instance representing the class of the given object.boolean
int
hashCode()
boolean
isClass()
Whether this generic type represents a simple class with no generic information.Class<?>
rawType()
Returns the object representing the class or interface that declared the type represented by this generic type instance.toString()
type()
The type represented by this generic type instance.
-
Constructor Details
-
GenericType
Constructs a new generic type, deriving the generic type and class from type parameter. Note that this constructor is protected, users should create a (usually anonymous) subclass as shown above.- Throws:
IllegalArgumentException
- in case the generic type parameter value is not provided by any of the subclasses.
-
-
Method Details
-
create
Constructs a new generic type, using the provided generic type information and deriving the class.- Type Parameters:
N
- generic type of the returned GenericType- Parameters:
genericType
- the generic type- Returns:
- new type wrapping the provided type
- Throws:
IllegalArgumentException
- if genericType isnull
or not an instance ofClass
orParameterizedType
whose raw type is an instance ofClass
.
-
create
Constructs a new generic type instance representing the given class.- Type Parameters:
N
- generic type of the returned GenericType- Parameters:
clazz
- the class to represent- Returns:
- new type wrapping the provided class
-
create
Constructs a new generic type instance representing the class of the given object.- Type Parameters:
N
- generic type of the returned GenericType- Parameters:
object
- the object to derive the class of- Returns:
- new type wrapping the class of the provided object
-
type
The type represented by this generic type instance.For
new GenericType<List<String>>(){}
, this would return aParameterizedType
forjava.util.List<java.lang.String>
.- Returns:
- the actual type represented by this generic type instance.
-
rawType
Returns the object representing the class or interface that declared the type represented by this generic type instance.For
new GenericType<List<String>>(){}
, this would return aninterface java.util.List
.- Returns:
- the class or interface that declared the type represented by this generic type instance.
-
isClass
public boolean isClass()Whether this generic type represents a simple class with no generic information.- Returns:
- true if this is a class, false if this is a generic type (e.g. returns true for
List
, false forList<String>
-
cast
Casts the parameter to the type of this generic type. This is a utility method to use in stream processing etc.- Parameters:
object
- instance to cast- Returns:
- typed instance
- Throws:
ClassCastException
- in case the object is not of the expected type
-
getTypeName
- Specified by:
getTypeName
in interfaceType
-
hashCode
public int hashCode() -
equals
-
toString
-