Class GenericType<T>
- java.lang.Object
-
- dk.cloudcreate.essentials.shared.types.GenericType<T>
-
- Type Parameters:
T- the type provided to theGenericType
public abstract class GenericType<T> extends Object
Using this class makes it possible to capture a generic/parameterized argument type, such asList<Money>, instead of having to rely on the classical.classconstruct.
When you specify a type reference using.classyou loose any Generic/Parameterized information, as you cannot writeList<Money>.class, onlyList.class.
WithGenericTypeyou can specify and capture parameterized type information
You can specify a parameterized type usingGenericType:
var genericType = new GenericType<List<Money>>(){};
Thetype/getType()will returnList.class
AndgenericType/getGenericType()will returnParameterizedType, which can be introspected further
You can also supply a non-parameterized type toGenericType:
var genericType = new GenericType<Money>(){};
In which casetype/getType()will returnMoney.class
AndgenericType/getGenericType()will returnMoney.class
-
-
Field Summary
Fields Modifier and Type Field Description TypegenericTypeClass<T>type
-
Constructor Summary
Constructors Constructor Description GenericType()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description TypegetGenericType()Class<T>getType()static Optional<Class<?>>resolveGenericType(Class<?> forType, int typeArgumentIndex)Resolve the concrete parameterized type a givenforTypehas specified with its direct super class.
Example:
Given super-class:
class AggregateRoot<ID, AGGREGATE_TYPE extends AggregateRoot<ID, AGGREGATE_TYPE>> implements Aggregate<ID>
And sub/concrete class:
class Order extends AggregateRoot<OrderId, Order>br>
Then:
GenericType.resolveGenericType(Order.class, 0)will returnOrderId.class
and
GenericType.resolveGenericType(Order.class, 1)will returnOrder.class
-
-
-
Method Detail
-
resolveGenericType
public static Optional<Class<?>> resolveGenericType(Class<?> forType, int typeArgumentIndex)
Resolve the concrete parameterized type a givenforTypehas specified with its direct super class.
Example:
Given super-class:
class AggregateRoot<ID, AGGREGATE_TYPE extends AggregateRoot<ID, AGGREGATE_TYPE>> implements Aggregate<ID>
And sub/concrete class:
class Order extends AggregateRoot<OrderId, Order>br>
Then:
GenericType.resolveGenericType(Order.class, 0)will returnOrderId.class
and
GenericType.resolveGenericType(Order.class, 1)will returnOrder.class- Parameters:
forType- the type that is specifying a parameterized type with its super-classtypeArgumentIndex- the index in the superclasses type parameters (0 based)- Returns:
- an optional with the parameterized type or
Optional.empty()if the type couldn't resolved
-
getGenericType
public Type getGenericType()
-
-