Skip to content

Commit

Permalink
Added method and constructor parameter injection points for Injection…
Browse files Browse the repository at this point in the history
…Point beans; completed the constructor test.

git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@923 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
drallen committed Jan 13, 2009
1 parent ec8eb88 commit e983721
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 11 deletions.
Expand Up @@ -419,7 +419,7 @@ public Set<InjectionPoint> getInjectionPoints()
for (AnnotatedItem<?, ?> annotatedInjectionPoint : annotatedInjectionPoints)
{
AnnotatedMember<?, ?> member = (AnnotatedMember<?, ?>) annotatedInjectionPoint;
injectionsPoints.add(InjectionPointImpl.of(member));
injectionsPoints.add(InjectionPointImpl.of(member, this));
}
return injectionsPoints;
}
Expand Down
Expand Up @@ -235,7 +235,7 @@ protected void injectEjbAndCommonFields(T beanInstance)

for (AnnotatedMethod<?> method : annotatedItem.getAnnotatedMethods(manager.getEjbResolver().getEJBAnnotation()))
{
InjectionPoint injectionPoint = new InjectionPointImpl(method, this);
InjectionPoint injectionPoint = InjectionPointImpl.of(method, this);
Object ejbInstance = manager.getEjbResolver().resolveEjb(injectionPoint, manager.getNaming());
method.invoke(beanInstance, ejbInstance);
}
Expand All @@ -253,7 +253,7 @@ protected void injectEjbAndCommonFields(T beanInstance)

for (AnnotatedMethod<?> method : annotatedItem.getAnnotatedMethods(manager.getEjbResolver().getPersistenceContextAnnotation()))
{
InjectionPoint injectionPoint = new InjectionPointImpl(method, this);
InjectionPoint injectionPoint = InjectionPointImpl.of(method, this);
Object puInstance = manager.getEjbResolver().resolvePersistenceContext(injectionPoint, manager.getNaming());
method.invoke(beanInstance, puInstance);
}
Expand Down
Expand Up @@ -31,7 +31,9 @@
import javax.webbeans.Standard;
import javax.webbeans.manager.Bean;

import org.jboss.webbeans.introspector.AnnotatedField;
import org.jboss.webbeans.introspector.AnnotatedMember;
import org.jboss.webbeans.introspector.AnnotatedParameter;

/**
* The container provided implementation for InjectionPoint beans
Expand All @@ -43,6 +45,7 @@
public class InjectionPointImpl implements InjectionPoint
{
private final AnnotatedMember<?, ?> memberInjectionPoint;
private final AnnotatedParameter<?> parameterInjectionPoint;
private final Bean<?> bean;

/**
Expand All @@ -52,15 +55,41 @@ public class InjectionPointImpl implements InjectionPoint
* @param bean The bean being injected
* @param beanInstance The instance of the bean being injected
*/
public InjectionPointImpl(AnnotatedMember<?, ?> injectedMember, Bean<?> bean)
public InjectionPointImpl(AnnotatedField<?> injectedMember, Bean<?> bean)
{
this.memberInjectionPoint = injectedMember;
this.parameterInjectionPoint = null;
this.bean = bean;
}

public static InjectionPointImpl of(AnnotatedMember<?, ?> member)
/**
* Creates a new injection point representing a parameter to a constructor or method.
*
* @param injectedMember The constructor or method member
* @param parameterInjectionPoint The parameter
* @param bean The bean owning the injectedMember
*/
public InjectionPointImpl(AnnotatedMember<?, ?> injectedMember, AnnotatedParameter<?> parameterInjectionPoint, Bean<?> bean)
{
this.memberInjectionPoint = injectedMember;
this.parameterInjectionPoint = parameterInjectionPoint;
this.bean = bean;
}

/**
* Returns a new injection point of any type. If this is a parameter, the
* information about the parameter is null.
*
* @param member The member being injected
* @param bean The bean
* @return a new injection point metadata bean
*/
public static InjectionPointImpl of(AnnotatedMember<?, ?> member, Bean<?> bean)
{
return new InjectionPointImpl(member, null);
if (member instanceof AnnotatedField)
return new InjectionPointImpl((AnnotatedField<?>) member, bean);
else
return new InjectionPointImpl(member, null, bean);
}

public boolean isField()
Expand Down Expand Up @@ -100,7 +129,10 @@ public Bean<?> getBean()

public Set<Annotation> getBindings()
{
return this.memberInjectionPoint.getBindingTypes();
if (isField())
return this.memberInjectionPoint.getBindingTypes();
else
return this.parameterInjectionPoint.getBindingTypes();
}

public Member getMember()
Expand All @@ -110,7 +142,10 @@ public Member getMember()

public Type getType()
{
return this.memberInjectionPoint.getType();
if (isField())
return this.memberInjectionPoint.getType();
else
return this.parameterInjectionPoint.getType();
}

public boolean isAnnotationPresent(Class<? extends Annotation> annotationType)
Expand Down
Expand Up @@ -23,7 +23,9 @@
import javax.webbeans.InjectionPoint;
import javax.webbeans.manager.Bean;

import org.jboss.webbeans.introspector.AnnotatedField;
import org.jboss.webbeans.introspector.AnnotatedMember;
import org.jboss.webbeans.introspector.AnnotatedParameter;

