Class GenericType<T>
java.lang.Object
dk.cloudcreate.essentials.shared.types.GenericType<T>
- Type Parameters:
T- the type provided to theGenericType
Using this class makes it possible to capture a generic/parameterized argument type, such as
When you specify a type reference using
With
You can specify a parameterized type using
You can also supply a non-parameterized type to
List<Money>,
instead of having to rely on the classical .class construct.When you specify a type reference using
.class you loose any Generic/Parameterized information, as you cannot write List<Money>.class,
only List.class.With
GenericType you can specify and capture parameterized type informationYou can specify a parameterized type using
GenericType: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 to
GenericType:var genericType = new GenericType<Money>(){};
In which casetype/getType()will returnMoney.class
AndgenericType/getGenericType()will returnMoney.class
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiongetType()static Class<?>resolveGenericTypeForInterface(Class<?> forType, Class<?> withGenericInterface, int typeArgumentIndex) Resolve a specific parameterized type argumenttypeArgumentIndexfor a givenforTypewhich implements a generic/parameterize interfacewithGenericInterface
Example:
Given thiswithGenericInterface:static Class<?>resolveGenericTypeOnSuperClass(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>
Then:
GenericType.resolveGenericType(Order.class, 0)will returnOrderId.class
and
GenericType.resolveGenericType(Order.class, 1)will returnOrder.class
-
Field Details
-
type
-
genericType
-
-
Constructor Details
-
GenericType
public GenericType()
-
-
Method Details
-
getType
-
getGenericType
-
resolveGenericTypeOnSuperClass
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>
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
-
resolveGenericTypeForInterface
public static Class<?> resolveGenericTypeForInterface(Class<?> forType, Class<?> withGenericInterface, int typeArgumentIndex) Resolve a specific parameterized type argumenttypeArgumentIndexfor a givenforTypewhich implements a generic/parameterize interfacewithGenericInterface
Example:
Given thiswithGenericInterface:
and thispublic interface WithState<ID, AGGREGATE_TYPE extends AggregateRoot<ID, AGGREGATE_TYPE>, AGGREGATE_STATE extends AggregateState<ID, AGGREGATE_TYPE>> { }forType:public static class Order extends AggregateRoot<String, Order> implements WithState<String, Order, OrderState> { }
then calling
with thesevar actualType = GenericType.resolveGenericTypeForInterface(Order.class, WithState.class, typeArgumentIndex);typeArgumentIndexwill yield the following results:typeArgumentIndex Returned generic type 0 String.class 1 Order.class 2 OrderState.class 3 throws IllegalArgumentException - Parameters:
forType- the type that implement thewithGenericInterfacewithGenericInterface- the parameterized/generic interface that specifies a parameterized type at indextypeArgumentIndextypeArgumentIndex- the parameterized argument index (0 based)- Returns:
- the type
- Throws:
IllegalStateException- in case the type doesn't extend the interface, the interface isn't genericIllegalArgumentException- if the interface doesn't specify a parameterized argument with the specified index
-