[webbeans-commits] Webbeans SVN: r3854 - in ri/trunk: tests/src/test/java/org/jboss/webbeans/test/unit/interceptor/simple and 1 other directory.

webbeans-commits at lists.jboss.org webbeans-commits at lists.jboss.org
Mon Oct 5 11:05:55 EDT 2009


Author: marius.bogoevici
Date: 2009-10-05 11:05:54 -0400 (Mon, 05 Oct 2009)
New Revision: 3854

Added:
   ri/trunk/tests/src/test/java/org/jboss/webbeans/test/unit/interceptor/simple/SimpleBeanWithStereotype.java
   ri/trunk/tests/src/test/java/org/jboss/webbeans/test/unit/interceptor/simple/SimpleInterceptorStereotype.java
Modified:
   ri/trunk/impl/src/main/java/org/jboss/webbeans/bean/InterceptorImpl.java
   ri/trunk/impl/src/main/java/org/jboss/webbeans/bean/ManagedBean.java
   ri/trunk/tests/src/test/java/org/jboss/webbeans/test/unit/interceptor/simple/SimpleInterceptorTest.java
Log:
Implement intercept() on Interceptor, correct handling of meta-annotations (on both classes and interceptors), adding support for stereotypes + test.

Modified: ri/trunk/impl/src/main/java/org/jboss/webbeans/bean/InterceptorImpl.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/bean/InterceptorImpl.java	2009-10-05 14:27:13 UTC (rev 3853)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/bean/InterceptorImpl.java	2009-10-05 15:05:54 UTC (rev 3854)
@@ -19,6 +19,7 @@
 
 import org.jboss.interceptor.model.InterceptorClassMetadata;
 import org.jboss.interceptor.registry.InterceptorClassMetadataRegistry;
+import org.jboss.interceptor.proxy.DirectClassInterceptionHandler;
 import org.jboss.webbeans.BeanManagerImpl;
 import org.jboss.webbeans.introspector.WBClass;
 import org.jboss.webbeans.metadata.cache.MetaAnnotationStore;
@@ -43,16 +44,7 @@
    {
       super(type, new StringBuilder().append(Interceptor.class.getSimpleName()).append(BEAN_ID_SEPARATOR).append(type.getName()).toString(), manager);
       this.interceptorClassMetadata = InterceptorClassMetadataRegistry.getRegistry().getInterceptorClassMetadata(type.getJavaClass());
-      this.interceptorBindingTypes = new HashSet<Annotation>();
-      for (Annotation annotation: getAnnotatedItem().getAnnotations())
-      {
-         if (manager.isInterceptorBindingType(annotation.annotationType()))
-         {
-            interceptorBindingTypes.add(annotation);
-            interceptorBindingTypes.addAll(getManager().getServices().get(MetaAnnotationStore.class).getInterceptorBindingModel(annotation.annotationType()).getInheritedInterceptionBindingTypes());
-         }
-      }
-      
+      this.interceptorBindingTypes = flattenInterceptorBindings(manager, getAnnotatedItem().getAnnotations());
    }
 
    public static <T> InterceptorImpl<T> of(WBClass<T> type, BeanManagerImpl manager)
