Author: pete.muir(a)jboss.org
Date: 2010-01-28 17:11:46 -0500 (Thu, 28 Jan 2010)
New Revision: 5657
Modified:
core/trunk/impl/src/main/java/org/jboss/weld/bean/AbstractClassBean.java
core/trunk/impl/src/main/java/org/jboss/weld/manager/SimpleInjectionTarget.java
core/trunk/impl/src/main/java/org/jboss/weld/util/Beans.java
Log:
WELD-390
Modified: core/trunk/impl/src/main/java/org/jboss/weld/bean/AbstractClassBean.java
===================================================================
--- core/trunk/impl/src/main/java/org/jboss/weld/bean/AbstractClassBean.java 2010-01-28
17:59:05 UTC (rev 5656)
+++ core/trunk/impl/src/main/java/org/jboss/weld/bean/AbstractClassBean.java 2010-01-28
22:11:46 UTC (rev 5657)
@@ -20,10 +20,8 @@
import static org.jboss.weld.logging.LoggerFactory.loggerFactory;
import static
org.jboss.weld.logging.messages.BeanMessage.CONFLICTING_INTERCEPTOR_BINDINGS;
import static org.jboss.weld.logging.messages.BeanMessage.INVOCATION_ERROR;
-import static org.jboss.weld.logging.messages.BeanMessage.NON_CONTAINER_DECORATOR;
import static org.jboss.weld.logging.messages.BeanMessage.ONLY_ONE_SCOPE_ALLOWED;
import static
org.jboss.weld.logging.messages.BeanMessage.PARAMETER_ANNOTATION_NOT_ALLOWED_ON_CONSTRUCTOR;
-import static
org.jboss.weld.logging.messages.BeanMessage.PROXY_INSTANTIATION_BEAN_ACCESS_FAILED;
import static org.jboss.weld.logging.messages.BeanMessage.PROXY_INSTANTIATION_FAILED;
import static
org.jboss.weld.logging.messages.BeanMessage.SPECIALIZING_BEAN_MUST_EXTEND_A_BEAN;
import static org.jboss.weld.logging.messages.BeanMessage.USING_DEFAULT_SCOPE;
@@ -59,11 +57,9 @@
import org.jboss.weld.bean.proxy.DecorationHelper;
import org.jboss.weld.bootstrap.BeanDeployerEnvironment;
import org.jboss.weld.context.SerializableContextualImpl;
-import org.jboss.weld.context.SerializableContextualInstanceImpl;
import org.jboss.weld.ejb.EJBApiAbstraction;
import org.jboss.weld.exceptions.DefinitionException;
import org.jboss.weld.exceptions.DeploymentException;
-import org.jboss.weld.exceptions.ForbiddenStateException;
import org.jboss.weld.exceptions.WeldException;
import org.jboss.weld.injection.ConstructorInjectionPoint;
import org.jboss.weld.injection.FieldInjectionPoint;
@@ -73,7 +69,6 @@
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.metadata.cache.MetaAnnotationStore;
import org.jboss.weld.serialization.spi.helpers.SerializableContextual;
-import org.jboss.weld.serialization.spi.helpers.SerializableContextualInstance;
import org.jboss.weld.util.Beans;
import org.jboss.weld.util.Proxies;
import org.jboss.weld.util.Proxies.TypeInfo;
@@ -145,8 +140,8 @@
private boolean hasSerializationOrInvocationInterceptorMethods;
// Bean callback methods
- private WeldMethod<?, ?> postConstruct;
- private WeldMethod<?, ?> preDestroy;
+ private List<WeldMethod<?, ? super T>> postConstructMethods;
+ private List<WeldMethod<?, ? super T>> preDestroyMethods;
// Injection target for the bean
private InjectionTarget<T> injectionTarget;
@@ -361,7 +356,7 @@
*/
protected void initPostConstruct()
{
- this.postConstruct = Beans.getPostConstruct(getWeldAnnotated());
+ this.postConstructMethods = Beans.getPostConstructMethods(getWeldAnnotated());
}
/**
@@ -369,7 +364,7 @@
*/
protected void initPreDestroy()
{
- this.preDestroy = Beans.getPreDestroy(getWeldAnnotated());
+ this.preDestroyMethods = Beans.getPreDestroyMethods(getWeldAnnotated());
}
/**
@@ -377,9 +372,9 @@
*
* @return The post-construct method
*/
- public WeldMethod<?, ?> getPostConstruct()
+ public List<WeldMethod<?, ? super T>> getPostConstruct()
{
- return postConstruct;
+ return postConstructMethods;
}
/**
@@ -387,9 +382,9 @@
*
* @return The pre-destroy method
*/
- public WeldMethod<?, ?> getPreDestroy()
+ public List<WeldMethod<?, ? super T>> getPreDestroy()
{
- return preDestroy;
+ return preDestroyMethods;
}
protected abstract boolean isInterceptionCandidate();
@@ -473,34 +468,39 @@
protected void defaultPreDestroy(T instance)
{
- WeldMethod<?, ?> preDestroy = getPreDestroy();
- if (preDestroy != null)
+ for (WeldMethod<?, ? super T> method : getPreDestroy())
{
- try
+ if (method != null)
{
- // note: RI supports injection into @PreDestroy
- preDestroy.invoke(instance);
+ try
+ {
+ // note: RI supports injection into @PreDestroy
+ method.invoke(instance);
+ }
+ catch (Exception e)
+ {
+ throw new WeldException(INVOCATION_ERROR, e, method, instance);
+ }
}
- catch (Exception e)
- {
- throw new WeldException(INVOCATION_ERROR, e, preDestroy, instance);
- }
}
}
protected void defaultPostConstruct(T instance)
{
- WeldMethod<?, ?> postConstruct = getPostConstruct();
- if (postConstruct != null)
+ for (WeldMethod<?, ? super T> method : getPostConstruct())
{
- try
+ if (method != null)
{
- postConstruct.invoke(instance);
+ try
+ {
+ // note: RI supports injection into @PreDestroy
+ method.invoke(instance);
+ }
+ catch (Exception e)
+ {
+ throw new WeldException(INVOCATION_ERROR, e, method, instance);
+ }
}
- catch (Exception e)
- {
- throw new WeldException(INVOCATION_ERROR, e, postConstruct, instance);
- }
}
}
@@ -562,16 +562,22 @@
if (methodDeclaredInterceptors != null)
{
if
(method.isAnnotationPresent(beanManager.getServices().get(EJBApiAbstraction.class).TIMEOUT_ANNOTATION_CLASS))
+ {
builder.interceptAroundTimeout(((AnnotatedMethod)
method).getJavaMember()).with(methodDeclaredInterceptors);
+ }
else
+ {
builder.interceptAroundInvoke(((AnnotatedMethod)
method).getJavaMember()).with(methodDeclaredInterceptors);
+ }
}
}
InterceptionModel<Class<?>, Class<?>> interceptionModel =
builder.build();
InterceptorClassMetadata interceptorClassMetadata =
InterceptorClassMetadataRegistry.getRegistry().getInterceptorClassMetadata(getType(),
true);
hasSerializationOrInvocationInterceptorMethods =
!interceptorClassMetadata.getInterceptorMethods(org.jboss.interceptor.model.InterceptionType.AROUND_INVOKE).isEmpty()
||
!interceptorClassMetadata.getInterceptorMethods(org.jboss.interceptor.model.InterceptionType.AROUND_TIMEOUT).isEmpty()
||
!interceptorClassMetadata.getInterceptorMethods(org.jboss.interceptor.model.InterceptionType.PRE_PASSIVATE).isEmpty()
||
!interceptorClassMetadata.getInterceptorMethods(org.jboss.interceptor.model.InterceptionType.POST_ACTIVATE).isEmpty();
if (interceptionModel.getAllInterceptors().size() > 0 ||
hasSerializationOrInvocationInterceptorMethods)
+ {
beanManager.getClassDeclaredInterceptorsRegistry().registerInterceptionModel(getType(),
builder.build());
+ }
}
}
Modified: core/trunk/impl/src/main/java/org/jboss/weld/manager/SimpleInjectionTarget.java
===================================================================
---
core/trunk/impl/src/main/java/org/jboss/weld/manager/SimpleInjectionTarget.java 2010-01-28
17:59:05 UTC (rev 5656)
+++
core/trunk/impl/src/main/java/org/jboss/weld/manager/SimpleInjectionTarget.java 2010-01-28
22:11:46 UTC (rev 5657)
@@ -16,10 +16,9 @@
*/
package org.jboss.weld.manager;
-import static
org.jboss.weld.logging.messages.BeanManagerMessage.ERROR_INVOKING_POST_CONSTRUCT;
-import static
org.jboss.weld.logging.messages.BeanManagerMessage.ERROR_INVOKING_PRE_DESTROY;
import static
org.jboss.weld.logging.messages.BeanManagerMessage.INJECTION_ON_NON_CONTEXTUAL;
import static
org.jboss.weld.logging.messages.BeanManagerMessage.MISSING_BEAN_CONSTRUCTOR_FOUND;
+import static org.jboss.weld.logging.messages.BeanMessage.INVOCATION_ERROR;
import java.util.HashSet;
import java.util.List;
@@ -43,18 +42,18 @@
/**
* @author pmuir
- *
+ *
*/
public class SimpleInjectionTarget<T> implements InjectionTarget<T>
{
-
+
private final BeanManagerImpl beanManager;
private final WeldClass<T> type;
private final ConstructorInjectionPoint<T> constructor;
private final List<Set<FieldInjectionPoint<?, ?>>>
injectableFields;
private final List<Set<MethodInjectionPoint<?, ?>>>
initializerMethods;
- private final WeldMethod<?, ?> postConstruct;
- private final WeldMethod<?, ?> preDestroy;
+ private final List<WeldMethod<?, ? super T>> postConstructMethods;
+ private final List<WeldMethod<?, ? super T>> preDestroyMethods;
private final Set<InjectionPoint> injectionPoints;
private final Set<WeldInjectionPoint<?, ?>> ejbInjectionPoints;
private final Set<WeldInjectionPoint<?, ?>>
persistenceContextInjectionPoints;
@@ -74,7 +73,8 @@
}
catch (Exception e)
{
- // this means the bean of a type that cannot be produce()d, but that is
non-fatal
+ // this means the bean of a type that cannot be produce()d, but that is
+ // non-fatal
// unless someone calls produce()
}
this.constructor = constructor;
@@ -82,8 +82,8 @@
this.injectionPoints.addAll(Beans.getFieldInjectionPoints(null,
this.injectableFields));
this.initializerMethods = Beans.getInitializerMethods(null, type);
this.injectionPoints.addAll(Beans.getParameterInjectionPoints(null,
initializerMethods));
- this.postConstruct = Beans.getPostConstruct(type);
- this.preDestroy = Beans.getPreDestroy(type);
+ this.postConstructMethods = Beans.getPostConstructMethods(type);
+ this.preDestroyMethods = Beans.getPreDestroyMethods(type);
this.ejbInjectionPoints = Beans.getEjbInjectionPoints(null, type, beanManager);
this.persistenceContextInjectionPoints =
Beans.getPersistenceContextInjectionPoints(null, type, beanManager);
this.persistenceUnitInjectionPoints = Beans.getPersistenceUnitInjectionPoints(null,
type, beanManager);
@@ -110,50 +110,58 @@
}
return constructor.newInstance(beanManager, ctx);
}
-
+
public void inject(final T instance, final CreationalContext<T> ctx)
{
new InjectionContextImpl<T>(beanManager, this, instance)
{
-
+
public void proceed()
{
Beans.injectEEFields(instance, beanManager, ejbInjectionPoints,
persistenceContextInjectionPoints, persistenceUnitInjectionPoints,
resourceInjectionPoints);
Beans.injectFieldsAndInitializers(instance, ctx, beanManager,
injectableFields, initializerMethods);
}
-
+
}.run();
}
public void postConstruct(T instance)
{
- if (postConstruct == null)
- return;
-
- try
+ for (WeldMethod<?, ? super T> method : postConstructMethods)
{
- postConstruct.invoke(instance);
+ if (method != null)
+ {
+ try
+ {
+ // note: RI supports injection into @PreDestroy
+ method.invoke(instance);
+ }
+ catch (Exception e)
+ {
+ throw new WeldException(INVOCATION_ERROR, e, method, instance);
+ }
+ }
}
- catch (Exception e)
- {
- throw new WeldException(ERROR_INVOKING_POST_CONSTRUCT, e, postConstruct);
- }
}
public void preDestroy(T instance)
{
- if (preDestroy == null)
- return;
-
- try
+ for (WeldMethod<?, ? super T> method : preDestroyMethods)
{
- preDestroy.invoke(instance);
+ if (method != null)
+ {
+ try
+ {
+ // note: RI supports injection into @PreDestroy
+ method.invoke(instance);
+ }
+ catch (Exception e)
+ {
+ throw new WeldException(INVOCATION_ERROR, e, method, instance);
+ }
+ }
}
- catch (Exception e)
- {
- throw new WeldException(ERROR_INVOKING_PRE_DESTROY, e, preDestroy);
- }
}
public void dispose(T instance)
Modified: core/trunk/impl/src/main/java/org/jboss/weld/util/Beans.java
===================================================================
--- core/trunk/impl/src/main/java/org/jboss/weld/util/Beans.java 2010-01-28 17:59:05 UTC
(rev 5656)
+++ core/trunk/impl/src/main/java/org/jboss/weld/util/Beans.java 2010-01-28 22:11:46 UTC
(rev 5657)
@@ -230,46 +230,50 @@
return injectionPoints;
}
- public static WeldMethod<?, ?> getPostConstruct(WeldClass<?> type)
+ public static <T> List<WeldMethod<?, ? super T>>
getPostConstructMethods(WeldClass<T> type)
{
- Set<WeldMethod<?, ?>> postConstructMethods =
type.getWeldMethods(PostConstruct.class);
- log.trace(FOUND_POST_CONSTRUCT_METHODS, postConstructMethods, type);
- if (postConstructMethods.size() > 1)
+ WeldClass<?> t = type;
+ List<WeldMethod<?, ? super T>> methods = new
ArrayList<WeldMethod<?, ? super T>>();
+ while (!t.getJavaClass().equals(Object.class))
{
- throw new DefinitionException(TOO_MANY_POST_CONSTRUCT_METHODS, type);
+ Set<WeldMethod<?, ? super T>> declaredMethods = (Set)
t.getDeclaredWeldMethods(PostConstruct.class);
+ log.trace(FOUND_POST_CONSTRUCT_METHODS, declaredMethods, type);
+ if (declaredMethods.size() > 1)
+ {
+ throw new DefinitionException(TOO_MANY_POST_CONSTRUCT_METHODS, type);
+ }
+ else if (declaredMethods.size() == 1)
+ {
+ WeldMethod<?, ? super T> method = declaredMethods.iterator().next();
+ log.trace(FOUND_ONE_POST_CONSTRUCT_METHOD, method, type);
+ methods.add(0, method);
+ }
+ t = t.getWeldSuperclass();
}
- else if (postConstructMethods.size() == 1)
- {
- WeldMethod<?, ?> postConstruct = postConstructMethods.iterator().next();
- log.trace(FOUND_ONE_POST_CONSTRUCT_METHOD, postConstruct, type);
- return postConstruct;
- }
- else
- {
- return null;
- }
+ return methods;
}
- public static WeldMethod<?, ?> getPreDestroy(WeldClass<?> type)
+ public static <T> List<WeldMethod<?, ? super T>>
getPreDestroyMethods(WeldClass<T> type)
{
- Set<WeldMethod<?, ?>> preDestroyMethods =
type.getWeldMethods(PreDestroy.class);
- log.trace(FOUND_PRE_DESTROY_METHODS, preDestroyMethods, type);
- if (preDestroyMethods.size() > 1)
+ WeldClass<?> t = type;
+ List<WeldMethod<?, ? super T>> methods = new
ArrayList<WeldMethod<?, ? super T>>();
+ while (!t.getJavaClass().equals(Object.class))
{
- // TODO actually this is wrong, in EJB you can have @PreDestroy methods
- // on the superclass, though the CDI spec is silent on the issue
- throw new DefinitionException(TOO_MANY_PRE_DESTROY_METHODS, type);
+ Set<WeldMethod<?, ? super T>> declaredMethods = (Set)
t.getDeclaredWeldMethods(PreDestroy.class);
+ log.trace(FOUND_PRE_DESTROY_METHODS, declaredMethods, type);
+ if (declaredMethods.size() > 1)
+ {
+ throw new DefinitionException(TOO_MANY_PRE_DESTROY_METHODS, type);
+ }
+ else if (declaredMethods.size() == 1)
+ {
+ WeldMethod<?, ? super T> method = declaredMethods.iterator().next();
+ log.trace(FOUND_ONE_PRE_DESTROY_METHOD, method, type);
+ methods.add(0, method);
+ }
+ t = t.getWeldSuperclass();
}
- else if (preDestroyMethods.size() == 1)
- {
- WeldMethod<?, ?> preDestroy = preDestroyMethods.iterator().next();
- log.trace(FOUND_ONE_PRE_DESTROY_METHOD, preDestroy, type);
- return preDestroy;
- }
- else
- {
- return null;
- }
+ return methods;
}
public static List<WeldMethod<?,?>>
getInterceptableMethods(WeldClass<?> type)