All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.weld.manager.InjectionTargetFactoryImpl Maven / Gradle / Ivy

There is a newer version: 3.0.0.Alpha1
Show newest version
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2013, Red Hat, Inc., and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jboss.weld.manager;

import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.Decorator;
import javax.enterprise.inject.spi.InjectionTarget;
import javax.enterprise.inject.spi.Interceptor;

import org.jboss.weld.annotated.enhanced.EnhancedAnnotatedType;
import org.jboss.weld.bean.SessionBean;
import org.jboss.weld.ejb.InternalEjbDescriptor;
import org.jboss.weld.exceptions.IllegalArgumentException;
import org.jboss.weld.injection.producer.AbstractInstantiator;
import org.jboss.weld.injection.producer.BasicInjectionTarget;
import org.jboss.weld.injection.producer.BeanInjectionTarget;
import org.jboss.weld.injection.producer.DecoratorInjectionTarget;
import org.jboss.weld.injection.producer.DefaultInstantiator;
import org.jboss.weld.injection.producer.InjectionTargetService;
import org.jboss.weld.injection.producer.NonProducibleInjectionTarget;
import org.jboss.weld.injection.producer.SubclassedComponentInstantiator;
import org.jboss.weld.injection.producer.ejb.SessionBeanInjectionTarget;
import org.jboss.weld.injection.spi.InjectionServices;
import org.jboss.weld.logging.BeanLogger;
import org.jboss.weld.manager.api.WeldInjectionTarget;
import org.jboss.weld.manager.api.WeldInjectionTargetFactory;
import org.jboss.weld.resources.ClassTransformer;
import org.jboss.weld.util.Beans;
import org.jboss.weld.util.reflection.Reflections;

/**
 * Factory capable of producing {@link InjectionTarget} implementations for a given combination of {@link AnnotatedType} and
 * {@link Bean} objects.
 *
 * @author Jozef Hartinger
 *
 * @param 
 */
public class InjectionTargetFactoryImpl implements WeldInjectionTargetFactory {

    private final BeanManagerImpl manager;
    private final EnhancedAnnotatedType type;
    private final InjectionTargetService injectionTargetService;
    private final InjectionServices injectionServices;

    protected InjectionTargetFactoryImpl(AnnotatedType type, BeanManagerImpl manager) {
        this.manager = manager;
        this.type = manager.getServices().get(ClassTransformer.class).getEnhancedAnnotatedType(type, manager.getId());
        this.injectionTargetService = manager.getServices().get(InjectionTargetService.class);
        this.injectionServices = manager.getServices().get(InjectionServices.class);
    }

    @Override
    public WeldInjectionTarget createInjectionTarget(Bean bean) {
        return createInjectionTarget(bean, false);
    }

    @Override
    public WeldInjectionTarget createInterceptorInjectionTarget() {
        return createInjectionTarget(null, true);
    }

    private WeldInjectionTarget createInjectionTarget(Bean bean, boolean interceptor) {
        try {
            return validate(createInjectionTarget(type, bean, interceptor));
        } catch (Throwable e) {
            throw new IllegalArgumentException(e);
        }
    }

    public BasicInjectionTarget createInjectionTarget(EnhancedAnnotatedType type, Bean bean, boolean interceptor) {
        BasicInjectionTarget injectionTarget = chooseInjectionTarget(type, bean, interceptor);
        /*
         * Every InjectionTarget, regardless whether it's used within Weld's Bean implementation or requested from extension has
         * to be initialized after beans (interceptors) are deployed.
         */
        initialize(injectionTarget);
        postProcess(injectionTarget);
        return injectionTarget;
    }

    private BasicInjectionTarget chooseInjectionTarget(EnhancedAnnotatedType type, Bean bean, boolean interceptor) {
        if (bean instanceof Decorator || type.isAnnotationPresent(javax.decorator.Decorator.class)) {
            return new DecoratorInjectionTarget(type, bean, manager);
        }
        if (type.isAbstract()) {
            if (type.getJavaClass().isInterface()) {
                throw BeanLogger.LOG.injectionTargetCannotBeCreatedForInterface(type);
            }
            BeanLogger.LOG.injectionTargetCreatedForAbstractClass(type.getJavaClass());
            return new NonProducibleInjectionTarget(type, bean, manager);
        }
        if (Reflections.isNonStaticInnerClass(type.getJavaClass())) {
            BeanLogger.LOG.injectionTargetCreatedForNonStaticInnerClass(type.getJavaClass());
            return new NonProducibleInjectionTarget(type, bean, manager);
        }
        if (Beans.getBeanConstructor(type) == null) {
            if (bean != null) {
                throw BeanLogger.LOG.injectionTargetCreatedForClassWithoutAppropriateConstructorException(type.getJavaClass());
            }
            BeanLogger.LOG.injectionTargetCreatedForClassWithoutAppropriateConstructor(type.getJavaClass());
            return new NonProducibleInjectionTarget(type, null, manager);
        }
        if (bean instanceof SessionBean) {
            return SessionBeanInjectionTarget.of(type, (SessionBean) bean, manager);
        }
        if (bean instanceof Interceptor || type.isAnnotationPresent(javax.interceptor.Interceptor.class)) {
            return BeanInjectionTarget.forCdiInterceptor(type, bean, manager);
        }
        if (interceptor) {
            return BasicInjectionTarget.createNonCdiInterceptor(type, manager);
        }
        return BeanInjectionTarget.createDefault(type, bean, manager);
    }

    protected InjectionTarget createMessageDrivenInjectionTarget(InternalEjbDescriptor descriptor) {
        EnhancedAnnotatedType implementationClass = Beans.getEjbImplementationClass(descriptor, manager, type);

        AbstractInstantiator instantiator = null;
        if (type.equals(implementationClass)) {
            instantiator = new DefaultInstantiator(type, null, manager);
        } else {
            // Session bean subclassed by the EJB container
            instantiator = SubclassedComponentInstantiator.forSubclassedEjb(type, implementationClass, null, manager);
        }
        return prepareInjectionTarget(BasicInjectionTarget.createDefault(type, null, manager, instantiator));
    }

    private BasicInjectionTarget initialize(BasicInjectionTarget injectionTarget) {
        injectionTargetService.addInjectionTargetToBeInitialized(type, injectionTarget);
        return injectionTarget;
    }

    private > I validate(I injectionTarget) {
        injectionTargetService.validateProducer(injectionTarget);
        return injectionTarget;
    }

    private void postProcess(InjectionTarget injectionTarget) {
        if (injectionServices != null) {
            injectionServices.registerInjectionTarget(injectionTarget, type.slim());
        }
    }

    /*
     * Just a shortcut for calling validate, initialize and postProcess
     */
    private BasicInjectionTarget prepareInjectionTarget(BasicInjectionTarget injectionTarget) {
        try {
            postProcess(initialize(validate(injectionTarget)));
            return injectionTarget;
        } catch (Throwable e) {
            throw new IllegalArgumentException(e);
        }
    }

    @Override
    public WeldInjectionTarget createNonProducibleInjectionTarget() {
        return prepareInjectionTarget(new NonProducibleInjectionTarget(type, null, manager));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy