Search in sources :

Example 1 with BeanInstantiationException

use of cn.taketoday.beans.BeanInstantiationException in project today-infrastructure 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 2 with BeanInstantiationException

use of cn.taketoday.beans.BeanInstantiationException in project today-infrastructure 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 3 with BeanInstantiationException

use of cn.taketoday.beans.BeanInstantiationException in project today-infrastructure 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 4 with BeanInstantiationException

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

the class ParserStrategyUtils method instantiateClass.

/**
 * 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 instantiateClass(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 5 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,
 * creating it via the given factory method.
 *
 * @param merged the bean definition
 * @param owner the owning BeanFactory
 * @param factoryBean the factory bean instance to call the factory method on,
 * or {@code null} in case of a static factory method
 * @param factoryMethod the factory method to use
 * @param args the factory method arguments to apply
 * @return a bean instance for this bean definition
 * @throws BeansException if the instantiation attempt failed
 */
public Object instantiate(RootBeanDefinition merged, @Nullable String beanName, BeanFactory owner, @Nullable Object factoryBean, final Method factoryMethod, Object... args) throws BeansException {
    try {
        ReflectionUtils.makeAccessible(factoryMethod);
        Method priorInvokedFactoryMethod = currentlyInvokedFactoryMethod.get();
        try {
            currentlyInvokedFactoryMethod.set(factoryMethod);
            Object result = factoryMethod.invoke(factoryBean, args);
            if (result == null) {
                result = NullValue.INSTANCE;
            }
            return result;
        } finally {
            if (priorInvokedFactoryMethod != null) {
                currentlyInvokedFactoryMethod.set(priorInvokedFactoryMethod);
            } else {
                currentlyInvokedFactoryMethod.remove();
            }
        }
    } catch (IllegalArgumentException ex) {
        throw new BeanInstantiationException(factoryMethod, "Illegal arguments to factory method '" + factoryMethod.getName() + "'; " + "args: " + StringUtils.arrayToCommaDelimitedString(args), ex);
    } catch (IllegalAccessException ex) {
        throw new BeanInstantiationException(factoryMethod, "Cannot access factory method '" + factoryMethod.getName() + "'; is it public?", ex);
    } catch (InvocationTargetException ex) {
        String msg = "Factory method '" + factoryMethod.getName() + "' threw exception";
        if (merged.getFactoryBeanName() != null && owner instanceof ConfigurableBeanFactory && ((ConfigurableBeanFactory) owner).isCurrentlyInCreation(merged.getFactoryBeanName())) {
            msg = "Circular reference involving containing bean '" + merged.getFactoryBeanName() + "' - consider " + "declaring the factory method as static for independence from its containing instance. " + msg;
        }
        throw new BeanInstantiationException(factoryMethod, msg, ex.getTargetException());
    }
}
Also used : ConfigurableBeanFactory(cn.taketoday.beans.factory.config.ConfigurableBeanFactory) BeanInstantiationException(cn.taketoday.beans.BeanInstantiationException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

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