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);
}
}
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);
}
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;
}
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;
}
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;
}
Aggregations