Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2017-2020 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.core.reflect;
import io.micronaut.core.beans.BeanIntrospection;
import io.micronaut.core.beans.BeanIntrospector;
import io.micronaut.core.convert.ConversionContext;
import io.micronaut.core.convert.ConversionService;
import io.micronaut.core.convert.exceptions.ConversionErrorException;
import io.micronaut.core.naming.NameUtils;
import io.micronaut.core.reflect.exception.InstantiationException;
import io.micronaut.core.type.Argument;
import io.micronaut.core.util.ArgumentUtils;
import io.micronaut.core.util.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.micronaut.core.annotation.NonNull;
import java.lang.reflect.Constructor;
import java.util.*;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* Utility methods for instantiating objects.
*
* @author Graeme Rocher
* @since 1.0
*/
public class InstantiationUtils {
/**
* Try to instantiate the given class.
*
* @param name The class name
* @param classLoader The class loader to use
* @return The instantiated instance or {@link Optional#empty()}
*/
public static Optional> tryInstantiate(String name, ClassLoader classLoader) {
try {
return ClassUtils.forName(name, classLoader)
.flatMap(InstantiationUtils::tryInstantiate);
} catch (Throwable e) {
Logger log = LoggerFactory.getLogger(InstantiationUtils.class);
if (log.isDebugEnabled()) {
log.debug("Tried, but could not instantiate type: {}", name, e);
}
return Optional.empty();
}
}
/**
* 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 The generic type
* @return The instantiated instance or {@link Optional#empty()}
* @throws InstantiationException When an error occurs
*/
public static @NonNull Optional tryInstantiate(@NonNull Class type, Map propertiesMap, ConversionContext context) {
ArgumentUtils.requireNonNull("type", type);
if (propertiesMap.isEmpty()) {
return tryInstantiate(type);
}
final Supplier 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