Skip to content

Commit

Permalink
Added InjectionPoint beans with injection into fields and parameters.
Browse files Browse the repository at this point in the history
git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@816 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
drallen committed Jan 7, 2009
1 parent ba9cab2 commit cf216c7
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 78 deletions.
29 changes: 24 additions & 5 deletions webbeans-ri/src/main/java/org/jboss/webbeans/bean/SimpleBean.java
Expand Up @@ -31,6 +31,7 @@
import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.MetaDataCache;
import org.jboss.webbeans.context.DependentContext;
import org.jboss.webbeans.injection.InjectionPointFactory;
import org.jboss.webbeans.injection.InjectionPointImpl;
import org.jboss.webbeans.introspector.AnnotatedClass;
import org.jboss.webbeans.introspector.AnnotatedConstructor;
Expand Down Expand Up @@ -62,7 +63,7 @@ public class SimpleBean<T> extends AbstractClassBean<T>
private AnnotatedMethod<Object> postConstruct;
// The pre-destroy method
private AnnotatedMethod<Object> preDestroy;

/**
* Creates a simple, annotation defined Web Bean
*
Expand All @@ -75,7 +76,7 @@ public static <T> SimpleBean<T> of(Class<T> clazz, ManagerImpl manager)
{
return of(AnnotatedClassImpl.of(clazz), manager);
}

/**
* Creates a simple, annotation defined Web Bean
*
Expand Down Expand Up @@ -117,13 +118,17 @@ public T create()
{
checkProducedInjectionPoints();
}
InjectionPointFactory injectionPointFactory = manager.getInjectionPointFactory();
injectionPointFactory.pushBean(this);
T instance = constructor.newInstance(manager);
injectionPointFactory.pushInstance(instance);
bindDecorators();
bindInterceptors();
injectEjbAndCommonFields(instance);
injectBoundFields(instance);
callInitializers(instance);
callPostConstruct(instance);
injectionPointFactory.popBeanAndInstance();
return instance;
}
finally
Expand Down Expand Up @@ -217,25 +222,29 @@ protected void callInitializers(T instance)
*/
protected void injectEjbAndCommonFields(T beanInstance)
{
InjectionPointFactory injectionPointFactory = manager.getInjectionPointFactory();
for (AnnotatedField<?> field : annotatedItem.getAnnotatedFields(manager.getEjbResolver().getEJBAnnotation()))
{
injectionPointFactory.pushInjectionPoint(field);
InjectionPoint injectionPoint = new InjectionPointImpl(field, this, beanInstance);
Object ejbInstance = manager.getEjbResolver().resolveEjb(injectionPoint, manager.getNaming());
field.inject(beanInstance, ejbInstance);
injectionPointFactory.popInjectionPoint();
}

for (AnnotatedMethod<?> method : annotatedItem.getAnnotatedMethods(manager.getEjbResolver().getEJBAnnotation()))
{
InjectionPoint injectionPoint = new InjectionPointImpl(method, this, beanInstance);
Object ejbInstance = manager.getEjbResolver().resolveEjb(injectionPoint, manager.getNaming());
method.invoke(beanInstance, ejbInstance);
}

for (AnnotatedField<?> field : annotatedItem.getAnnotatedFields(manager.getEjbResolver().getPersistenceContextAnnotation()))
{
InjectionPoint injectionPoint = new InjectionPointImpl(field, this, beanInstance);
EntityManagerFactory entityManagerFactory = manager.getEjbResolver().resolvePersistenceUnit(injectionPoint, manager.getNaming());
field.inject(beanInstance, entityManagerFactory.createEntityManager());
injectionPointFactory.popInjectionPoint();
}
}

Expand All @@ -246,9 +255,19 @@ protected void injectEjbAndCommonFields(T beanInstance)
*/
protected void injectBoundFields(T instance)
{
InjectionPointFactory injectionPointFactory = manager.getInjectionPointFactory();
for (AnnotatedField<?> injectableField : getInjectableFields())
{
injectableField.inject(instance, manager);
injectionPointFactory.pushInjectionPoint(injectableField);
if (InjectionPoint.class.isAssignableFrom(injectableField.getType()))
{
injectableField.inject(instance, injectionPointFactory.newInstance());
}
else
{
injectableField.inject(instance, manager);
}
injectionPointFactory.popInjectionPoint();
}
}

Expand Down
Expand Up @@ -23,7 +23,7 @@
import javax.webbeans.InjectionPoint;
import javax.webbeans.manager.Bean;

import org.jboss.webbeans.introspector.jlr.AbstractAnnotatedMember;
import org.jboss.webbeans.introspector.AnnotatedMember;

