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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Tinstantiate(Class<T> type) Instantiate the given class rethrowing any exceptions asInstantiationException.static <T> Tinstantiate(Class<T> type, Class<?>[] argTypes, Object... args) Instantiate the given class rethrowing any exceptions asInstantiationException.static <T> Tinstantiate(String type, Class<T> requiredType) Instantiate the given class rethrowing any exceptions asInstantiationException.static Objectinstantiate(String type, ClassLoader classLoader) Instantiate the given class rethrowing any exceptions asInstantiationException.static <T> TinstantiateReflectively(Class<T> type, Class<?>[] argTypes, Object[] args) Instantiate the given class rethrowing any exceptions asInstantiationException, only using reflection.tryInstantiate(@NonNull Class<T> type) Try to instantiate the given class using the most optimal strategy first trying theBeanIntrospectorand 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 valuesMapof 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 theBeanIntrospectorand 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
-
instantiateReflectively
@NonNull public static <T> T instantiateReflectively(Class<T> type, Class<?>[] argTypes, Object[] args) Instantiate the given class rethrowing any exceptions asInstantiationException, only using reflection.- 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:
- 4.8.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
-