java.lang.Object
dk.cloudcreate.essentials.shared.types.GenericType<T>
Type Parameters:
T - the type provided to the GenericType

public abstract class GenericType<T> extends Object
Using this class makes it possible to capture a generic/parameterized argument type, such as 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 information

You can specify a parameterized type using GenericType:
var genericType = new GenericType<List<Money>>(){};

The type/getType() will return List.class
And genericType/getGenericType() will return ParameterizedType, which can be introspected further


You can also supply a non-parameterized type to GenericType:
var genericType = new GenericType<Money>(){};

In which case type/getType() will return Money.class
And genericType/getGenericType() will return Money.class
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    final Type
     
    final Class<T>
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
     
     
    static Class<?>
    resolveGenericTypeForInterface(Class<?> forType, Class<?> withGenericInterface, int typeArgumentIndex)
    Resolve a specific parameterized type argument typeArgumentIndex for a given forType which implements a generic/parameterize interface withGenericInterface
    Example:
    Given this withGenericInterface:
    static Class<?>
    resolveGenericTypeOnSuperClass(Class<?> forType, int typeArgumentIndex)
    Resolve the concrete parameterized type a given forType has 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 return OrderId.class

    and
    GenericType.resolveGenericType(Order.class, 1) will return Order.class

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • type

      public final Class<T> type
    • genericType

      public final Type genericType
  • Constructor Details

    • GenericType

      public GenericType()
  • Method Details

    • getType

      public Class<T> getType()
    • getGenericType

      public Type getGenericType()
    • resolveGenericTypeOnSuperClass

      public static Class<?> resolveGenericTypeOnSuperClass(Class<?> forType, int typeArgumentIndex)
      Resolve the concrete parameterized type a given forType has 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 return OrderId.class

      and
      GenericType.resolveGenericType(Order.class, 1) will return Order.class
      Parameters:
      forType - the type that is specifying a parameterized type with its super-class
      typeArgumentIndex - 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 argument typeArgumentIndex for a given forType which implements a generic/parameterize interface withGenericInterface
      Example:
      Given this withGenericInterface:
      
       public interface WithState<ID, AGGREGATE_TYPE extends AggregateRoot<ID, AGGREGATE_TYPE>, AGGREGATE_STATE extends AggregateState<ID, AGGREGATE_TYPE>> {
       }
       
      and this forType:
      
       public static class Order extends AggregateRoot<String, Order> implements WithState<String, Order, OrderState> {
       }
       

      then calling
      
       var actualType = GenericType.resolveGenericTypeForInterface(Order.class,
                                                                   WithState.class,
                                                                   typeArgumentIndex);
       
      with these typeArgumentIndex will yield the following results:
      typeArgumentIndexReturned generic type
      0String.class
      1Order.class
      2OrderState.class
      3throws IllegalArgumentException
      Parameters:
      forType - the type that implement the withGenericInterface
      withGenericInterface - the parameterized/generic interface that specifies a parameterized type at index typeArgumentIndex
      typeArgumentIndex - the parameterized argument index (0 based)
      Returns:
      the type
      Throws:
      IllegalStateException - in case the type doesn't extend the interface, the interface isn't generic
      IllegalArgumentException - if the interface doesn't specify a parameterized argument with the specified index