/**
* Factory used to create the container provided implementation for the
Expand All @@ -38,7 +38,7 @@ public class InjectionPointFactory
{
private final Stack<Bean<?>> beans = new Stack<Bean<?>>();
private final Stack<Object> beanInstances = new Stack<Object>();
private final Stack<AbstractAnnotatedMember<?, ? extends Member>> injectionPoints = new Stack<AbstractAnnotatedMember<?, ? extends Member>>();
private final Stack<AnnotatedMember<?, ? extends Member>> injectionPoints = new Stack<AnnotatedMember<?, ? extends Member>>();

/**
* Pushes the current bean that is being instantiated onto a stack for later
Expand Down Expand Up @@ -67,7 +67,7 @@ public void pushInstance(Object currentInstance)
*
* @param injectedMember The metadata for the injection point
*/
public void pushInjectionPoint(AbstractAnnotatedMember<?, ? extends Member> injectedMember)
public void pushInjectionPoint(AnnotatedMember<?, ? extends Member> injectedMember)
{
injectionPoints.push(injectedMember);
}
Expand Down Expand Up @@ -106,7 +106,7 @@ public InjectionPoint newInstance()
// the bean stack.
InjectionPoint injectionPoint = null;
Bean<?> currentBean = beans.pop();
AbstractAnnotatedMember<?, ? extends Member> currentInjection = injectionPoints.pop();
AnnotatedMember<?, ? extends Member> currentInjection = injectionPoints.pop();
if (beanInstances.size() < beans.size())
{
injectionPoint = new InjectionPointImpl(injectionPoints.peek(), beans.peek(), beanInstances.peek());
Expand Down
Expand Up @@ -21,7 +21,7 @@
import java.lang.reflect.Constructor;
import java.util.List;

import javax.webbeans.manager.Manager;
import org.jboss.webbeans.ManagerImpl;

/**
* Represents a Class Constructor
Expand Down Expand Up @@ -56,7 +56,7 @@ public interface AnnotatedConstructor<T> extends AnnotatedMember<T, Constructor<
* @param manager The Web Beans manager
* @return The created instance
*/
public T newInstance(Manager manager);
public T newInstance(ManagerImpl manager);

/**
* Gets the declaring class of the annotation
Expand Down
Expand Up @@ -32,7 +32,7 @@
import javax.webbeans.Fires;
import javax.webbeans.IfExists;
import javax.webbeans.Observes;
import javax.webbeans.manager.Manager;
import org.jboss.webbeans.ManagerImpl;

/**
* AnnotatedType provides a uniform access to the annotations on an annotated
Expand Down Expand Up @@ -75,7 +75,7 @@ public interface AnnotatedMethod<T> extends AnnotatedMember<T, Method>
* @param manager The Web Beans manager
* @return A reference to the instance
*/
public T invoke(Object instance, Manager manager);
public T invoke(Object instance, ManagerImpl manager);

/**
* Invokes the method on the class of the passed instance, not the declaring
Expand All @@ -85,7 +85,7 @@ public interface AnnotatedMethod<T> extends AnnotatedMember<T, Method>
* @param manager The Web Beans manager
* @return A reference to the instance
*/
public T invokeOnInstance(Object instance, Manager manager);
public T invokeOnInstance(Object instance, ManagerImpl manager);

/**
* Invokes the observer method
Expand All @@ -95,7 +95,7 @@ public interface AnnotatedMethod<T> extends AnnotatedMember<T, Method>
* @param manager The Web Beans manager
* @return A reference to the instance
*/
public T invokeWithSpecialValue(Object instance, Class<? extends Annotation> specialParam, Object specialVal, Manager manager);
public T invokeWithSpecialValue(Object instance, Class<? extends Annotation> specialParam, Object specialVal, ManagerImpl manager);

/**
* Invokes the method
Expand Down
Expand Up @@ -24,17 +24,12 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.webbeans.BindingType;
import javax.webbeans.manager.Manager;

import org.jboss.webbeans.binding.CurrentBinding;
import org.jboss.webbeans.introspector.AnnotatedItem;
import org.jboss.webbeans.introspector.AnnotatedParameter;
import org.jboss.webbeans.util.Reflections;
import org.jboss.webbeans.util.Strings;
import org.jboss.webbeans.util.Types;
Expand Down Expand Up @@ -189,46 +184,6 @@ protected static AnnotationMap buildAnnotationMap(Annotation[] annotations)
return annotationMap;
}

/**
* Static helper method for getting the current parameter values from a list
* of annotated parameters.
*
* @param parameters The list of annotated parameter to look up
* @param manager The Web Beans manager
* @return The object array of looked up values
*/
protected static Object[] getParameterValues(List<AnnotatedParameter<Object>> parameters, Manager manager)
{
return getParameterValues(parameters, null, null, manager);
}

/**
* Static helper method for getting the current parameter values from a list
* of annotated parameters.
*
* @param parameters The list of annotated parameter to look up
* @param manager The Web Beans manager
* @return The object array of looked up values
*/
protected static Object[] getParameterValues(List<AnnotatedParameter<Object>> parameters, Object specialVal, Class<? extends Annotation> specialParam, Manager manager)
{
Object[] parameterValues = new Object[parameters.size()];
Iterator<AnnotatedParameter<Object>> iterator = parameters.iterator();
for (int i = 0; i < parameterValues.length; i++)
{
AnnotatedParameter<Object> param = iterator.next();
if ( specialParam!=null && param.isAnnotationPresent(specialParam))
{
parameterValues[i] = specialVal;
}
else
{
parameterValues[i] = param.getValue(manager);
}
}
return parameterValues;
}

// The annotation map (annotation type -> annotation) of the item
private final AnnotationMap annotationMap;
// The meta-annotation map (annotation type -> set of annotations containing
Expand Down
Expand Up @@ -21,12 +21,16 @@
import java.lang.reflect.Member;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.webbeans.BindingType;
import javax.webbeans.InjectionPoint;
import javax.webbeans.manager.Manager;

import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.injection.InjectionPointFactory;
import org.jboss.webbeans.introspector.AnnotatedMember;
import org.jboss.webbeans.introspector.AnnotatedParameter;
import org.jboss.webbeans.util.Reflections;
Expand Down Expand Up @@ -179,4 +183,51 @@ public S getMember()
return getDelegate();
}

