Skip to content

Commit

Permalink
WELD-1345 Major refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jharting committed Mar 6, 2013
1 parent f6ed557 commit 10b10f7
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 122 deletions.
2 changes: 1 addition & 1 deletion impl/src/main/java/org/jboss/weld/bean/ManagedBean.java
Expand Up @@ -97,7 +97,7 @@ protected static String createId(String beanType, EnhancedAnnotatedType<?> type)
protected ManagedBean(BeanAttributes<T> attributes, EnhancedAnnotatedType<T> type, String idSuffix, BeanManagerImpl beanManager) {
super(attributes, type, idSuffix, beanManager);
this.proxiable = Proxies.isTypesProxyable(getTypes());
setProducer(beanManager.getInjectionTargetFactory(getEnhancedAnnotated()).internalCreateInjectionTarget(getEnhancedAnnotated(), this));
setProducer(beanManager.getInjectionTargetFactory(getEnhancedAnnotated()).createInjectionTarget(getEnhancedAnnotated(), this));
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion impl/src/main/java/org/jboss/weld/bean/SessionBean.java
Expand Up @@ -111,7 +111,7 @@ protected static String createId(String beanType, InternalEjbDescriptor<?> ejbDe
protected SessionBean(BeanAttributes<T> attributes, EnhancedAnnotatedType<T> type, InternalEjbDescriptor<T> ejbDescriptor, String idSuffix, BeanManagerImpl manager) {
super(attributes, type, idSuffix, manager);
this.ejbDescriptor = ejbDescriptor;
setProducer(beanManager.getInjectionTargetFactory(getEnhancedAnnotated()).internalCreateInjectionTarget(getEnhancedAnnotated(), this));
setProducer(beanManager.getInjectionTargetFactory(getEnhancedAnnotated()).createInjectionTarget(getEnhancedAnnotated(), this));
}

/**
Expand Down
Expand Up @@ -38,7 +38,7 @@
import org.jboss.weld.context.WeldCreationalContext;
import org.jboss.weld.context.api.ContextualInstance;
import org.jboss.weld.exceptions.UnsupportedOperationException;
import org.jboss.weld.injection.producer.AbstractInjectionTarget;
import org.jboss.weld.injection.producer.BasicInjectionTarget;
import org.jboss.weld.injection.producer.AbstractMemberProducer;
import org.jboss.weld.serialization.spi.ContextualStore;

Expand Down Expand Up @@ -81,8 +81,8 @@ protected <T> void addDependentInstance(T instance, Contextual<T> contextual, We
if (creationalContext.getDependentInstances().isEmpty()) {
if (contextual instanceof ManagedBean<?> && ! isInterceptorOrDecorator(contextual)) {
ManagedBean<?> managedBean = (ManagedBean<?>) contextual;
if (managedBean.getProducer() instanceof AbstractInjectionTarget<?>) {
AbstractInjectionTarget<?> injectionTarget = (AbstractInjectionTarget<?>) managedBean.getProducer();
if (managedBean.getProducer() instanceof BasicInjectionTarget<?>) {
BasicInjectionTarget<?> injectionTarget = (BasicInjectionTarget<?>) managedBean.getProducer();
if (injectionTarget.getLifecycleCallbackInvoker().getPreDestroyMethods().isEmpty() && !injectionTarget.hasInterceptors()) {
// there is no @PreDestroy callback to call when destroying this dependent instance
// therefore, we do not need to keep the reference
Expand Down
11 changes: 5 additions & 6 deletions impl/src/main/java/org/jboss/weld/enums/EnumInjectionTarget.java
Expand Up @@ -25,9 +25,8 @@
import javax.enterprise.inject.spi.InjectionTarget;

import org.jboss.weld.annotated.enhanced.EnhancedAnnotatedType;
import org.jboss.weld.injection.ResourceInjectionPoint;
import org.jboss.weld.injection.WeldInjectionPoint;
import org.jboss.weld.injection.producer.DefaultInjectionTarget;
import org.jboss.weld.injection.producer.BeanInjectionTarget;
import org.jboss.weld.injection.producer.Instantiator;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.util.InjectionPoints;
Expand All @@ -41,7 +40,7 @@
* @param <T> enum type
*/

public class EnumInjectionTarget<T extends Enum<?>> extends DefaultInjectionTarget<T> {
public class EnumInjectionTarget<T extends Enum<?>> extends BeanInjectionTarget<T> {
public static <T extends Enum<?>> EnumInjectionTarget<T> of(EnhancedAnnotatedType<T> clazz, BeanManagerImpl manager) {
return new EnumInjectionTarget<T>(clazz, manager);
}
Expand All @@ -62,9 +61,9 @@ protected void checkType(EnhancedAnnotatedType<T> type) {

@Override
public void dispose(T instance) {
for (ResourceInjectionPoint<?, ?> ip : getResourceInjectionPoints()) {
ip.disinject(instance);
}
// for (ResourceInjectionPoint<?, ?> ip : getResourceInjectionPoints()) {
// ip.disinject(instance);
// }
for (InjectionPoint ip : getInjectionPoints()) {
if (ip.getAnnotated() instanceof AnnotatedField<?>) {
disinject(InjectionPoints.getWeldInjectionPoint(ip), instance);
Expand Down
Expand Up @@ -16,41 +16,36 @@
*/
package org.jboss.weld.injection.producer;

import static org.jboss.weld.logging.messages.BeanMessage.FINAL_BEAN_CLASS_WITH_DECORATORS_NOT_ALLOWED;
import static org.jboss.weld.logging.messages.BeanMessage.FINAL_BEAN_CLASS_WITH_INTERCEPTORS_NOT_ALLOWED;
import static org.jboss.weld.logging.messages.BeanMessage.NON_CONTAINER_DECORATOR;
import static org.jboss.weld.logging.messages.BeanMessage.SIMPLE_BEAN_AS_NON_STATIC_INNER_CLASS_NOT_ALLOWED;

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

import javax.enterprise.context.Dependent;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.Decorator;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.InjectionTarget;
import javax.enterprise.inject.spi.Interceptor;

import org.jboss.weld.annotated.enhanced.EnhancedAnnotatedMethod;
import org.jboss.weld.annotated.enhanced.EnhancedAnnotatedType;
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.manager.BeanManagerImpl;
import org.jboss.weld.resources.ClassTransformer;
import org.jboss.weld.util.collections.WeldCollections;

/**
* Basic {@link InjectionTarget} implementation. The implementation supports:
* <ul>
* <li>@Inject injection + initializers</li>
* <li>@PostConstruct/@PreDestroy callbacks</li>
* </ul>
*
* Interception and decoration is not supported but can be added using extension points.
*
* @author Pete Muir
* @author Jozef Hartinger
*/
public abstract class AbstractInjectionTarget<T> extends AbstractProducer<T> implements InjectionTarget<T> {
public class BasicInjectionTarget<T> extends AbstractProducer<T> implements InjectionTarget<T> {

protected final BeanManagerImpl beanManager;
private final SlimAnnotatedType<T> type;
Expand All @@ -62,7 +57,7 @@ public abstract class AbstractInjectionTarget<T> extends AbstractProducer<T> imp
private final Injector<T> injector;
private final LifecycleCallbackInvoker<T> invoker;

public AbstractInjectionTarget(EnhancedAnnotatedType<T> type, Bean<T> bean, BeanManagerImpl beanManager) {
public BasicInjectionTarget(EnhancedAnnotatedType<T> type, Bean<T> bean, BeanManagerImpl beanManager) {
this.beanManager = beanManager;
this.type = type.slim();
Set<InjectionPoint> injectionPoints = new HashSet<InjectionPoint>();
Expand All @@ -85,42 +80,6 @@ protected void checkType(EnhancedAnnotatedType<T> type) {
}
}

protected void checkDecoratedMethods(EnhancedAnnotatedType<T> type, List<Decorator<?>> decorators) {
if (type.isFinal()) {
throw new DeploymentException(FINAL_BEAN_CLASS_WITH_DECORATORS_NOT_ALLOWED, this);
}
for (Decorator<?> decorator : decorators) {
EnhancedAnnotatedType<?> decoratorClass;
if (decorator instanceof DecoratorImpl<?>) {
DecoratorImpl<?> decoratorBean = (DecoratorImpl<?>) decorator;
decoratorClass = decoratorBean.getBeanManager().getServices().get(ClassTransformer.class).getEnhancedAnnotatedType(decoratorBean.getAnnotated());
} else if (decorator instanceof CustomDecoratorWrapper<?>) {
decoratorClass = ((CustomDecoratorWrapper<?>) decorator).getEnhancedAnnotated();
} else {
throw new IllegalStateException(NON_CONTAINER_DECORATOR, decorator);
}

for (EnhancedAnnotatedMethod<?, ?> decoratorMethod : decoratorClass.getEnhancedMethods()) {
EnhancedAnnotatedMethod<?, ?> method = type.getEnhancedMethod(decoratorMethod.getSignature());
if (method != null && !method.isStatic() && !method.isPrivate() && method.isFinal()) {
throw new DeploymentException(FINAL_BEAN_CLASS_WITH_INTERCEPTORS_NOT_ALLOWED, method, decoratorMethod);
}
}
}
}

protected boolean isInterceptor() {
return (bean instanceof Interceptor<?>) || type.isAnnotationPresent(javax.interceptor.Interceptor.class);
}

protected boolean isDecorator() {
return (bean instanceof Decorator<?>) || type.isAnnotationPresent(javax.decorator.Decorator.class);
}

protected boolean isInterceptionCandidate() {
return !isInterceptor() && !isDecorator();
}

public T produce(CreationalContext<T> ctx) {
T instance = instantiator.newInstance(ctx, beanManager, null);
if (bean != null && !bean.getScope().equals(Dependent.class) && !instantiator.hasDecoratorSupport()) {
Expand Down Expand Up @@ -182,9 +141,6 @@ public boolean hasDecorators() {
}

protected void initializeAfterBeanDiscovery(EnhancedAnnotatedType<T> annotatedType) {
if (isInterceptionCandidate() && !beanManager.getInterceptorModelRegistry().containsKey(annotatedType.getJavaClass())) {
new InterceptionModelInitializer<T>(beanManager, annotatedType, getBean()).init();
}
}

/**
Expand All @@ -193,18 +149,18 @@ protected void initializeAfterBeanDiscovery(EnhancedAnnotatedType<T> annotatedTy
* {@link #initInstantiator(EnhancedAnnotatedType, Bean, BeanManagerImpl, Set)} method is supposed to register all these
* injection points within the injectionPoints set passed in as a parameter.
*/
protected abstract Instantiator<T> initInstantiator(EnhancedAnnotatedType<T> type, Bean<T> bean, BeanManagerImpl beanManager, Set<InjectionPoint> injectionPoints);
protected Instantiator<T> initInstantiator(EnhancedAnnotatedType<T> type, Bean<T> bean, BeanManagerImpl beanManager, Set<InjectionPoint> injectionPoints) {
DefaultInstantiator<T> instantiator = new DefaultInstantiator<T>(type, bean, beanManager);
injectionPoints.addAll(instantiator.getConstructor().getParameterInjectionPoints());
return instantiator;
}

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

protected LifecycleCallbackInvoker<T> initInvoker(EnhancedAnnotatedType<T> type) {
if (isInterceptor()) {
return NoopLifecycleCallbackInvoker.getInstance();
} else {
return new DefaultLifecycleCallbackInvoker<T>(type);
}
return new DefaultLifecycleCallbackInvoker<T>(type);
}

@Override
Expand Down
Expand Up @@ -16,46 +16,32 @@
*/
package org.jboss.weld.injection.producer;

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

import static org.jboss.weld.logging.messages.BeanMessage.FINAL_BEAN_CLASS_WITH_DECORATORS_NOT_ALLOWED;
import static org.jboss.weld.logging.messages.BeanMessage.FINAL_BEAN_CLASS_WITH_INTERCEPTORS_NOT_ALLOWED;
import static org.jboss.weld.logging.messages.BeanMessage.NON_CONTAINER_DECORATOR;
import java.util.List;
import java.util.Set;

import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.Decorator;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.Interceptor;

import org.jboss.weld.annotated.enhanced.EnhancedAnnotatedMethod;
import org.jboss.weld.annotated.enhanced.EnhancedAnnotatedType;
import org.jboss.weld.injection.InjectionPointFactory;
import org.jboss.weld.injection.ResourceInjectionPoint;
import org.jboss.weld.bean.CustomDecoratorWrapper;
import org.jboss.weld.bean.DecoratorImpl;
import org.jboss.weld.exceptions.DeploymentException;
import org.jboss.weld.exceptions.IllegalStateException;
import org.jboss.weld.interceptor.util.InterceptionUtils;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.util.collections.ArraySet;
import org.jboss.weld.resources.ClassTransformer;

/**
* @author Pete Muir
* @author Jozef Hartinger
*/
public class DefaultInjectionTarget<T> extends AbstractInjectionTarget<T> {

private final Set<ResourceInjectionPoint<?, ?>> resourceInjectionPoints;
public class BeanInjectionTarget<T> extends BasicInjectionTarget<T> {

public DefaultInjectionTarget(EnhancedAnnotatedType<T> type, Bean<T> bean, BeanManagerImpl beanManager) {
public BeanInjectionTarget(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
protected Instantiator<T> initInstantiator(EnhancedAnnotatedType<T> type, Bean<T> bean, BeanManagerImpl beanManager, Set<InjectionPoint> injectionPoints) {
DefaultInstantiator<T> instantiator = new DefaultInstantiator<T>(type, bean, beanManager);
injectionPoints.addAll(instantiator.getConstructor().getParameterInjectionPoints());
return instantiator;
}

@Override
Expand Down Expand Up @@ -83,8 +69,22 @@ public void dispose(T instance) {
// No-op
}

protected boolean isInterceptor() {
return (getBean() instanceof Interceptor<?>) || getType().isAnnotationPresent(javax.interceptor.Interceptor.class);
}

protected boolean isDecorator() {
return (getBean() instanceof Decorator<?>) || getType().isAnnotationPresent(javax.decorator.Decorator.class);
}

protected boolean isInterceptionCandidate() {
return !isInterceptor() && !isDecorator();
}

public void initializeAfterBeanDiscovery(EnhancedAnnotatedType<T> annotatedType) {
super.initializeAfterBeanDiscovery(annotatedType);
if (isInterceptionCandidate() && !beanManager.getInterceptorModelRegistry().containsKey(annotatedType.getJavaClass())) {
new InterceptionModelInitializer<T>(beanManager, annotatedType, getBean()).init();
}
boolean hasInterceptors = isInterceptionCandidate() && (beanManager.getInterceptorModelRegistry().containsKey(getType().getJavaClass()));

List<Decorator<?>> decorators = null;
Expand All @@ -98,7 +98,7 @@ public void initializeAfterBeanDiscovery(EnhancedAnnotatedType<T> annotatedType)

if (hasInterceptors || hasDecorators) {
if (!(getInstantiator() instanceof DefaultInstantiator<?>)) {
throw new IllegalStateException("Unexpected instantiator " + getInstantiator());
throw new java.lang.IllegalStateException("Unexpected instantiator " + getInstantiator());
}
DefaultInstantiator<T> delegate = (DefaultInstantiator<T>) getInstantiator();
setInstantiator(new SubclassedComponentInstantiator<T>(annotatedType, getBean(), delegate, beanManager));
Expand All @@ -112,7 +112,36 @@ public void initializeAfterBeanDiscovery(EnhancedAnnotatedType<T> annotatedType)
}
}

public Set<ResourceInjectionPoint<?, ?>> getResourceInjectionPoints() {
return resourceInjectionPoints;
protected void checkDecoratedMethods(EnhancedAnnotatedType<T> type, List<Decorator<?>> decorators) {
if (type.isFinal()) {
throw new DeploymentException(FINAL_BEAN_CLASS_WITH_DECORATORS_NOT_ALLOWED, this);
}
for (Decorator<?> decorator : decorators) {
EnhancedAnnotatedType<?> decoratorClass;
if (decorator instanceof DecoratorImpl<?>) {
DecoratorImpl<?> decoratorBean = (DecoratorImpl<?>) decorator;
decoratorClass = decoratorBean.getBeanManager().getServices().get(ClassTransformer.class).getEnhancedAnnotatedType(decoratorBean.getAnnotated());
} else if (decorator instanceof CustomDecoratorWrapper<?>) {
decoratorClass = ((CustomDecoratorWrapper<?>) decorator).getEnhancedAnnotated();
} else {
throw new IllegalStateException(NON_CONTAINER_DECORATOR, decorator);
}

for (EnhancedAnnotatedMethod<?, ?> decoratorMethod : decoratorClass.getEnhancedMethods()) {
EnhancedAnnotatedMethod<?, ?> method = type.getEnhancedMethod(decoratorMethod.getSignature());
if (method != null && !method.isStatic() && !method.isPrivate() && method.isFinal()) {
throw new DeploymentException(FINAL_BEAN_CLASS_WITH_INTERCEPTORS_NOT_ALLOWED, method, decoratorMethod);
}
}
}
}

@Override
protected LifecycleCallbackInvoker<T> initInvoker(EnhancedAnnotatedType<T> type) {
if (isInterceptor()) {
return NoopLifecycleCallbackInvoker.getInstance();
} else {
return new DefaultLifecycleCallbackInvoker<T>(type);
}
}
}
Expand Up @@ -48,7 +48,7 @@
*
* @param <T>
*/
public class DecoratorInjectionTarget<T> extends DefaultInjectionTarget<T> {
public class DecoratorInjectionTarget<T> extends BeanInjectionTarget<T> {

private final WeldInjectionPoint<?, ?> delegateInjectionPoint;

Expand Down
Expand Up @@ -48,7 +48,7 @@ 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) {
public DefaultInjector(EnhancedAnnotatedType<T> type, Bean<T> bean, BeanManagerImpl beanManager) {
this.injectableFields = InjectionPointFactory.instance().getFieldInjectionPoints(bean, type, beanManager);
this.initializerMethods = BeanMethods.getInitializerMethods(bean, type, beanManager);
}
Expand Down
Expand Up @@ -31,9 +31,9 @@
public class InjectionTargetInitializationContext<T> {

private final EnhancedAnnotatedType<T> enhancedAnnotatedType;
private final AbstractInjectionTarget<T> injectionTarget;
private final BasicInjectionTarget<T> injectionTarget;

public InjectionTargetInitializationContext(EnhancedAnnotatedType<T> enhancedAnnotatedType, AbstractInjectionTarget<T> injectionTarget) {
public InjectionTargetInitializationContext(EnhancedAnnotatedType<T> enhancedAnnotatedType, BasicInjectionTarget<T> injectionTarget) {
this.enhancedAnnotatedType = enhancedAnnotatedType;
this.injectionTarget = injectionTarget;
}
Expand All @@ -42,7 +42,7 @@ public void initialize() {
injectionTarget.initializeAfterBeanDiscovery(enhancedAnnotatedType);
}

public AbstractInjectionTarget<T> getInjectionTarget() {
public BasicInjectionTarget<T> getInjectionTarget() {
return injectionTarget;
}

Expand Down

0 comments on commit 10b10f7

Please sign in to comment.