/**
* Used to create the container provided implementation for the InjectionPoint
Expand All @@ -38,6 +40,7 @@ public class InjectionPointProvider
{
private final Stack<Bean<?>> beans = new Stack<Bean<?>>();
private final Stack<AnnotatedMember<?, ? extends Member>> injectionPoints = new Stack<AnnotatedMember<?, ? extends Member>>();
private final Stack<AnnotatedParameter<?>> injectionParameters = new Stack<AnnotatedParameter<?>>();

/**
* Pushes the current bean that is being instantiated onto a stack for later
Expand All @@ -61,6 +64,11 @@ public void pushInjectionMember(AnnotatedMember<?, ? extends Member> injectedMem
injectionPoints.push(injectedMember);
}

public void pushInjectionParameter(AnnotatedParameter<?> parameter)
{
injectionParameters.push(parameter);
}

/**
* Pops the bean from the stack. This should be called whenever all
* processing is complete for instantiating a bean.
Expand All @@ -79,6 +87,11 @@ public void popInjectionMember()
injectionPoints.pop();
}

public void popInjectionParameter()
{
injectionParameters.pop();
}

/**
* Returns the InjectionPoint where the current bean under construction is
* being injected.
Expand All @@ -87,7 +100,11 @@ public void popInjectionMember()
*/
public InjectionPoint getPreviousInjectionPoint()
{
return new InjectionPointImpl(getPreviousInjectionMember(), getPreviousBean());
AnnotatedMember<?, ? extends Member> member = getPreviousInjectionMember();
if (member instanceof AnnotatedField)
return new InjectionPointImpl((AnnotatedField<?>) member, getPreviousBean());
else
return new InjectionPointImpl(member, getPreviousParameter(), getPreviousBean());
}

/**
Expand All @@ -98,13 +115,18 @@ public InjectionPoint getPreviousInjectionPoint()
*/
public InjectionPoint getCurrentInjectionPoint()
{
return new InjectionPointImpl(getCurrentInjectionMember(), getCurrentBean());
AnnotatedMember<?, ? extends Member> member = getCurrentInjectionMember();
if (member instanceof AnnotatedField)
return new InjectionPointImpl((AnnotatedField<?>) member, getCurrentBean());
else
return new InjectionPointImpl(member, getCurrentParameter(), getCurrentBean());
}

protected Bean<?> getCurrentBean()
{
return beans.peek();
}

protected AnnotatedMember<?, ? extends Member> getCurrentInjectionMember()
{
if (injectionPoints.size() > 0)
Expand All @@ -113,6 +135,11 @@ protected Bean<?> getCurrentBean()
return null;
}

protected AnnotatedParameter<?> getCurrentParameter()
{
return injectionParameters.peek();
}

protected Bean<?> getPreviousBean()
{
Bean<?> currentBean = beans.pop();
Expand All @@ -139,4 +166,22 @@ protected Bean<?> getPreviousBean()
}
return result;
}

protected AnnotatedParameter<?> getPreviousParameter()
{
AnnotatedParameter<?> result = null;
if (getCurrentInjectionMember() instanceof AnnotatedField)
{
// Since no parameter is pushed, top of stack is the one wanted
result = injectionParameters.peek();
}
else
{
AnnotatedParameter<?> currentParameter = injectionParameters.pop();
result = injectionParameters.peek();
injectionParameters.push(currentParameter);
}
return result;
}

}
Expand Up @@ -220,7 +220,9 @@ protected Object[] getParameterValues(List<AnnotatedParameter<Object>> parameter
}
else
{
injectionPointProvider.pushInjectionParameter(param);
parameterValues[i] = param.getValue(manager);
injectionPointProvider.popInjectionParameter();
}
}
}
Expand Down
Expand Up @@ -148,7 +148,7 @@ public void testGetMemberMethod()
assert false;
}

@Test(groups = { "broken", "injectionPoint" })
@Test(groups = { "injectionPoint" })
@SpecAssertion(section = "5.11")
public void testGetMemberConstructor()
{
Expand All @@ -163,6 +163,10 @@ public void testGetMemberConstructor()
BeanWithInjectionPointMetadata beanWithInjectionPoint = beanWithInjectedBean.getInjectedBean();
assert beanWithInjectionPoint.getInjectedMetadata() != null;
assert Constructor.class.isAssignableFrom(beanWithInjectionPoint.getInjectedMetadata().getMember().getClass());

// Since the type and bindings must correspond to the parameter, check them
assert beanWithInjectionPoint.getInjectedMetadata().getType().equals(BeanWithInjectionPointMetadata.class);
assert beanWithInjectionPoint.getInjectedMetadata().getBindings().contains(new CurrentBinding());
}
finally
{
Expand Down
Expand Up @@ -17,6 +17,7 @@
package org.jboss.webbeans.test.beans;

import javax.webbeans.Current;
import javax.webbeans.Initializer;

/**
* Test bean to inject a bean using injection point metadata into a constructor
Expand All @@ -28,6 +29,7 @@ public class ConstructorInjectionPointBean
{
private BeanWithInjectionPointMetadata injectedBean;

@Initializer
public ConstructorInjectionPointBean(@Current BeanWithInjectionPointMetadata injectedBean)
{
this.injectedBean = injectedBean;
Expand Down

0 comments on commit e983721

Please sign in to comment.