Search in sources :

Example 6 with BeanInstantiationException

use of cn.taketoday.beans.BeanInstantiationException in project today-framework by TAKETODAY.

the class InstantiationStrategy method instantiate.

/**
 * Return an instance of the bean with the given name in this factory.
 *
 * @param bd the bean definition
 * @param beanName the name of the bean when it is created in this context.
 * The name can be {@code null} if we are autowiring a bean which doesn't
 * belong to the factory.
 * @param owner the owning BeanFactory
 * @return a bean instance for this bean definition
 * @throws BeansException if the instantiation attempt failed
 */
public Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner) throws BeansException {
    // Don't override the class with CGLIB if no overrides.
    if (bd.hasMethodOverrides()) {
        // Must generate CGLIB subclass.
        return instantiateWithMethodInjection(bd, beanName, owner);
    } else {
        Constructor<?> constructorToUse;
        synchronized (bd.constructorArgumentLock) {
            constructorToUse = (Constructor<?>) bd.resolvedConstructorOrFactoryMethod;
            if (constructorToUse == null) {
                final Class<?> clazz = bd.getBeanClass();
                if (clazz.isInterface()) {
                    throw new BeanInstantiationException(clazz, "Specified class is an interface");
                }
                try {
                    constructorToUse = clazz.getDeclaredConstructor();
                    bd.resolvedConstructorOrFactoryMethod = constructorToUse;
                } catch (Throwable ex) {
                    throw new BeanInstantiationException(clazz, "No default constructor found", ex);
                }
            }
        }
        return BeanUtils.newInstance(constructorToUse);
    }
}
Also used : BeanInstantiationException(cn.taketoday.beans.BeanInstantiationException)

Example 7 with BeanInstantiationException

use of cn.taketoday.beans.BeanInstantiationException in project today-framework by TAKETODAY.

the class ParserStrategyUtils method createInstance.

private static Object createInstance(Class<?> clazz, Environment environment, ResourceLoader resourceLoader, BeanDefinitionRegistry registry, @Nullable ClassLoader classLoader) {
    Constructor<?>[] constructors = clazz.getDeclaredConstructors();
    if (constructors.length == 1 && constructors[0].getParameterCount() > 0) {
        try {
            Constructor<?> constructor = constructors[0];
            Object[] args = resolveArgs(constructor, environment, resourceLoader, registry, classLoader);
            ReflectionUtils.makeAccessible(constructor);
            return BeanUtils.newInstance(constructor, args);
        } catch (BeanInstantiationException e) {
            throw e;
        } catch (Exception ex) {
            throw new BeanInstantiationException(clazz, "No suitable constructor found", ex);
        }
    }
    return BeanUtils.newInstance(clazz);
}
Also used : Constructor(java.lang.reflect.Constructor) BeanInstantiationException(cn.taketoday.beans.BeanInstantiationException) BeanInstantiationException(cn.taketoday.beans.BeanInstantiationException)

Example 8 with BeanInstantiationException

use of cn.taketoday.beans.BeanInstantiationException in project today-framework by TAKETODAY.

the class ParserStrategyUtils method newInstance.

/**
 * Instantiate a class using an appropriate constructor and return the new
 * instance as the specified assignable type. The returned instance will
 * have {@link BeanClassLoaderAware}, {@link BeanFactoryAware},
 * {@link EnvironmentAware}, and {@link ResourceLoaderAware} contracts
 * invoked if they are implemented by the given object.
 */
@SuppressWarnings("unchecked")
static <T> T newInstance(Class<?> clazz, Class<T> assignableTo, BootstrapContext loadingContext) {
    Assert.notNull(clazz, "Class must not be null");
    Assert.isAssignable(assignableTo, clazz);
    if (clazz.isInterface()) {
        throw new BeanInstantiationException(clazz, "Specified class is an interface");
    }
    BeanDefinitionRegistry registry = loadingContext.getRegistry();
    PatternResourceLoader resourceLoader = loadingContext.getResourceLoader();
    Environment environment = loadingContext.getEnvironment();
    ClassLoader classLoader = registry instanceof ConfigurableBeanFactory ? ((ConfigurableBeanFactory) registry).getBeanClassLoader() : resourceLoader.getClassLoader();
    T instance = (T) createInstance(clazz, environment, resourceLoader, registry, classLoader);
    ParserStrategyUtils.invokeAwareMethods(instance, environment, resourceLoader, registry, classLoader);
    return instance;
}
Also used : ConfigurableBeanFactory(cn.taketoday.beans.factory.config.ConfigurableBeanFactory) BeanInstantiationException(cn.taketoday.beans.BeanInstantiationException) BeanDefinitionRegistry(cn.taketoday.beans.factory.support.BeanDefinitionRegistry) Environment(cn.taketoday.core.env.Environment) PatternResourceLoader(cn.taketoday.core.io.PatternResourceLoader)

