Search in sources :

Example 1 with PatternResourceLoader

use of cn.taketoday.core.io.PatternResourceLoader 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 2 with PatternResourceLoader

use of cn.taketoday.core.io.PatternResourceLoader 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 3 with PatternResourceLoader

use of cn.taketoday.core.io.PatternResourceLoader 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)3 ConfigurableBeanFactory (cn.taketoday.beans.factory.config.ConfigurableBeanFactory)3 BeanDefinitionRegistry (cn.taketoday.beans.factory.support.BeanDefinitionRegistry)3 Environment (cn.taketoday.core.env.Environment)3 PatternResourceLoader (cn.taketoday.core.io.PatternResourceLoader)3