@@ -69,7 +61,7 @@
    {
       try
       {
-         return ctx.proceed();
+         return new DirectClassInterceptionHandler(instance, getType()).invoke(ctx.getTarget(), org.jboss.interceptor.model.InterceptionType.valueOf(type.name()), ctx);
       } catch (Exception e)
       {
          throw new RuntimeException(e);

Modified: ri/trunk/impl/src/main/java/org/jboss/webbeans/bean/ManagedBean.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/bean/ManagedBean.java	2009-10-05 14:27:13 UTC (rev 3853)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/bean/ManagedBean.java	2009-10-05 15:05:54 UTC (rev 3854)
@@ -28,6 +28,7 @@
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Set;
+import java.util.HashSet;
 
 import org.jboss.interceptor.model.InterceptionModelBuilder;
 import org.jboss.interceptor.proxy.DirectClassInterceptionHandler;
@@ -226,14 +227,10 @@
    {
       InterceptionModelBuilder<Class<?>, Interceptor> builder = InterceptionModelBuilder.newBuilderFor(getType(), (Class) Interceptor.class);
 
-      List<Annotation> classBindingAnnotations = new ArrayList<Annotation>();
-
-      for (Annotation annotation : getType().getAnnotations())
+      Set<Annotation> classBindingAnnotations = flattenInterceptorBindings(manager, annotatedItem.getAnnotations());
+       for (Class<? extends Annotation> annotation: getStereotypes())
       {
-         if (manager.isInterceptorBindingType(annotation.annotationType()))
-         {
-            classBindingAnnotations.add(annotation);
-         }
+          classBindingAnnotations.addAll(flattenInterceptorBindings(manager, manager.getStereotypeDefinition(annotation)));
       }
 
       builder.interceptPostConstruct().with(manager.resolveInterceptors(InterceptionType.POST_CONSTRUCT, classBindingAnnotations.toArray(new Annotation[0])).toArray(new Interceptor<?>[]{}));
@@ -243,14 +240,7 @@
       for (WBMethod<?, ?> method : businessMethods)
       {
          List<Annotation> methodBindingAnnotations = new ArrayList<Annotation>(classBindingAnnotations);
-         for (Annotation annotation : method.getAnnotations())
-         {
-            if (manager.isInterceptorBindingType(annotation.annotationType()))
-            {
-               methodBindingAnnotations.add(annotation);
-               methodBindingAnnotations.addAll(manager.getServices().get(MetaAnnotationStore.class).getInterceptorBindingModel(annotation.annotationType()).getInheritedInterceptionBindingTypes());
-            }
-         }
+         methodBindingAnnotations.addAll(flattenInterceptorBindings(manager, method.getAnnotations()));
          List<Interceptor<?>> methodBoundInterceptors = manager.resolveInterceptors(InterceptionType.AROUND_INVOKE, methodBindingAnnotations.toArray(new Annotation[]{}));
          builder.interceptAroundInvoke(((AnnotatedMethod)method).getJavaMember()).with(methodBoundInterceptors.toArray(new Interceptor[]{}));
       }
@@ -446,4 +436,24 @@
       return instance;
    }
 
+   /**
+    * Extracts the complete set of interception bindings from a given set of annotations.
+    * 
+    * @param manager
+    * @param annotations
+    * @return
+    */
+   protected static Set<Annotation> flattenInterceptorBindings(BeanManagerImpl manager, Set<Annotation> annotations)
+   {
+      Set<Annotation> foundInterceptionBindingTypes = new HashSet<Annotation>();
+      for (Annotation annotation: annotations)
+      {
+         if (manager.isInterceptorBindingType(annotation.annotationType()))
+         {
+            foundInterceptionBindingTypes.add(annotation);
+            foundInterceptionBindingTypes.addAll(manager.getServices().get(MetaAnnotationStore.class).getInterceptorBindingModel(annotation.annotationType()).getInheritedInterceptionBindingTypes());
+         }
+      }
+      return foundInterceptionBindingTypes;
+   }
 }

Copied: ri/trunk/tests/src/test/java/org/jboss/webbeans/test/unit/interceptor/simple/SimpleBeanWithStereotype.java (from rev 3846, ri/trunk/tests/src/test/java/org/jboss/webbeans/test/unit/interceptor/simple/SimpleBeanImpl.java)
===================================================================
--- ri/trunk/tests/src/test/java/org/jboss/webbeans/test/unit/interceptor/simple/SimpleBeanWithStereotype.java	                        (rev 0)
+++ ri/trunk/tests/src/test/java/org/jboss/webbeans/test/unit/interceptor/simple/SimpleBeanWithStereotype.java	2009-10-05 15:05:54 UTC (rev 3854)
@@ -0,0 +1,45 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat, Inc. and/or its affiliates, 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.test.unit.interceptor.simple;
+
+import javax.annotation.PostConstruct;
+
+/**
+ * @author <a href="mailto:mariusb at redhat.com">Marius Bogoevici</a>
+ */
+ at SimpleInterceptorStereotype
+public class SimpleBeanWithStereotype
+{
+
+   public static boolean businessMethodInvoked = false;
+
+   public static boolean postConstructCalled = false;
+
+   public String doSomething()
+   {
+      businessMethodInvoked = true;
+      return "Hello!";
+   }
+
+   @PostConstruct
+   public void doPostConstruct()
+   {
+      postConstructCalled = true;
+   }
+
+}
\ No newline at end of file

Added: ri/trunk/tests/src/test/java/org/jboss/webbeans/test/unit/interceptor/simple/SimpleInterceptorStereotype.java
===================================================================
--- ri/trunk/tests/src/test/java/org/jboss/webbeans/test/unit/interceptor/simple/SimpleInterceptorStereotype.java	                        (rev 0)
+++ ri/trunk/tests/src/test/java/org/jboss/webbeans/test/unit/interceptor/simple/SimpleInterceptorStereotype.java	2009-10-05 15:05:54 UTC (rev 3854)
@@ -0,0 +1,35 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat, Inc. and/or its affiliates, 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.test.unit.interceptor.simple;
+
+import javax.enterprise.inject.stereotype.Stereotype;
+import static java.lang.annotation.ElementType.TYPE;
+import java.lang.annotation.Retention;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+import java.lang.annotation.Target;
+
+/**
+ * @author <a href="mailto:mariusb at redhat.com">Marius Bogoevici</a>
+ */
+ at Stereotype
+ at SecondaryInterceptionBinding
+ at Target(TYPE)
+ at Retention(RUNTIME)
+public @interface SimpleInterceptorStereotype
+{
+}

Modified: ri/trunk/tests/src/test/java/org/jboss/webbeans/test/unit/interceptor/simple/SimpleInterceptorTest.java
===================================================================
--- ri/trunk/tests/src/test/java/org/jboss/webbeans/test/unit/interceptor/simple/SimpleInterceptorTest.java	2009-10-05 14:27:13 UTC (rev 3853)
+++ ri/trunk/tests/src/test/java/org/jboss/webbeans/test/unit/interceptor/simple/SimpleInterceptorTest.java	2009-10-05 15:05:54 UTC (rev 3854)
@@ -61,6 +61,22 @@
       assert !SimpleInterceptor.preDestroyCalled;
       assert TwoBindingsInterceptor.aroundInvokeCalled;
       assert SimpleBeanImpl.postConstructCalled;
+   }
 
+   @Test
+   public void testSimpleInterceptorWithStereotype()
+   {
+      Bean bean = getCurrentManager().getBeans(SimpleBeanWithStereotype.class).iterator().next();
+      CreationalContext creationalContext = getCurrentManager().createCreationalContext(bean);
+      SimpleBeanWithStereotype simpleBean = (SimpleBeanWithStereotype)bean.create(creationalContext);
+      String result = simpleBean.doSomething();
+      assert "Hello!".equals(result);
+      bean.destroy(simpleBean, creationalContext);
+      assert SimpleInterceptor.aroundInvokeCalled;
+      assert SimpleInterceptor.postConstructCalled;
+      assert SimpleInterceptor.preDestroyCalled;
+      assert TwoBindingsInterceptor.aroundInvokeCalled;
+      assert SimpleBeanWithStereotype.postConstructCalled;
+
    }
 }




More information about the weld-commits mailing list