Skip to content

Commit

Permalink
Refactored some of the injection point metadata bean code.
Browse files Browse the repository at this point in the history
git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@835 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
drallen committed Jan 8, 2009
1 parent 73fb0e3 commit 036431e
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 40 deletions.
6 changes: 6 additions & 0 deletions webbeans-ri/src/main/java/org/jboss/webbeans/Resolver.java
Expand Up @@ -28,13 +28,15 @@
import java.util.TreeSet;
import java.util.concurrent.Callable;

import javax.webbeans.InjectionPoint;
import javax.webbeans.NullableDependencyException;
import javax.webbeans.TypeLiteral;
import javax.webbeans.manager.Bean;
import javax.webbeans.manager.Decorator;
import javax.webbeans.manager.InterceptionType;
import javax.webbeans.manager.Interceptor;

import org.jboss.webbeans.bean.InjectionPointBean;
import org.jboss.webbeans.introspector.AnnotatedItem;
import org.jboss.webbeans.introspector.ForwardingAnnotatedItem;
import org.jboss.webbeans.model.BindingTypeModel;
Expand Down Expand Up @@ -215,6 +217,10 @@ public AnnotatedItem<T, S> delegate()
{
beans = new HashSet<Bean<T>>((List) manager.getBeans());
}
else if (InjectionPoint.class.isAssignableFrom(element.getType()))
{
beans.add(InjectionPointBean.of(key, manager));
}
else
{
beans = registerInjectionPoint(element);
Expand Down
@@ -0,0 +1,66 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat Middleware LLC, 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.webbeans.bean;

import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.introspector.AnnotatedItem;

/**
* Bean for InjectionPoint metadata
*
* @author David Allen
*
*/
public class InjectionPointBean<T, S> extends AbstractFacadeBean<T, S, Object>
{

/**
* Creates an InjectionPoint Web Bean for the injection of the containing bean owning
* the field, constructor or method for the annotated item
*
* @param <T> must be InjectionPoint
* @param <S>
* @param field The annotated member field/parameter for the injection
* @param manager The RI manager implementation
* @return a new bean for this injection point
*/
public static <T, S> InjectionPointBean<T, S> of(AnnotatedItem<T, S> field, ManagerImpl manager)
{
return new InjectionPointBean<T, S>(field, manager);
}

protected InjectionPointBean(AnnotatedItem<T, S> field, ManagerImpl manager)
{
super(field, manager);
}

@SuppressWarnings("unchecked")
@Override
public T create()
{
return (T) manager.getInjectionPointFactory().getPreviousInjectionPoint();
}

@Override
public void destroy(T instance)
{
// The instance is always in the Dependent context and can be garbage
// collected
}

}
41 changes: 27 additions & 14 deletions webbeans-ri/src/main/java/org/jboss/webbeans/bean/SimpleBean.java
Expand Up @@ -119,15 +119,29 @@ public T create()
}
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();
T instance = null;
try
{
instance = constructor.newInstance(manager);
try
{
injectionPointFactory.pushInstance(instance);
bindDecorators();
bindInterceptors();
injectEjbAndCommonFields(instance);
injectBoundFields(instance);
callInitializers(instance);
callPostConstruct(instance);
}
finally
{
injectionPointFactory.popInstance();
}
}
finally
{
injectionPointFactory.popBean();
}
return instance;
}
finally
Expand Down Expand Up @@ -261,15 +275,14 @@ protected void injectBoundFields(T instance)
for (AnnotatedField<?> injectableField : getInjectableFields())
{
injectionPointFactory.pushInjectionPoint(injectableField);
if (InjectionPoint.class.isAssignableFrom(injectableField.getType()))
try
{
injectableField.inject(instance, injectionPointFactory.newInstance());
injectableField.inject(instance, manager);
}
else
finally
{
injectableField.inject(instance, manager);
injectionPointFactory.popInjectionPoint();
}
injectionPointFactory.popInjectionPoint();
}
}

Expand Down
Expand Up @@ -73,12 +73,20 @@ public void pushInjectionPoint(AnnotatedMember<?, ? extends Member> injectedMemb
}

/**
* Pops the bean and its current instance from the stack. This should be called
* Pops the bean from the stack. This should be called
* whenever all processing is complete for instantiating a bean.
*/
public void popBeanAndInstance()
public void popBean()
{
beans.pop();
}

/**
* Pops the current instance from the stack. This should be called
* whenever all processing is complete for instantiating a bean.
*/
public void popInstance()
{
beanInstances.pop();
}

Expand All @@ -98,7 +106,7 @@ public void popInjectionPoint()
*
* @return a new injection point metadata object
*/
public InjectionPoint newInstance()
public InjectionPoint getPreviousInjectionPoint()
{
// When the injected member is a constructor, we are short one instance,
// so the instance on the top of the stack is the bean instance
Expand Down
Expand Up @@ -26,7 +26,6 @@
import java.util.Map;

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

import org.jboss.webbeans.ManagerImpl;
Expand Down Expand Up @@ -138,7 +137,7 @@ public boolean isTransient()
{
return Reflections.isTransient(getDelegate());
}

/**
* Gets the current value of the member
*
Expand Down Expand Up @@ -176,16 +175,16 @@ public String toString()
}
toString = "Abstract annotated member " + getName();
return toString;
}
}

public S getMember()
{
return getDelegate();
}

/**
* Helper method for getting the current parameter values from a list
* of annotated parameters.
* 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
Expand All @@ -197,36 +196,39 @@ protected Object[] getParameterValues(List<AnnotatedParameter<Object>> parameter
}

/**
* Helper method for getting the current parameter values from a list
* of annotated parameters.
* 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)
{
Object[] parameterValues = new Object[parameters.size()];
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++)
try
{
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
Iterator<AnnotatedParameter<Object>> iterator = parameters.iterator();
for (int i = 0; i < parameterValues.length; i++)
{
parameterValues[i] = param.getValue(manager);
AnnotatedParameter<Object> param = iterator.next();
if (specialParam != null && param.isAnnotationPresent(specialParam))
{
parameterValues[i] = specialVal;
}
else
{
parameterValues[i] = param.getValue(manager);
}
}
}
injectionPointFactory.popInjectionPoint();
finally
{
injectionPointFactory.popInjectionPoint();

}
return parameterValues;
}

Expand Down

0 comments on commit 036431e

Please sign in to comment.