Example 9 with BeanInstantiationException

use of cn.taketoday.beans.BeanInstantiationException in project today-framework by TAKETODAY.

the class AbstractInjectionFailureAnalyzer method getDescription.

@Nullable
private String getDescription(Throwable rootFailure) {
    UnsatisfiedDependencyException unsatisfiedDependency = findMostNestedCause(rootFailure, UnsatisfiedDependencyException.class);
    if (unsatisfiedDependency != null) {
        return getDescription(unsatisfiedDependency);
    }
    BeanInstantiationException beanInstantiationException = findMostNestedCause(rootFailure, BeanInstantiationException.class);
    if (beanInstantiationException != null) {
        return getDescription(beanInstantiationException);
    }
    return null;
}
Also used : UnsatisfiedDependencyException(cn.taketoday.beans.factory.UnsatisfiedDependencyException) BeanInstantiationException(cn.taketoday.beans.BeanInstantiationException) Nullable(cn.taketoday.lang.Nullable)

Example 10 with BeanInstantiationException

use of cn.taketoday.beans.BeanInstantiationException in project today-infrastructure by TAKETODAY.

the class ParserStrategyUtils method newInstance.

/**
 * Instantiate a class using an appropriate constructor and return the new
 * instance as the specified assignable type. The returned instance will
 * have {@link BeanClassLoaderAware}, {@link BeanFactoryAware},
 * {@link EnvironmentAware}, and {@link ResourceLoaderAware} contracts
 * invoked if they are implemented by the given object.
 */
@SuppressWarnings("unchecked")
static <T> T newInstance(Class<?> clazz, Class<T> assignableTo, BootstrapContext loadingContext) {
    Assert.notNull(clazz, "Class must not be null");
    Assert.isAssignable(assignableTo, clazz);
    if (clazz.isInterface()) {
        throw new BeanInstantiationException(clazz, "Specified class is an interface");
    }
    BeanDefinitionRegistry registry = loadingContext.getRegistry();
    PatternResourceLoader resourceLoader = loadingContext.getResourceLoader();
    Environment environment = loadingContext.getEnvironment();
    ClassLoader classLoader = registry instanceof ConfigurableBeanFactory ? ((ConfigurableBeanFactory) registry).getBeanClassLoader() : resourceLoader.getClassLoader();
    T instance = (T) createInstance(clazz, environment, resourceLoader, registry, classLoader);
    ParserStrategyUtils.invokeAwareMethods(instance, environment, resourceLoader, registry, classLoader);
    return instance;
}
Also used : ConfigurableBeanFactory(cn.taketoday.beans.factory.config.ConfigurableBeanFactory) BeanInstantiationException(cn.taketoday.beans.BeanInstantiationException) BeanDefinitionRegistry(cn.taketoday.beans.factory.support.BeanDefinitionRegistry) Environment(cn.taketoday.core.env.Environment) PatternResourceLoader(cn.taketoday.core.io.PatternResourceLoader)

Aggregations

BeanInstantiationException (cn.taketoday.beans.BeanInstantiationException)11 ConfigurableBeanFactory (cn.taketoday.beans.factory.config.ConfigurableBeanFactory)5 BeanDefinitionRegistry (cn.taketoday.beans.factory.support.BeanDefinitionRegistry)3 Environment (cn.taketoday.core.env.Environment)3 PatternResourceLoader (cn.taketoday.core.io.PatternResourceLoader)3 UnsatisfiedDependencyException (cn.taketoday.beans.factory.UnsatisfiedDependencyException)2 Nullable (cn.taketoday.lang.Nullable)2 Constructor (java.lang.reflect.Constructor)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2