/**
* Helper method for getting the current parameter values from a list
* of annotated parameters.
*
* @param parameters The list of annotated parameter to look up
* @param manager The Web Beans manager
* @return The object array of looked up values
*/
protected Object[] getParameterValues(List<AnnotatedParameter<Object>> parameters, ManagerImpl manager)
{
return getParameterValues(parameters, null, null, manager);
}

/**
* Helper method for getting the current parameter values from a list
* of annotated parameters.
*
* @param parameters The list of annotated parameter to look up
* @param manager The Web Beans manager
* @return The object array of looked up values
*/
protected Object[] getParameterValues(List<AnnotatedParameter<Object>> parameters, Object specialVal, Class<? extends Annotation> specialParam, ManagerImpl manager)
{
InjectionPointFactory injectionPointFactory = manager.getInjectionPointFactory();
injectionPointFactory.pushInjectionPoint(this);
Object[] parameterValues = new Object[parameters.size()];
Iterator<AnnotatedParameter<Object>> iterator = parameters.iterator();
for (int i = 0; i < parameterValues.length; i++)
{
AnnotatedParameter<Object> param = iterator.next();
if ( specialParam!=null && param.isAnnotationPresent(specialParam))
{
parameterValues[i] = specialVal;
}
else if ( InjectionPoint.class.isAssignableFrom(param.getType()) )
{
parameterValues[i] = injectionPointFactory.newInstance();
}
else
{
parameterValues[i] = param.getValue(manager);
}
}
injectionPointFactory.popInjectionPoint();
return parameterValues;
}

}
Expand Up @@ -26,8 +26,6 @@
import java.util.List;

import javax.webbeans.ExecutionException;
import javax.webbeans.manager.Manager;

import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.introspector.AnnotatedConstructor;
import org.jboss.webbeans.introspector.AnnotatedParameter;
Expand Down Expand Up @@ -185,7 +183,7 @@ public List<AnnotatedParameter<Object>> getAnnotatedParameters(Class<? extends A
*
* @see org.jboss.webbeans.introspector.AnnotatedConstructor#newInstance(ManagerImpl)
*/
public T newInstance(Manager manager)
public T newInstance(ManagerImpl manager)
{
try
{
Expand Down
Expand Up @@ -26,8 +26,6 @@
import java.util.Collections;
import java.util.List;

import javax.webbeans.manager.Manager;

import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.introspector.AnnotatedMethod;
import org.jboss.webbeans.introspector.AnnotatedParameter;
Expand Down Expand Up @@ -248,19 +246,19 @@ public int hashCode()
* Object)
*/
@SuppressWarnings("unchecked")
public T invoke(Object instance, Manager manager)
public T invoke(Object instance, ManagerImpl manager)
{
return (T) Reflections.invokeAndWrap(getDelegate(), instance, getParameterValues(parameters, manager));
}

@SuppressWarnings("unchecked")
public T invokeOnInstance(Object instance, Manager manager)
public T invokeOnInstance(Object instance, ManagerImpl manager)
{
return (T) Reflections.invokeAndWrap(getName(), getParameterTypesAsArray(), instance, getParameterValues(parameters, manager));
}

@SuppressWarnings("unchecked")
public T invokeWithSpecialValue(Object instance, Class<? extends Annotation> specialParam, Object specialVal, Manager manager)
public T invokeWithSpecialValue(Object instance, Class<? extends Annotation> specialParam, Object specialVal, ManagerImpl manager)
{
return (T) Reflections.invokeAndWrap(getDelegate(), instance, getParameterValues(parameters, specialVal, specialParam, manager));
}
Expand Down

0 comments on commit cf216c7

Please sign in to comment.