Package io.micronaut.core.reflect
Class InstantiationUtils
java.lang.Object
io.micronaut.core.reflect.InstantiationUtils
Utility methods for instantiating objects.
- Since:
- 1.0
- Author:
- Graeme Rocher
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T
instantiate
(Class<T> type) Instantiate the given class rethrowing any exceptions asInstantiationException
.static <T> T
instantiate
(Class<T> type, Class<?>[] argTypes, Object... args) Instantiate the given class rethrowing any exceptions asInstantiationException
.static <T> T
instantiate
(String type, Class<T> requiredType) Instantiate the given class rethrowing any exceptions asInstantiationException
.static Object
instantiate
(String type, ClassLoader classLoader) Instantiate the given class rethrowing any exceptions asInstantiationException
.tryInstantiate
(@NonNull 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.tryInstantiate
(@NonNull Class<T> type, Map propertiesMap, ConversionContext context) Try to instantiate the given class usingBeanIntrospector
.tryInstantiate
(@NonNull Constructor<T> type, Object... args) Try to instantiate the given class.static Optional<?>
tryInstantiate
(String name, ClassLoader classLoader) Try to instantiate the given class.
-
Constructor Details
-
InstantiationUtils
public InstantiationUtils()
-
-
Method Details
-
tryInstantiate
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> @NonNull Optional<T> tryInstantiate(@NonNull @NonNull Class<T> type, 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
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> @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 typeargs
- The arguments to the constructor- Returns:
- The instantiated instance or
Optional.empty()
-
instantiate
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
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
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
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
-