Package io.micronaut.core.reflect
Class InstantiationUtils
- java.lang.Object
-
- io.micronaut.core.reflect.InstantiationUtils
-
public class InstantiationUtils extends java.lang.Object
Utility methods for instantiating objects.- Since:
- 1.0
-
-
Constructor Summary
Constructors Constructor Description InstantiationUtils()
-
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 asInstantiationException
.static <T> T
instantiate(java.lang.Class<T> type, java.lang.Class<?>[] argTypes, java.lang.Object... args)
Instantiate the given class rethrowing any exceptions asInstantiationException
.static <T> T
instantiate(java.lang.String type, java.lang.Class<T> requiredType)
Instantiate the given class rethrowing any exceptions asInstantiationException
.static java.lang.Object
instantiate(java.lang.String type, java.lang.ClassLoader classLoader)
Instantiate the given class rethrowing any exceptions asInstantiationException
.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 theBeanIntrospector
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 usingBeanIntrospector
.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.
-
-
-
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 nameclassLoader
- 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 usingBeanIntrospector
.- Type Parameters:
T
- The generic type- Parameters:
type
- The typepropertiesMap
- The properties valuesMap
of the instancecontext
- 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 theBeanIntrospector
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 typeargs
- 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 asInstantiationException
.- 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 asInstantiationException
.- Type Parameters:
T
- The generic type- Parameters:
type
- The typeargTypes
- The argument typesargs
- 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 asInstantiationException
.- Parameters:
type
- The typeclassLoader
- 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 asInstantiationException
.- Type Parameters:
T
- The type- Parameters:
type
- The typerequiredType
- The required type- Returns:
- The instantiated instance
- Throws:
InstantiationException
- When an error occurs
-
-