Skip to content

Commit

Permalink
WELD-1345 Isolate InjectionTarget.inject() logic into Injectors
Browse files Browse the repository at this point in the history
  • Loading branch information
jharting committed Mar 6, 2013
1 parent c0bb725 commit a40bade
Show file tree
Hide file tree
Showing 10 changed files with 290 additions and 68 deletions.
Expand Up @@ -40,19 +40,16 @@
import org.jboss.weld.annotated.enhanced.EnhancedAnnotatedMethod;
import org.jboss.weld.annotated.enhanced.EnhancedAnnotatedType;
import org.jboss.weld.annotated.runtime.RuntimeAnnotatedMembers;
import org.jboss.weld.annotated.slim.SlimAnnotatedType;
import org.jboss.weld.bean.CustomDecoratorWrapper;
import org.jboss.weld.bean.DecoratorImpl;
import org.jboss.weld.exceptions.DefinitionException;
import org.jboss.weld.exceptions.DeploymentException;
import org.jboss.weld.exceptions.IllegalStateException;
import org.jboss.weld.exceptions.WeldException;
import org.jboss.weld.injection.FieldInjectionPoint;
import org.jboss.weld.injection.InjectionPointFactory;
import org.jboss.weld.injection.MethodInjectionPoint;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.resources.ClassTransformer;
import org.jboss.weld.util.BeanMethods;
import org.jboss.weld.util.InjectionPoints;
import org.jboss.weld.util.collections.WeldCollections;

/**
Expand All @@ -62,31 +59,28 @@
public abstract class AbstractInjectionTarget<T> extends AbstractProducer<T> implements InjectionTarget<T> {

protected final BeanManagerImpl beanManager;
private final AnnotatedType<T> type;
private final List<Set<FieldInjectionPoint<?, ?>>> injectableFields;
private final List<Set<MethodInjectionPoint<?, ?>>> initializerMethods;
private final SlimAnnotatedType<T> type;
private final List<AnnotatedMethod<? super T>> postConstructMethods;
private final List<AnnotatedMethod<? super T>> preDestroyMethods;
private final Set<InjectionPoint> injectionPoints;
private final Bean<T> bean;

// Instantiation
private Instantiator<T> instantiator;
private final Injector<T> injector;

public AbstractInjectionTarget(EnhancedAnnotatedType<T> type, Bean<T> bean, BeanManagerImpl beanManager) {
this.beanManager = beanManager;
this.type = type.slim();
Set<InjectionPoint> injectionPoints = new HashSet<InjectionPoint>();

this.bean = bean;
this.injectableFields = InjectionPointFactory.instance().getFieldInjectionPoints(bean, type, beanManager);
injectionPoints.addAll(InjectionPoints.flattenInjectionPoints(this.injectableFields));
this.initializerMethods = BeanMethods.getInitializerMethods(bean, type, beanManager);
injectionPoints.addAll(InjectionPoints.flattenParameterInjectionPoints(initializerMethods));
this.postConstructMethods = initPostConstructMethods(type);
this.preDestroyMethods = initPreDestroyMethods(type);

checkType(type);
this.injector = initInjector(type, bean, beanManager);
this.injector.registerInjectionPoints(injectionPoints);
this.instantiator = initInstantiator(type, bean, beanManager, injectionPoints);
this.injectionPoints = WeldCollections.immutableSet(injectionPoints);
checkDelegateInjectionPoints();
Expand Down Expand Up @@ -161,6 +155,11 @@ public T produce(CreationalContext<T> ctx) {
return instance;
}

@Override
public void inject(T instance, CreationalContext<T> ctx) {
injector.inject(instance, ctx, beanManager, type, this);
}

public void postConstruct(T instance) {
for (AnnotatedMethod<? super T> method : postConstructMethods) {
if (method != null) {
Expand Down Expand Up @@ -203,14 +202,6 @@ protected BeanManagerImpl getBeanManager() {
return beanManager;
}

public List<Set<FieldInjectionPoint<?, ?>>> getInjectableFields() {
return injectableFields;
}

public List<Set<MethodInjectionPoint<?, ?>>> getInitializerMethods() {
return initializerMethods;
}

public Instantiator<T> getInstantiator() {
return instantiator;
}
Expand Down Expand Up @@ -253,11 +244,19 @@ protected void initializeAfterBeanDiscovery(EnhancedAnnotatedType<T> annotatedTy
*/
protected abstract Instantiator<T> initInstantiator(EnhancedAnnotatedType<T> type, Bean<T> bean, BeanManagerImpl beanManager, Set<InjectionPoint> injectionPoints);

