- java.lang.Object
-
- io.helidon.common.GenericType<T>
-
- Type Parameters:
T
- the generic type parameter
- All Implemented Interfaces:
Type
public class GenericType<T> extends Object implements 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-classingGenericType
. Alternatively, an object representing a concrete parameterized type can be created using acreate(Type)
and manually specifying thetype()
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
Constructors Modifier Constructor Description protected
GenericType()
Constructs a new generic type, deriving the generic type and class from type parameter.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description T
cast(Object object)
Casts the parameter to the type of this generic type.static <N> GenericType<N>
create(Class<N> clazz)
Constructs a new generic type instance representing the given class.static <N> GenericType<N>
create(Type genericType)
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
equals(Object obj)
String
getTypeName()
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.String
toString()
Type
type()
The type represented by this generic type instance.
-
-
-
Constructor Detail
-
GenericType
protected GenericType() throws IllegalArgumentException
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 Detail
-
create
public static <N> GenericType<N> create(Type genericType) throws IllegalArgumentException
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
public static <N> GenericType<N> create(Class<N> clazz)
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
public static <N> GenericType<N> create(N object)
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
public Type 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
public Class<?> 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
public T cast(Object object) throws ClassCastException
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
public String getTypeName()
- Specified by:
getTypeName
in interfaceType
-
-