use of io.micronaut.core.reflect.exception.InstantiationException in project micronaut-core by micronaut-projects.
the class InstantiationUtils method tryInstantiate.
/**
* Try to instantiate the given class using {@link io.micronaut.core.beans.BeanIntrospector}.
*
* @param type The type
* @param propertiesMap The properties values {@link Map} of the instance
* @param context The Conversion context
* @param <T> The generic type
* @return The instantiated instance or {@link Optional#empty()}
* @throws InstantiationException When an error occurs
*/
@NonNull
public static <T> Optional<T> tryInstantiate(@NonNull Class<T> type, Map propertiesMap, ConversionContext context) {
ArgumentUtils.requireNonNull("type", type);
if (propertiesMap.isEmpty()) {
return tryInstantiate(type);
}
final Supplier<T> reflectionFallback = () -> {
Logger log = LoggerFactory.getLogger(InstantiationUtils.class);
if (log.isDebugEnabled()) {
log.debug("Tried, but could not instantiate type: " + type);
}
return null;
};
T result = BeanIntrospector.SHARED.findIntrospection(type).map(introspection -> {
T instance;
Argument[] constructorArguments = introspection.getConstructorArguments();
List<Object> arguments = new ArrayList<>(constructorArguments.length);
try {
if (constructorArguments.length > 0) {
Map bindMap = new LinkedHashMap(propertiesMap.size());
Set<Map.Entry<?, ?>> entries = propertiesMap.entrySet();
for (Map.Entry<?, ?> entry : entries) {
Object key = entry.getKey();
bindMap.put(NameUtils.decapitalize(NameUtils.dehyphenate(key.toString())), entry.getValue());
}
for (Argument<?> argument : constructorArguments) {
if (bindMap.containsKey(argument.getName())) {
Object converted = ConversionService.SHARED.convert(bindMap.get(argument.getName()), argument.getType(), ConversionContext.of(argument)).orElseThrow(() -> new ConversionErrorException(argument, context.getLastError().orElse(() -> new IllegalArgumentException("Value [" + bindMap.get(argument.getName()) + "] cannot be converted to type : " + argument.getType()))));
arguments.add(converted);
} else if (argument.isDeclaredNullable()) {
arguments.add(null);
} else {
context.reject(new ConversionErrorException(argument, () -> new IllegalArgumentException("No Value found for argument " + argument.getName())));
}
}
instance = introspection.instantiate(arguments.toArray());
} else {
instance = introspection.instantiate();
}
return instance;
} catch (InstantiationException e) {
return reflectionFallback.get();
}
}).orElseGet(reflectionFallback);
return Optional.ofNullable(result);
}
use of io.micronaut.core.reflect.exception.InstantiationException in project micronaut-core by micronaut-projects.
the class InstantiationUtils method instantiate.
/**
* Instantiate the given class rethrowing any exceptions as {@link InstantiationException}.
*
* @param type The type
* @param argTypes The argument types
* @param args The values of arguments
* @param <T> The generic type
* @return The instantiated instance
* @throws InstantiationException When an error occurs
* @since 3.0.0
*/
public static <T> T instantiate(Class<T> type, Class<?>[] argTypes, Object... args) {
try {
return BeanIntrospector.SHARED.findIntrospection(type).map(bi -> bi.instantiate(args)).orElseGet(() -> {
try {
Logger log = ClassUtils.REFLECTION_LOGGER;
if (log.isDebugEnabled()) {
log.debug("Reflectively instantiating type: " + type);
}
final Constructor<T> declaredConstructor = type.getDeclaredConstructor(argTypes);
declaredConstructor.setAccessible(true);
return declaredConstructor.newInstance(args);
} catch (Throwable e) {
throw new InstantiationException("Could not instantiate type [" + type.getName() + "]: " + e.getMessage(), e);
}
});
} catch (Throwable e) {
throw new InstantiationException("Could not instantiate type [" + type.getName() + "]: " + e.getMessage(), e);
}
}
Aggregations