Search in sources :

Example 1 with ConstructorConfig

use of org.jboss.as.pojo.descriptor.ConstructorConfig in project wildfly by wildfly.

the class BeanUtils method instantiateBean.

/**
     * Instantiate bean.
     *
     * @param beanConfig the bean metadata config, must not be null
     * @param beanInfo the bean info, can be null if enough info
     * @param index the reflection index, must not be null
     * @param module the current CL module, must not be null
     * @return new bean instance
     * @throws Throwable for any error
     */
public static Object instantiateBean(BeanMetaDataConfig beanConfig, BeanInfo beanInfo, DeploymentReflectionIndex index, Module module) throws Throwable {
    Joinpoint instantiateJoinpoint = null;
    ValueConfig[] parameters = new ValueConfig[0];
    String[] types = Configurator.NO_PARAMS_TYPES;
    ConstructorConfig ctorConfig = beanConfig.getConstructor();
    if (ctorConfig != null) {
        parameters = ctorConfig.getParameters();
        types = Configurator.getTypes(parameters);
        String factoryClass = ctorConfig.getFactoryClass();
        FactoryConfig factory = ctorConfig.getFactory();
        if (factoryClass != null || factory != null) {
            String factoryMethod = ctorConfig.getFactoryMethod();
            if (factoryMethod == null)
                throw PojoLogger.ROOT_LOGGER.missingFactoryMethod(beanConfig);
            if (factoryClass != null) {
                // static factory
                Class<?> factoryClazz = Class.forName(factoryClass, false, module.getClassLoader());
                Method method = Configurator.findMethod(index, factoryClazz, factoryMethod, types, true, true, true);
                MethodJoinpoint mj = new MethodJoinpoint(method);
                // null, since this is static call
                mj.setTarget(new ImmediateValue<Object>(null));
                mj.setParameters(parameters);
                instantiateJoinpoint = mj;
            } else if (factory != null) {
                ReflectionJoinpoint rj = new ReflectionJoinpoint(factory.getBeanInfo(), factoryMethod, types);
                // null type is ok, as this should be plain injection
                rj.setTarget(new ImmediateValue<Object>(factory.getValue(null)));
                rj.setParameters(parameters);
                instantiateJoinpoint = rj;
            }
        }
    }
    // plain bean's ctor
    if (instantiateJoinpoint == null) {
        if (beanInfo == null)
            throw new StartException(PojoLogger.ROOT_LOGGER.missingBeanInfo(beanConfig));
        Constructor ctor = (types.length == 0) ? beanInfo.getConstructor() : beanInfo.findConstructor(types);
        ConstructorJoinpoint constructorJoinpoint = new ConstructorJoinpoint(ctor);
        constructorJoinpoint.setParameters(parameters);
        instantiateJoinpoint = constructorJoinpoint;
    }
    return instantiateJoinpoint.dispatch();
}
Also used : Constructor(java.lang.reflect.Constructor) ConstructorConfig(org.jboss.as.pojo.descriptor.ConstructorConfig) FactoryConfig(org.jboss.as.pojo.descriptor.FactoryConfig) Method(java.lang.reflect.Method) ImmediateValue(org.jboss.msc.value.ImmediateValue) ValueConfig(org.jboss.as.pojo.descriptor.ValueConfig) StartException(org.jboss.msc.service.StartException)

Aggregations

Constructor (java.lang.reflect.Constructor)1 Method (java.lang.reflect.Method)1 ConstructorConfig (org.jboss.as.pojo.descriptor.ConstructorConfig)1 FactoryConfig (org.jboss.as.pojo.descriptor.FactoryConfig)1 ValueConfig (org.jboss.as.pojo.descriptor.ValueConfig)1 StartException (org.jboss.msc.service.StartException)1 ImmediateValue (org.jboss.msc.value.ImmediateValue)1