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