Class InstanceUtils


  • public final class InstanceUtils
    extends java.lang.Object
    Class instance creation utility (to gather all instantiations in one place).
    Since:
    16.11.2023
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> T create​(java.lang.Class<T> type)
      Create a new instance using no-args constructor.
      static <T> T create​(java.lang.Class<T> type, java.lang.Class<?>[] param, java.lang.Object... args)
      Create a new instance using constructor with provided parameters and values.
      static <T> T create​(java.lang.Class<T> type, java.lang.Class<?> param, java.lang.Object arg)
      Shortcut for create(Class, Class[], Object...) for one constructor argument.
      static <T> T createWithAnyConstructor​(java.lang.Class<T> type)
      For tests ONLY!
      static <T> T createWithNulls​(java.lang.Class<T> type, java.lang.Class<?>... param)
      Create new instance using constructor with provided params and nulls as values.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • create

        public static <T> T create​(java.lang.Class<T> type)
        Create a new instance using no-args constructor.
        Type Parameters:
        T - instance type
        Parameters:
        type - class
        Returns:
        class instance
      • create

        public static <T> T create​(java.lang.Class<T> type,
                                   java.lang.Class<?> param,
                                   java.lang.Object arg)
        Shortcut for create(Class, Class[], Object...) for one constructor argument.
        Type Parameters:
        T - instance type
        Parameters:
        type - class
        param - constructor parameter type
        arg - parameter value
        Returns:
        object instance
      • create

        public static <T> T create​(java.lang.Class<T> type,
                                   java.lang.Class<?>[] param,
                                   java.lang.Object... args)
        Create a new instance using constructor with provided parameters and values.
        Type Parameters:
        T - instance type
        Parameters:
        type - class
        param - constructor parameter types
        args - constructor arguments
        Returns:
        object instance
      • createWithNulls

        public static <T> T createWithNulls​(java.lang.Class<T> type,
                                            java.lang.Class<?>... param)
        Create new instance using constructor with provided params and nulls as values.
        Type Parameters:
        T - instance type
        Parameters:
        type - class
        param - constructor params
        Returns:
        object instance
      • createWithAnyConstructor

        public static <T> T createWithAnyConstructor​(java.lang.Class<T> type)
        For tests ONLY!

        Tries to find constructor with the smallest amount of arguments and use it with nulls to instantiate object.

        WARNING: primitive arguments are not supported!

        Type Parameters:
        T - target type
        Parameters:
        type - class
        Returns:
        class instance