
org.jboss.weld.manager.InjectionTargetFactoryImpl Maven / Gradle / Ivy
/*
* 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 jakarta.enterprise.inject.spi.AnnotatedType;
import jakarta.enterprise.inject.spi.Bean;
import jakarta.enterprise.inject.spi.Decorator;
import jakarta.enterprise.inject.spi.InjectionTarget;
import jakarta.enterprise.inject.spi.Interceptor;
import jakarta.enterprise.inject.spi.configurator.AnnotatedTypeConfigurator;
import org.jboss.weld.annotated.enhanced.EnhancedAnnotatedType;
import org.jboss.weld.bean.SessionBean;
import org.jboss.weld.bootstrap.events.configurator.AnnotatedTypeConfiguratorImpl;
import org.jboss.weld.ejb.spi.EjbDescriptor;
import org.jboss.weld.exceptions.IllegalArgumentException;
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.InjectionTargetService;
import org.jboss.weld.injection.producer.NonProducibleInjectionTarget;
import org.jboss.weld.injection.spi.InjectionServices;
import org.jboss.weld.logging.BeanManagerLogger;
import org.jboss.weld.manager.api.WeldInjectionTarget;
import org.jboss.weld.manager.api.WeldInjectionTargetFactory;
import org.jboss.weld.module.EjbSupport;
import org.jboss.weld.resources.ClassTransformer;
import org.jboss.weld.util.InjectionTargets;
/**
* 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 originalAnnotatedType;
private final InjectionTargetService injectionTargetService;
private final InjectionServices injectionServices;
private volatile EnhancedAnnotatedType annotatedType;
private volatile AnnotatedTypeConfiguratorImpl configurator;
protected InjectionTargetFactoryImpl(AnnotatedType type, BeanManagerImpl manager) {
this.manager = manager;
this.originalAnnotatedType = 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 {
initAnnotatedType();
return validate(createInjectionTarget(annotatedType, 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;
}
@Override
public synchronized AnnotatedTypeConfigurator configure() {
if (annotatedType != null) {
BeanManagerLogger.LOG.unableToConfigureInjectionTargetFactory(annotatedType);
throw new IllegalStateException();
}
if (configurator == null) {
configurator = new AnnotatedTypeConfiguratorImpl<>(originalAnnotatedType);
}
return configurator;
}
private synchronized void initAnnotatedType() {
if (annotatedType != null) {
return;
}
if (configurator != null) {
AnnotatedType configuredType = configurator.complete();
annotatedType = manager.getServices().get(ClassTransformer.class).getEnhancedAnnotatedType(configuredType,
manager.getId());
} else {
annotatedType = originalAnnotatedType;
}
}
private BasicInjectionTarget chooseInjectionTarget(EnhancedAnnotatedType type, Bean bean, boolean interceptor) {
if (bean instanceof Decorator> || type.isAnnotationPresent(jakarta.decorator.Decorator.class)) {
return new DecoratorInjectionTarget(type, bean, manager);
}
NonProducibleInjectionTarget nonProducible = InjectionTargets.createNonProducibleInjectionTarget(type, bean,
manager);
if (nonProducible != null) {
return nonProducible;
}
if (bean instanceof SessionBean>) {
return manager.getServices().get(EjbSupport.class).createSessionBeanInjectionTarget(type, (SessionBean) bean,
manager);
}
if (bean instanceof Interceptor> || type.isAnnotationPresent(jakarta.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(EjbDescriptor descriptor) {
return prepareInjectionTarget(
manager.getServices().get(EjbSupport.class).createMessageDrivenInjectionTarget(originalAnnotatedType,
descriptor, manager));
}
private BasicInjectionTarget initialize(BasicInjectionTarget injectionTarget) {
injectionTargetService.addInjectionTargetToBeInitialized(originalAnnotatedType, injectionTarget);
return injectionTarget;
}
private > I validate(I injectionTarget) {
injectionTargetService.validateProducer(injectionTarget);
return injectionTarget;
}
private void postProcess(InjectionTarget injectionTarget) {
if (injectionServices != null) {
injectionServices.registerInjectionTarget(injectionTarget, originalAnnotatedType.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(NonProducibleInjectionTarget.create(originalAnnotatedType, null, manager));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy