Skip to content

Commit

Permalink
WBRI-298 WBRI-310 WBRI-327
Browse files Browse the repository at this point in the history
git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@3358 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
pmuir committed Jul 29, 2009
1 parent 6a81343 commit 8ea1021
Show file tree
Hide file tree
Showing 16 changed files with 235 additions and 157 deletions.
57 changes: 47 additions & 10 deletions impl/src/main/java/org/jboss/webbeans/BeanManagerImpl.java
Expand Up @@ -277,7 +277,7 @@ public static BeanManagerImpl newRootManager(ServiceRegistry serviceRegistry)
List<Class<?>> defaultEnabledDecoratorClasses = new ArrayList<Class<?>>();

ListMultimap<Class<? extends Annotation>, Context> contexts = Multimaps.newListMultimap(new ConcurrentHashMap<Class<? extends Annotation>, Collection<Context>>(), new Supplier<List<Context>>()
{
{

public List<Context> get()
{
Expand Down Expand Up @@ -883,22 +883,29 @@ public Object getReference(Bean<?> bean, Type beanType, CreationalContext<?> cre
return getReference(bean, creationalContext);
}

@SuppressWarnings("unchecked")
public Object getInjectableReference(InjectionPoint injectionPoint, CreationalContext<?> creationalContext)

/**
* Get a reference, registering the injection point used.
*
* @param injectionPoint the injection point to register
* @param resolvedBean the bean to get a reference to
* @param creationalContext the creationalContext
* @return
*/
public Object getReference(InjectionPoint injectionPoint, Bean<?> resolvedBean, CreationalContext<?> creationalContext)
{
boolean registerInjectionPoint = !injectionPoint.getType().equals(InjectionPoint.class);
boolean registerInjectionPoint = (injectionPoint != null && !injectionPoint.getType().equals(InjectionPoint.class));
try
{
if (registerInjectionPoint)
{
currentInjectionPoint.get().push(injectionPoint);
}
WBAnnotated<?, ?> element = ResolvableWBClass.of(injectionPoint.getType(), injectionPoint.getBindings().toArray(new Annotation[0]), this);
Bean<?> resolvedBean = getBean(element, element.getBindingsAsArray());
if (getServices().get(MetaAnnotationStore.class).getScopeModel(resolvedBean.getScopeType()).isNormal() && !Proxies.isTypeProxyable(injectionPoint.getType()))
{
throw new UnproxyableResolutionException("Attempting to inject an unproxyable normal scoped bean " + resolvedBean + " into " + injectionPoint);
}
// TODO Can we move this logic to getReference?
if (creationalContext instanceof CreationalContextImpl)
{
CreationalContextImpl<?> creationalContextImpl = (CreationalContextImpl<?>) creationalContext;
Expand All @@ -924,6 +931,14 @@ public Object getInjectableReference(InjectionPoint injectionPoint, CreationalCo
}
}
}


public Object getInjectableReference(InjectionPoint injectionPoint, CreationalContext<?> creationalContext)
{
WBAnnotated<?, ?> element = ResolvableWBClass.of(injectionPoint.getType(), injectionPoint.getBindings().toArray(new Annotation[0]), this);
Bean<?> resolvedBean = getBean(element, element.getBindingsAsArray());
return getReference(injectionPoint, resolvedBean, creationalContext);
}

/**
* Returns an instance by API type and binding types
Expand Down Expand Up @@ -1080,12 +1095,11 @@ public ServiceRegistry getServices()
}

/**
* Accesses the factory used to create each instance of InjectionPoint that
* is injected into web beans.
* The injection point being operated on for this thread
*
* @return the factory
* @return the current injection point
*/
public InjectionPoint getInjectionPoint()
public InjectionPoint getCurrentInjectionPoint()
{
if (!currentInjectionPoint.get().empty())
{
Expand All @@ -1096,6 +1110,29 @@ public InjectionPoint getInjectionPoint()
return null;
}
}

/**
* Replaces (or adds) the current injection point. If a current injection
* point exists, it will be replaced. If no current injection point exists,
* one will be added.
*
* @param injectionPoint the injection point to use
* @return the injection point added, or null if non previous existed
*/
public InjectionPoint replaceOrPushCurrentInjectionPoint(InjectionPoint injectionPoint)
{
InjectionPoint originalInjectionPoint = null;
if (!currentInjectionPoint.get().empty())
{
originalInjectionPoint = currentInjectionPoint.get().pop();
}
else
{
log.trace("No current injection point to replace #0", injectionPoint);
}
currentInjectionPoint.get().push(injectionPoint);
return originalInjectionPoint;
}

/**
*
Expand Down
45 changes: 24 additions & 21 deletions impl/src/main/java/org/jboss/webbeans/bean/AbstractBean.java
Expand Up @@ -105,17 +105,18 @@ public static Class<? extends Annotation> getDeploymentType(List<Class<? extends
// The API types
protected Set<Type> types;
// The injection points
protected Set<WBInjectionPoint<?, ?>> injectionPoints;
private Set<WBInjectionPoint<?, ?>> injectionPoints;
private Set<WBInjectionPoint<?, ?>> delegateInjectionPoints;
// If the type a primitive?
private boolean primitive;
// The Web Beans manager
protected BeanManagerImpl manager;

protected boolean _serializable;
private boolean _serializable;

private boolean initialized;

private Set<WBInjectionPoint<?, ?>> decoratesInjectionPoint;


protected boolean isInitialized()
{
Expand All @@ -131,7 +132,8 @@ public AbstractBean(BeanManagerImpl manager)
{
super(manager);
this.manager = manager;
injectionPoints = new HashSet<WBInjectionPoint<?, ?>>();
this.injectionPoints = new HashSet<WBInjectionPoint<?, ?>>();
this.delegateInjectionPoints = new HashSet<WBInjectionPoint<?,?>>();
}

/**
Expand All @@ -156,34 +158,37 @@ public void initialize(BeanDeployerEnvironment environment)
initScopeType();
initSerializable();
initProxyable();
initInjectionPoints();
initDecorates();
checkDecorates();
checkDelegateInjectionPoints();
}

protected void checkDecorates()
protected void checkDelegateInjectionPoints()
{
if (this.decoratesInjectionPoint.size() > 0)
if (this.delegateInjectionPoints.size() > 0)
{
throw new DefinitionException("Cannot place @Decorates at an injection point which is not on a Decorator " + this);
}
}

protected void initDecorates()
protected void addInjectionPoint(WBInjectionPoint<?, ?> injectionPoint)
{
this.decoratesInjectionPoint = new HashSet<WBInjectionPoint<?, ?>>();
for (WBInjectionPoint<?, ?> injectionPoint : getAnnotatedInjectionPoints())
if (injectionPoint.isAnnotationPresent(Decorates.class))
{
if (injectionPoint.isAnnotationPresent(Decorates.class))
{
this.decoratesInjectionPoint.add(injectionPoint);
}
this.delegateInjectionPoints.add(injectionPoint);
}
injectionPoints.add(injectionPoint);
}

protected void addInjectionPoints(Iterable<? extends WBInjectionPoint<?, ?>> injectionPoints)
{
for (WBInjectionPoint<?, ?> injectionPoint : injectionPoints)
{
addInjectionPoint(injectionPoint);
}
}

protected Set<WBInjectionPoint<?, ?>> getDecoratesInjectionPoint()
protected Set<WBInjectionPoint<?, ?>> getDelegateInjectionPoints()
{
return decoratesInjectionPoint;
return delegateInjectionPoints;
}

/**
Expand All @@ -205,8 +210,6 @@ protected void initBindings()
log.trace("Using binding types " + bindings + " specified by annotations");
}

protected abstract void initInjectionPoints();

protected void initDefaultBindings()
{
if (bindings.size() == 0)
Expand Down
92 changes: 46 additions & 46 deletions impl/src/main/java/org/jboss/webbeans/bean/AbstractClassBean.java
Expand Up @@ -39,6 +39,7 @@
import javax.enterprise.inject.deployment.DeploymentType;
import javax.enterprise.inject.deployment.Production;
import javax.enterprise.inject.spi.Decorator;
import javax.enterprise.inject.spi.InjectionPoint;

import org.jboss.webbeans.BeanManagerImpl;
import org.jboss.webbeans.DefinitionException;
Expand Down Expand Up @@ -113,6 +114,7 @@ protected Integer initialValue()
public void initialize(BeanDeployerEnvironment environment)
{
initInitializerMethods();
initInjectableFields();
super.initialize(environment);
checkBeanImplementation();
initDecorators();
Expand Down Expand Up @@ -149,54 +151,54 @@ protected void initProxyClassForDecoratedBean()
}
}

protected T applyDecorators(T instance, CreationalContext<T> creationalContext)
protected T applyDecorators(T instance, CreationalContext<T> creationalContext, InjectionPoint originalInjectionPoint)
{
if (hasDecorators())
List<SerializableBeanInstance<DecoratorBean<Object>, Object>> decoratorInstances = new ArrayList<SerializableBeanInstance<DecoratorBean<Object>,Object>>();
InjectionPoint ip = originalInjectionPoint;
boolean outside = decoratorStackPosition.get().intValue() == 0;
try
{
List<SerializableBeanInstance<DecoratorBean<Object>, Object>> decoratorInstances = new ArrayList<SerializableBeanInstance<DecoratorBean<Object>,Object>>();
boolean outside = decoratorStackPosition.get().intValue() == 0;
try
int i = decoratorStackPosition.get();
while (i < decorators.size())
{
int i = decoratorStackPosition.get();
while (i < decorators.size())
Decorator<?> decorator = decorators.get(i);
if (decorator instanceof DecoratorBean<?>)
{
Decorator<?> decorator = decorators.get(i);
if (decorator instanceof DecoratorBean)
{
decoratorStackPosition.set(++i);
decoratorInstances.add(new SerializableBeanInstance<DecoratorBean<Object>, Object>((DecoratorBean) decorator, getManager().getReference(decorator, Object.class, creationalContext)));
}
else
{
throw new IllegalStateException("Cannot operate on non container provided decorator " + decorator);
}
decoratorStackPosition.set(++i);

@SuppressWarnings("unchecked")
DecoratorBean<Object> decoratorBean = (DecoratorBean<Object>) decorator;

Object decoratorInstance = getManager().getReference(ip, decorator, creationalContext);
decoratorInstances.add(new SerializableBeanInstance<DecoratorBean<Object>, Object>(decoratorBean, decoratorInstance));
ip = decoratorBean.getDelegateInjectionPoint();
}
}
finally
{
if (outside)
else
{
decoratorStackPosition.remove();
throw new IllegalStateException("Cannot operate on non container provided decorator " + decorator);
}
}
try
{
T proxy = proxyClassForDecorators.newInstance();
((ProxyObject) proxy).setHandler(new DecoratorProxyMethodHandler(decoratorInstances, instance));
return proxy;
}
catch (InstantiationException e)
{
throw new RuntimeException("Could not instantiate decorator proxy for " + toString(), e);
}
catch (IllegalAccessException e)
}
finally
{
if (outside)
{
throw new RuntimeException("Could not access bean correctly when creating decorator proxy for " + toString(), e);
decoratorStackPosition.remove();
}
}
else
try
{
return instance;
T proxy = proxyClassForDecorators.newInstance();
((ProxyObject) proxy).setHandler(new DecoratorProxyMethodHandler(decoratorInstances, instance));
return proxy;
}
catch (InstantiationException e)
{
throw new RuntimeException("Could not instantiate decorator proxy for " + toString(), e);
}
catch (IllegalAccessException e)
{
throw new RuntimeException("Could not access bean correctly when creating decorator proxy for " + toString(), e);
}
}

Expand Down Expand Up @@ -248,17 +250,10 @@ protected void initType()
/**
* Initializes the injection points
*/
protected void initInjectionPoints()
protected void initInjectableFields()
{
injectableFields = new HashSet<FieldInjectionPoint<?>>(Beans.getFieldInjectionPoints(annotatedItem, this));
super.injectionPoints.addAll(injectableFields);
for (WBMethod<?> initializer : getInitializerMethods())
{
for (WBParameter<?> parameter : initializer.getParameters())
{
injectionPoints.add(ParameterInjectionPoint.of(this, parameter));
}
}
addInjectionPoints(injectableFields);
}

/**
Expand Down Expand Up @@ -287,7 +282,12 @@ else if (method.getAnnotatedParameters(Observes.class).size() > 0)
}
else
{
initializerMethods.add(MethodInjectionPoint.of(this, method));
MethodInjectionPoint<?> initializerMethod = MethodInjectionPoint.of(this, method);
initializerMethods.add(initializerMethod);
for (WBParameter<?> parameter : initializerMethod.getParameters())
{
addInjectionPoint(ParameterInjectionPoint.of(this, parameter));
}
}
}
}
Expand Down
Expand Up @@ -198,7 +198,7 @@ else if (instance != null)
{
throw new IllegalProductException("Producers cannot declare passivating scope and return a non-serializable class");
}
InjectionPoint injectionPoint = manager.getInjectionPoint();
InjectionPoint injectionPoint = manager.getCurrentInjectionPoint();
if (injectionPoint == null)
{
return;
Expand Down Expand Up @@ -280,11 +280,17 @@ else if (deploymentTypes.size() == 1)
return;
}
}

@Override
protected void initSerializable()
{
_serializable = true;
// No-op
}

@Override
public boolean isSerializable()
{
return true;
}

/**
Expand Down

0 comments on commit 8ea1021

Please sign in to comment.