Class InstantiationUtils

java.lang.Object
io.micronaut.core.reflect.InstantiationUtils

public class InstantiationUtils extends Object
Utility methods for instantiating objects.
Since:
1.0
Author:
Graeme Rocher
  • Constructor Details

    • InstantiationUtils

      public InstantiationUtils()
  • Method Details

    • tryInstantiate

      public static Optional<?> tryInstantiate(String name, 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> @NonNull Optional<T> tryInstantiate(@NonNull @NonNull Class<T> type, 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> @NonNull Optional<T> tryInstantiate(@NonNull @NonNull 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> @NonNull Optional<T> tryInstantiate(@NonNull @NonNull Constructor<T> type, 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(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(Class<T> type, Class<?>[] argTypes, 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 Object instantiate(String type, 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(String type, 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