Class InstantiationUtils


  • public class InstantiationUtils
    extends java.lang.Object
    Utility methods for instantiating objects.
    Since:
    1.0
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> T instantiate​(java.lang.Class<T> type)
      Instantiate the given class rethrowing any exceptions as InstantiationException.
      static <T> T instantiate​(java.lang.Class<T> type, java.lang.Class<?>[] argTypes, java.lang.Object... args)
      Instantiate the given class rethrowing any exceptions as InstantiationException.
      static <T> T instantiate​(java.lang.String type, java.lang.Class<T> requiredType)
      Instantiate the given class rethrowing any exceptions as InstantiationException.
      static java.lang.Object instantiate​(java.lang.String type, java.lang.ClassLoader classLoader)
      Instantiate the given class rethrowing any exceptions as InstantiationException.
      static <T> java.util.Optional<T> tryInstantiate​(java.lang.Class<T> type)
      Try to instantiate the given class using the most optimal strategy first trying the BeanIntrospector and if no bean is present falling back to reflection.
      static <T> java.util.Optional<T> tryInstantiate​(java.lang.Class<T> type, java.util.Map propertiesMap, ConversionContext context)
      Try to instantiate the given class using BeanIntrospector.
      static <T> java.util.Optional<T> tryInstantiate​(java.lang.reflect.Constructor<T> type, java.lang.Object... args)
      Try to instantiate the given class.
      static java.util.Optional<?> tryInstantiate​(java.lang.String name, java.lang.ClassLoader classLoader)
      Try to instantiate the given class.
      • Methods inherited from class java.lang.Object

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

      • InstantiationUtils

        public InstantiationUtils()
    • Method Detail

      • tryInstantiate

        public static java.util.Optional<?> tryInstantiate​(java.lang.String name,
                                                           java.lang.ClassLoader classLoader)
        Try to instantiate the given class.
        Parameters:
        name - The class name
        classLoader - The class loader to use
        Returns:
        The instantiated instance or Optional.empty()
      • tryInstantiate

        @NonNull
        public static <T> java.util.Optional<T> tryInstantiate​(@NonNull
                                                               java.lang.Class<T> type,
                                                               java.util.Map propertiesMap,
                                                               ConversionContext context)
        Try to instantiate the given class using BeanIntrospector.
        Type Parameters:
        T - The generic type
        Parameters:
        type - The type
        propertiesMap - The properties values Map of the instance
        context - The Conversion context
        Returns:
        The instantiated instance or Optional.empty()
        Throws:
        InstantiationException - When an error occurs
      • tryInstantiate

        @NonNull
        public static <T> java.util.Optional<T> tryInstantiate​(@NonNull
                                                               java.lang.Class<T> type)
        Try to instantiate the given class using the most optimal strategy first trying the BeanIntrospector and if no bean is present falling back to reflection.
        Type Parameters:
        T - The generic type
        Parameters:
        type - The type
        Returns:
        The instantiated instance or Optional.empty()
      • tryInstantiate

        @NonNull
        public static <T> java.util.Optional<T> tryInstantiate​(@NonNull
                                                               java.lang.reflect.Constructor<T> type,
                                                               java.lang.Object... args)
        Try to instantiate the given class.
        Type Parameters:
        T - The generic type
        Parameters:
        type - The type
        args - The arguments to the constructor
        Returns:
        The instantiated instance or Optional.empty()
      • instantiate

        public static <T> T instantiate​(java.lang.Class<T> type)
        Instantiate the given class rethrowing any exceptions as InstantiationException.
        Type Parameters:
        T - The generic type
        Parameters:
        type - The type
        Returns:
        The instantiated instance
        Throws:
        InstantiationException - When an error occurs
      • instantiate

        public static <T> T instantiate​(java.lang.Class<T> type,
                                        java.lang.Class<?>[] argTypes,
                                        java.lang.Object... args)
        Instantiate the given class rethrowing any exceptions as InstantiationException.
        Type Parameters:
        T - The generic type
        Parameters:
        type - The type
        argTypes - The argument types
        args - The values of arguments
        Returns:
        The instantiated instance
        Throws:
        InstantiationException - When an error occurs
        Since:
        3.0.0
      • instantiate

        public static java.lang.Object instantiate​(java.lang.String type,
                                                   java.lang.ClassLoader classLoader)
        Instantiate the given class rethrowing any exceptions as InstantiationException.
        Parameters:
        type - The type
        classLoader - The classloader
        Returns:
        The instantiated instance
        Throws:
        InstantiationException - When an error occurs
      • instantiate

        public static <T> T instantiate​(java.lang.String type,
                                        java.lang.Class<T> requiredType)
        Instantiate the given class rethrowing any exceptions as InstantiationException.
        Type Parameters:
        T - The type
        Parameters:
        type - The type
        requiredType - The required type
        Returns:
        The instantiated instance
        Throws:
        InstantiationException - When an error occurs