Class ConstructorUtils

java.lang.Object
org.axonframework.common.ConstructorUtils

public final class ConstructorUtils extends Object
Utility class for constructing objects using reflection.

Utility functions in this class do not create instances directly. Instead, they provide reusable functions that can be used to construct a multitude of instances of the same type. It is advisable to cache this function for reuse.

Since:
5.0.0
Author:
Mitchell Herrijgers
  • Method Details

    • getConstructorFunctionWithZeroArguments

      public static <T> Supplier<T> getConstructorFunctionWithZeroArguments(@Nonnull Class<T> type)
      Returns a function that constructs an instance of the given type using the zero-argument constructor. If the type does not have a zero-argument constructor, an IllegalArgumentException is thrown.
      Type Parameters:
      T - The type of object to construct.
      Parameters:
      type - The type of object to construct. Must have a zero-argument constructor.
      Returns:
      A function that constructs an instance of the given type using the zero-argument constructor.
    • factoryForTypeWithOptionalArgument

      public static <T, A> Function<A,T> factoryForTypeWithOptionalArgument(@Nonnull Class<T> type, @Nonnull Class<? extends A> argumentClass)
      Returns a function that constructs an instance of the given type using the constructor that accepts an argument of the given class, or alternatively using a zero-argument constructor. If neither is available, an IllegalArgumentException is thrown.
      Type Parameters:
      T - The type of object to construct.
      A - The type of the argument to pass to the constructor.
      Parameters:
      type - The type of object to construct. Must have a constructor that accepts an argument of the given class, or a zero-argument constructor.
      argumentClass - The class of the argument to pass to the constructor.
      Returns:
      A function that constructs an instance of the given type using the constructor that accepts an argument of the given class, or alternatively using a zero-argument constructor.