Author: marius.bogoevici
Date: 2009-10-19 03:38:42 -0400 (Mon, 19 Oct 2009)
New Revision: 4191
Modified:
core/trunk/impl/src/main/java/org/jboss/weld/Validator.java
core/trunk/impl/src/main/java/org/jboss/weld/bean/AbstractClassBean.java
core/trunk/impl/src/main/java/org/jboss/weld/bean/ManagedBean.java
core/trunk/impl/src/main/java/org/jboss/weld/metadata/cache/InterceptorBindingModel.java
Log:
Scan EJB3-style interceptors for EJBs as well (don't handle them), so that we can
validate passivation inconsistencies.
Make sure that interceptor bindings are recognized according to the spec wrt @Target (only
METHOD and TYPE), meta-binding rules are observed.
Modified: core/trunk/impl/src/main/java/org/jboss/weld/Validator.java
===================================================================
--- core/trunk/impl/src/main/java/org/jboss/weld/Validator.java 2009-10-19 07:35:37 UTC
(rev 4190)
+++ core/trunk/impl/src/main/java/org/jboss/weld/Validator.java 2009-10-19 07:38:42 UTC
(rev 4191)
@@ -122,7 +122,7 @@
validateCdiBoundInterceptors(beanManager, classBean);
}
// validate EJB-defined interceptors
- if (classBean instanceof ManagedBean &&
((ManagedBean)classBean).hasDirectlyDefinedInterceptors())
+ if (((AbstractClassBean<?>) bean).hasDirectlyDefinedInterceptors())
{
validateDirectlyDefinedInterceptorClasses(beanManager, classBean);
}
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 2009-10-19
07:35:37 UTC (rev 4190)
+++ core/trunk/impl/src/main/java/org/jboss/weld/bean/AbstractClassBean.java 2009-10-19
07:38:42 UTC (rev 4191)
@@ -40,6 +40,9 @@
import javax.inject.Scope;
import org.jboss.interceptor.model.InterceptionModelBuilder;
+import org.jboss.interceptor.model.InterceptionModel;
+import org.jboss.interceptor.model.InterceptorClassMetadataImpl;
+import org.jboss.interceptor.util.InterceptionUtils;
import org.jboss.weld.BeanManagerImpl;
import org.jboss.weld.DefinitionException;
import org.jboss.weld.DeploymentException;
@@ -57,6 +60,7 @@
import org.jboss.weld.util.Beans;
import org.jboss.weld.util.Proxies;
import org.jboss.weld.util.Strings;
+import org.jboss.weld.util.Reflections;
/**
* An abstract bean representation common for class-based beans
@@ -125,7 +129,10 @@
checkType();
initProxyClassForDecoratedBean();
if (isInterceptionCandidate())
- initInterceptors();
+ {
+ initCdiBoundInterceptors();
+ initDirectlyDefinedInterceptors();
+ }
}
protected void checkType()
@@ -394,9 +401,7 @@
return foundInterceptionBindingTypes;
}
-
-
- protected void initInterceptors()
+ protected void initCdiBoundInterceptors()
{
if (manager.getCdiInterceptorsRegistry().getInterceptionModel(getType()) == null)
{
@@ -440,7 +445,12 @@
builder.interceptAroundInvoke(((AnnotatedMethod)
method).getJavaMember()).with(toSerializableContextualArray(methodBoundInterceptors));
}
}
- manager.getCdiInterceptorsRegistry().registerInterceptionModel(getType(),
builder.build());
+
InterceptionModel<Class<?>,SerializableContextual<Interceptor<?>,?>>
serializableContextualInterceptionModel = builder.build();
+ // if there is at least one applicable interceptor, register it
+ if (serializableContextualInterceptionModel.getAllInterceptors().size() > 0)
+ {
+ manager.getCdiInterceptorsRegistry().registerInterceptionModel(getType(),
serializableContextualInterceptionModel);
+ }
}
}
@@ -511,5 +521,55 @@
return false;
}
+ public boolean hasDirectlyDefinedInterceptors()
+ {
+ if (manager.getClassDeclaredInterceptorsRegistry().getInterceptionModel(getType())
!= null)
+ return
manager.getClassDeclaredInterceptorsRegistry().getInterceptionModel(getType()).getAllInterceptors().size()
> 0;
+ else
+ return false;
+ }
+ protected void initDirectlyDefinedInterceptors()
+ {
+ if (manager.getClassDeclaredInterceptorsRegistry().getInterceptionModel(getType())
== null && InterceptionUtils.supportsEjb3InterceptorDeclaration())
+ {
+ InterceptionModelBuilder<Class<?>, Class<?>> builder =
InterceptionModelBuilder.newBuilderFor(getType(), (Class) Class.class);
+
+ Class<?>[] classDeclaredInterceptors = null;
+ if
(getAnnotatedItem().isAnnotationPresent(InterceptionUtils.getInterceptorsAnnotationClass()))
+ {
+ Annotation interceptorsAnnotation =
getType().getAnnotation(InterceptionUtils.getInterceptorsAnnotationClass());
+ classDeclaredInterceptors =
Reflections.extractValues(interceptorsAnnotation);
+ }
+
+ if (classDeclaredInterceptors != null)
+ {
+ builder.interceptAll().with(classDeclaredInterceptors);
+ }
+
+ List<WeldMethod<?, ?>> businessMethods =
Beans.getInterceptableBusinessMethods(getAnnotatedItem());
+ for (WeldMethod<?, ?> method : businessMethods)
+ {
+ boolean excludeClassInterceptors =
method.isAnnotationPresent(InterceptionUtils.getExcludeClassInterceptorsAnnotationClass());
+ Class<?>[] methodDeclaredInterceptors = null;
+ if
(method.isAnnotationPresent(InterceptionUtils.getInterceptorsAnnotationClass()))
+ {
+ methodDeclaredInterceptors =
Reflections.extractValues(method.getAnnotation(InterceptionUtils.getInterceptorsAnnotationClass()));
+ }
+ if (excludeClassInterceptors)
+ {
+
builder.ignoreGlobalInterceptors(((AnnotatedMethod)method).getJavaMember());
+ }
+ if (methodDeclaredInterceptors != null)
+ {
+ builder.interceptAroundInvoke(((AnnotatedMethod)
method).getJavaMember()).with(methodDeclaredInterceptors);
+ }
+ }
+ InterceptionModel<Class<?>, Class<?>> interceptionModel =
builder.build();
+ if (interceptionModel.getAllInterceptors().size() > 0 || new
InterceptorClassMetadataImpl(getType()).isInterceptor())
+
manager.getClassDeclaredInterceptorsRegistry().registerInterceptionModel(getType(),
builder.build());
+ }
+ }
+
+
}
Modified: core/trunk/impl/src/main/java/org/jboss/weld/bean/ManagedBean.java
===================================================================
--- core/trunk/impl/src/main/java/org/jboss/weld/bean/ManagedBean.java 2009-10-19 07:35:37
UTC (rev 4190)
+++ core/trunk/impl/src/main/java/org/jboss/weld/bean/ManagedBean.java 2009-10-19 07:38:42
UTC (rev 4191)
@@ -186,7 +186,7 @@
initEEInjectionPoints();
if (isInterceptionCandidate())
{
- initClassInterceptors();
+ initDirectlyDefinedInterceptors();
}
setInjectionTarget(new InjectionTarget<T>()
{
@@ -425,14 +425,6 @@
return !Beans.isInterceptor(getAnnotatedItem()) &&
!Beans.isDecorator(getAnnotatedItem());
}
- public boolean hasDirectlyDefinedInterceptors()
- {
- if (manager.getClassDeclaredInterceptorsRegistry().getInterceptionModel(getType())
!= null)
- return
manager.getClassDeclaredInterceptorsRegistry().getInterceptionModel(getType()).getAllInterceptors().size()
> 0;
- else
- return false;
- }
-
protected T applyInterceptors(T instance, final CreationalContext<T>
creationalContext)
{
try
@@ -459,49 +451,4 @@
return instance;
}
- protected void initClassInterceptors()
- {
- if (manager.getClassDeclaredInterceptorsRegistry().getInterceptionModel(getType())
== null && InterceptionUtils.supportsEjb3InterceptorDeclaration())
- {
- InterceptionModelBuilder<Class<?>, Class<?>> builder =
InterceptionModelBuilder.newBuilderFor(getType(), (Class) Class.class);
-
- Class<?>[] classDeclaredInterceptors = null;
- if
(getAnnotatedItem().isAnnotationPresent(InterceptionUtils.getInterceptorsAnnotationClass()))
- {
- Annotation interceptorsAnnotation =
getType().getAnnotation(InterceptionUtils.getInterceptorsAnnotationClass());
- classDeclaredInterceptors =
Reflections.extractValues(interceptorsAnnotation);
- }
-
- if (classDeclaredInterceptors != null)
- {
- builder.interceptPostConstruct().with(classDeclaredInterceptors);
- builder.interceptPreDestroy().with(classDeclaredInterceptors);
- builder.interceptPrePassivate().with(classDeclaredInterceptors);
- builder.interceptPostActivate().with(classDeclaredInterceptors);
- }
-
- List<WeldMethod<?, ?>> businessMethods =
Beans.getInterceptableBusinessMethods(getAnnotatedItem());
- for (WeldMethod<?, ?> method : businessMethods)
- {
- boolean excludeClassInterceptors =
method.isAnnotationPresent(InterceptionUtils.getExcludeClassInterceptorsAnnotationClass());
- Class<?>[] methodDeclaredInterceptors = null;
- if
(method.isAnnotationPresent(InterceptionUtils.getInterceptorsAnnotationClass()))
- {
- methodDeclaredInterceptors =
Reflections.extractValues(method.getAnnotation(InterceptionUtils.getInterceptorsAnnotationClass()));
- }
- if (!excludeClassInterceptors && classDeclaredInterceptors != null)
- {
- builder.interceptAroundInvoke(((AnnotatedMethod)
method).getJavaMember()).with(classDeclaredInterceptors);
- }
- if (methodDeclaredInterceptors != null)
- {
- builder.interceptAroundInvoke(((AnnotatedMethod)
method).getJavaMember()).with(methodDeclaredInterceptors);
- }
- }
- InterceptionModel<Class<?>, Class<?>> interceptionModel =
builder.build();
- if (interceptionModel.getAllInterceptors().size() > 0 || new
InterceptorClassMetadataImpl(getType()).isInterceptor())
-
manager.getClassDeclaredInterceptorsRegistry().registerInterceptionModel(getType(),
builder.build());
- }
- }
-
}
Modified:
core/trunk/impl/src/main/java/org/jboss/weld/metadata/cache/InterceptorBindingModel.java
===================================================================
---
core/trunk/impl/src/main/java/org/jboss/weld/metadata/cache/InterceptorBindingModel.java 2009-10-19
07:35:37 UTC (rev 4190)
+++
core/trunk/impl/src/main/java/org/jboss/weld/metadata/cache/InterceptorBindingModel.java 2009-10-19
07:38:42 UTC (rev 4191)
@@ -22,10 +22,16 @@
import org.jboss.weld.log.Logging;
import org.jboss.weld.resources.ClassTransformer;
import org.jboss.weld.util.collections.Arrays2;
+import org.jboss.weld.util.Reflections;
+import org.jboss.weld.DefinitionException;
import javax.interceptor.InterceptorBinding;
import javax.enterprise.inject.Nonbinding;
import java.lang.annotation.Annotation;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import java.lang.annotation.ElementType;
import java.lang.reflect.InvocationTargetException;
import java.util.Set;
@@ -43,9 +49,14 @@
public InterceptorBindingModel(Class<T> type, ClassTransformer transformer)
{
super(type, transformer);
- initNonBindingTypes();
- initInterceptionBindingTypes();
- this.metaAnnotations = getAnnotatedAnnotation().getAnnotations();
+ if (isValid())
+ {
+ initNonBindingTypes();
+ initInterceptionBindingTypes();
+ checkArrayAndAnnotationValuedMembers();
+ checkMetaAnnotations();
+ this.metaAnnotations = getAnnotatedAnnotation().getAnnotations();
+ }
}
protected Set<Class<? extends Annotation>> getMetaAnnotationTypes()
@@ -68,6 +79,54 @@
inheritedInterceptionBindingTypes =
getAnnotatedAnnotation().getMetaAnnotations(InterceptorBinding.class);
}
+ @Override
+ protected void initValid()
+ {
+ super.initValid();
+ if (!getAnnotatedAnnotation().isAnnotationPresent(Target.class))
+ {
+ this.valid = false;
+ log.debug("#0 is missing @Target", getAnnotatedAnnotation());
+ }
+ else
+ {
+ ElementType[] targetElementTypes =
getAnnotatedAnnotation().getAnnotation(Target.class).value();
+ if (!Arrays2.unorderedEquals(targetElementTypes, ElementType.TYPE,
ElementType.METHOD)
+ && !Arrays2.unorderedEquals(targetElementTypes,
ElementType.TYPE))
+ {
+ this.valid = false;
+ log.debug("#0 is not declared @Target(TYPE, METHOD) or
@Target(TYPE)");
+ }
+ }
+ }
+
+ private void checkMetaAnnotations()
+ {
+ if
(Arrays2.containsAll(getAnnotatedAnnotation().getAnnotation(Target.class).value(),
ElementType.METHOD))
+ {
+ for (Annotation inheritedBinding: getInheritedInterceptionBindingTypes())
+ {
+ if
(!Arrays2.containsAll(inheritedBinding.annotationType().getAnnotation(Target.class).value(),
ElementType.METHOD))
+ {
+ this.valid = false;
+ log.debug("#0 is declared @Target(TYPE, METHOD), but inherits #1,
which is declared @Target(TYPE)",
+ getAnnotatedAnnotation(), inheritedBinding);
+ }
+ }
+ }
+ }
+
+ private void checkArrayAndAnnotationValuedMembers()
+ {
+ for (WeldMethod<?, ?> annotatedMethod :
getAnnotatedAnnotation().getMembers())
+ {
+ if ((Reflections.isArrayType(annotatedMethod.getJavaClass()) ||
Annotation.class.isAssignableFrom(annotatedMethod.getJavaClass())) &&
!nonBindingTypes.contains(annotatedMethod))
+ {
+ throw new DefinitionException("Member of array type or annotation type
must be annotated @NonBinding " + annotatedMethod);
+ }
+ }
+ }
+
public Set<Annotation> getInheritedInterceptionBindingTypes()
{
return inheritedInterceptionBindingTypes;