protected Injector<T> initInjector(EnhancedAnnotatedType<T> type, Bean<T> bean, BeanManagerImpl beanManager) {
return new DefaultInjector<T>(type, bean, beanManager);
}

@Override
public AnnotatedType<T> getAnnotated() {
return type;
}

public Injector<T> getInjector() {
return injector;
}

@Override
public String toString() {
if (getBean() == null) {
Expand Down
Expand Up @@ -19,10 +19,10 @@
import static org.jboss.weld.logging.Category.BEAN;
import static org.jboss.weld.logging.LoggerFactory.loggerFactory;
import static org.jboss.weld.logging.messages.BeanMessage.CIRCULAR_CALL;
import static org.jboss.weld.logging.messages.BeanMessage.DECLARING_BEAN_MISSING;
import static org.jboss.weld.logging.messages.BeanMessage.PRODUCER_METHOD_CANNOT_HAVE_A_WILDCARD_RETURN_TYPE;
import static org.jboss.weld.logging.messages.BeanMessage.PRODUCER_METHOD_WITH_TYPE_VARIABLE_RETURN_TYPE_MUST_BE_DEPENDENT;
import static org.jboss.weld.logging.messages.BeanMessage.RETURN_TYPE_MUST_BE_CONCRETE;
import static org.jboss.weld.logging.messages.BeanMessage.DECLARING_BEAN_MISSING;

import java.io.Serializable;
import java.lang.reflect.Member;
Expand All @@ -43,7 +43,6 @@
import org.jboss.weld.exceptions.DefinitionException;
import org.jboss.weld.injection.CurrentInjectionPoint;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.util.Beans;
import org.jboss.weld.util.reflection.Reflections;
import org.slf4j.cal10n.LocLogger;

Expand Down
Expand Up @@ -21,18 +21,15 @@
import java.util.List;
import java.util.Set;

import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.Decorator;
import javax.enterprise.inject.spi.InjectionPoint;

import org.jboss.weld.annotated.enhanced.EnhancedAnnotatedType;
import org.jboss.weld.injection.InjectionContextImpl;
import org.jboss.weld.injection.InjectionPointFactory;
import org.jboss.weld.injection.ResourceInjectionPoint;
import org.jboss.weld.interceptor.util.InterceptionUtils;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.util.Beans;
import org.jboss.weld.util.collections.ArraySet;

/**
Expand Down Expand Up @@ -61,14 +58,9 @@ protected Instantiator<T> initInstantiator(EnhancedAnnotatedType<T> type, Bean<T
return instantiator;
}

public void inject(final T instance, final CreationalContext<T> ctx) {
new InjectionContextImpl<T>(beanManager, this, getType(), instance) {
public void proceed() {
Beans.injectEEFields(resourceInjectionPoints, instance, ctx);
Beans.injectFieldsAndInitializers(instance, ctx, beanManager, getInjectableFields(), getInitializerMethods());
}

}.run();
@Override
protected Injector<T> initInjector(EnhancedAnnotatedType<T> type, Bean<T> bean, BeanManagerImpl beanManager) {
return new ResourceInjector<T>(type, bean, beanManager);
}

public void postConstruct(T instance) {
Expand Down
@@ -0,0 +1,84 @@
/*
* 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.injection.producer;

import java.util.List;
import java.util.Set;

import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.InjectionTarget;
import javax.inject.Inject;

import org.jboss.weld.annotated.enhanced.EnhancedAnnotatedType;
import org.jboss.weld.annotated.slim.SlimAnnotatedType;
import org.jboss.weld.injection.FieldInjectionPoint;
import org.jboss.weld.injection.InjectionContextImpl;
import org.jboss.weld.injection.InjectionPointFactory;
import org.jboss.weld.injection.MethodInjectionPoint;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.util.BeanMethods;
import org.jboss.weld.util.Beans;
import org.jboss.weld.util.InjectionPoints;

/**
* Injector implementation that injects {@link Inject}-annotated fields and calls CDI initializer methods.
*
* @author Jozef Hartinger
*
* @param <T>
*/
public class DefaultInjector<T> implements Injector<T> {

private final List<Set<FieldInjectionPoint<?, ?>>> injectableFields;
private final List<Set<MethodInjectionPoint<?, ?>>> initializerMethods;

protected DefaultInjector(EnhancedAnnotatedType<T> type, Bean<T> bean, BeanManagerImpl beanManager) {
this.injectableFields = InjectionPointFactory.instance().getFieldInjectionPoints(bean, type, beanManager);
this.initializerMethods = BeanMethods.getInitializerMethods(bean, type, beanManager);
}

@Override
public void registerInjectionPoints(Set<InjectionPoint> injectionPoints) {
injectionPoints.addAll(InjectionPoints.flattenInjectionPoints(this.injectableFields));
injectionPoints.addAll(InjectionPoints.flattenParameterInjectionPoints(this.initializerMethods));
}

@Override
public void inject(final T instance, final CreationalContext<T> ctx, final BeanManagerImpl manager, SlimAnnotatedType<T> type, InjectionTarget<T> injectionTarget) {
new InjectionContextImpl<T>(manager, injectionTarget, type, instance) {
public void proceed() {
inject(instance, ctx, manager);
}
}.run();
}

public void inject(final T instance, final CreationalContext<T> ctx, BeanManagerImpl manager) {
Beans.injectFieldsAndInitializers(instance, ctx, manager, injectableFields, initializerMethods);
}

@Override
public List<Set<FieldInjectionPoint<?, ?>>> getInjectableFields() {
return injectableFields;
}

@Override
public List<Set<MethodInjectionPoint<?, ?>>> getInitializerMethods() {
return initializerMethods;
}
}
54 changes: 54 additions & 0 deletions impl/src/main/java/org/jboss/weld/injection/producer/Injector.java
@@ -0,0 +1,54 @@
/*
* 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.injection.producer;

import java.util.List;
import java.util.Set;

import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.InjectionTarget;

import org.jboss.weld.annotated.slim.SlimAnnotatedType;
import org.jboss.weld.injection.FieldInjectionPoint;
import org.jboss.weld.injection.MethodInjectionPoint;
import org.jboss.weld.manager.BeanManagerImpl;

/**
* Implementations of this interface are capable of performing field/method injection as defined in
* {@link InjectionTarget#inject(Object, CreationalContext)}.
*
* @author Jozef Hartinger
*
* @param <T>
*/
public interface Injector<T> {

void inject(T instance, CreationalContext<T> ctx, BeanManagerImpl manager, SlimAnnotatedType<T> type, InjectionTarget<T> injectionTarget);

/**
* Add field/parameter injection points to the set of injection points of an InjectionTarget. The resulting set is returned
* from {@link InjectionTarget#getInjectionPoints()}.
*
* @param injectionPoints
*/
void registerInjectionPoints(Set<InjectionPoint> injectionPoints);

List<Set<MethodInjectionPoint<?, ?>>> getInitializerMethods();

List<Set<FieldInjectionPoint<?, ?>>> getInjectableFields();
}
@@ -0,0 +1,62 @@
/*
* 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.injection.producer;

import static org.jboss.weld.util.collections.WeldCollections.immutableSet;

import java.util.Set;

import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;

import org.jboss.weld.annotated.enhanced.EnhancedAnnotatedType;
import org.jboss.weld.injection.InjectionPointFactory;
import org.jboss.weld.injection.ResourceInjectionPoint;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.util.Beans;
import org.jboss.weld.util.collections.ArraySet;

/**
* {@link Injector} that adds support for resource field injection.
*
* @see DefaultInjector
*
* @author Jozef Hartinger
*
* @param <T>
*/
public class ResourceInjector<T> extends DefaultInjector<T> {

private final Set<ResourceInjectionPoint<?, ?>> resourceInjectionPoints;

protected ResourceInjector(EnhancedAnnotatedType<T> type, Bean<T> bean, BeanManagerImpl beanManager) {
super(type, bean, beanManager);
Set<ResourceInjectionPoint<?, ?>> resourceInjectionPoints = new ArraySet<ResourceInjectionPoint<?, ?>>();
resourceInjectionPoints.addAll(InjectionPointFactory.silentInstance().getEjbInjectionPoints(bean, type, beanManager));
resourceInjectionPoints.addAll(InjectionPointFactory.silentInstance().getPersistenceContextInjectionPoints(bean, type, beanManager));
resourceInjectionPoints.addAll(InjectionPointFactory.silentInstance().getPersistenceUnitInjectionPoints(bean, type, beanManager));
resourceInjectionPoints.addAll(InjectionPointFactory.silentInstance().getResourceInjectionPoints(bean, type, beanManager));
resourceInjectionPoints.addAll(InjectionPointFactory.silentInstance().getWebServiceRefInjectionPoints(bean, type, beanManager));
this.resourceInjectionPoints = immutableSet(resourceInjectionPoints);
}

@Override
public void inject(T instance, CreationalContext<T> ctx, BeanManagerImpl manager) {
Beans.injectEEFields(resourceInjectionPoints, instance, ctx);
super.inject(instance, ctx, manager);
}
}

0 comments on commit a40bade

Please sign in to comment.