[weld-commits] Weld SVN: r6800 - in extensions/trunk/impl/src: main/java/org/jboss/weld/extensions/util and 1 other directories.

weld-commits at lists.jboss.org weld-commits at lists.jboss.org
Sat Jul 24 10:17:57 EDT 2010


Author: pete.muir at jboss.org
Date: 2010-07-24 10:17:57 -0400 (Sat, 24 Jul 2010)
New Revision: 6800

Modified:
   extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericBeanExtension.java
   extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/util/Reflections.java
   extensions/trunk/impl/src/test/java/org/jboss/weld/extensions/test/bean/generic/Baz.java
   extensions/trunk/impl/src/test/java/org/jboss/weld/extensions/test/bean/generic/GenericBeanProducer.java
   extensions/trunk/impl/src/test/java/org/jboss/weld/extensions/test/bean/generic/GenericBeanTest.java
Log:
tests for initializer and ctor injection for generic beans

Modified: extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericBeanExtension.java
===================================================================
--- extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericBeanExtension.java	2010-07-24 13:59:59 UTC (rev 6799)
+++ extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericBeanExtension.java	2010-07-24 14:17:57 UTC (rev 6800)
@@ -16,6 +16,8 @@
  */
 package org.jboss.weld.extensions.bean.generic;
 
+import static org.jboss.weld.extensions.util.Reflections.getRawType;
+
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Member;
 import java.lang.reflect.Type;
@@ -208,7 +210,7 @@
          if (field.isAnnotationPresent(Inject.class))
          {
             // if this is a configuration injection point
-            if (concrete.annotationType().isAssignableFrom(field.getJavaMember().getType()))
+            if (concrete.annotationType().isAssignableFrom(getRawType(field.getBaseType())))
             {
                Synthetic genericConfigurationQualifier = syntheticProvider.get();
                builder.addToField(field, genericConfigurationQualifier);
@@ -228,6 +230,14 @@
          {
             for (AnnotatedParameter<? super X> parameter : method.getParameters())
             {
+               // if this is a configuration injection point
+               if (concrete.annotationType().isAssignableFrom(getRawType(parameter.getBaseType())))
+               {
+                  Synthetic genericConfigurationQualifier = syntheticProvider.get();
+                  builder.addToParameter(parameter, genericConfigurationQualifier);
+                  event.addBean(createConfigurationBean(beanManager, concrete, genericConfigurationQualifier));
+               }
+               // if this is a generic bean injection point
                if (parameter.isAnnotationPresent(GenericBean.class))
                {
                   builder.removeFromParameter(parameter, GenericBean.class);
@@ -243,6 +253,14 @@
          {
             for (AnnotatedParameter<X> parameter : constructor.getParameters())
             {
+               // if this is a configuration injection point
+               if (concrete.annotationType().isAssignableFrom(getRawType(parameter.getBaseType())))
+               {
+                  Synthetic genericConfigurationQualifier = syntheticProvider.get();
+                  builder.addToParameter(parameter, genericConfigurationQualifier);
+                  event.addBean(createConfigurationBean(beanManager, concrete, genericConfigurationQualifier));
+               }
+               // if this is a generic bean injection point
                if (parameter.isAnnotationPresent(GenericBean.class))
                {
                   builder.removeFromParameter(parameter, GenericBean.class);
@@ -257,21 +275,20 @@
 
    private Bean<Annotation> createConfigurationBean(BeanManager beanManager, final Annotation genericConfiguration, Annotation syntheticQualifier)
    {
-      BeanBuilder<Annotation> builder = new BeanBuilder<Annotation>(beanManager).setJavaClass(genericConfiguration.annotationType()).setTypes(Arrays2.<Type> asSet(genericConfiguration.annotationType(), Object.class)).setScope(Dependent.class).setQualifiers(Arrays2.asSet(syntheticQualifier))
       // TODO make this passivation capable?
-            .setBeanLifecycle(new BeanLifecycle<Annotation>()
-            {
+      BeanBuilder<Annotation> builder = new BeanBuilder<Annotation>(beanManager).setJavaClass(genericConfiguration.annotationType()).setTypes(Arrays2.<Type> asSet(genericConfiguration.annotationType(), Object.class)).setScope(Dependent.class).setQualifiers(Arrays2.asSet(syntheticQualifier)).setBeanLifecycle(new BeanLifecycle<Annotation>()
+      {
 
-               public void destroy(Bean<Annotation> bean, Annotation arg0, CreationalContext<Annotation> arg1)
-               {
-                  // No-op
-               }
+         public void destroy(Bean<Annotation> bean, Annotation arg0, CreationalContext<Annotation> arg1)
+         {
+            // No-op
+         }
 
-               public Annotation create(Bean<Annotation> bean, CreationalContext<Annotation> arg0)
-               {
-                  return genericConfiguration;
-               }
-            });
+         public Annotation create(Bean<Annotation> bean, CreationalContext<Annotation> arg0)
+         {
+            return genericConfiguration;
+         }
+      });
 
       return builder.create();
    }

Modified: extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/util/Reflections.java
===================================================================
--- extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/util/Reflections.java	2010-07-24 13:59:59 UTC (rev 6799)
+++ extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/util/Reflections.java	2010-07-24 14:17:57 UTC (rev 6800)
@@ -22,6 +22,8 @@
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Member;
 import java.lang.reflect.Method;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
 import java.util.HashSet;
 import java.util.Set;
 
@@ -355,5 +357,22 @@
          throw new RuntimeException(buildInvokeMethodErrorMessage(method, obj, args), e);
       }
    }
+   
+   @SuppressWarnings("unchecked")
+   public static <T> Class<T> getRawType(Type type)
+   {
+      if (type instanceof Class<?>)
+      {
+         return (Class<T>) type;
+      }
+      else if (type instanceof ParameterizedType)
+      {
+         if (((ParameterizedType) type).getRawType() instanceof Class<?>)
+         {
+            return (Class<T>) ((ParameterizedType) type).getRawType();
+         }
+      }
+      return null;
+   }
 
 }

Modified: extensions/trunk/impl/src/test/java/org/jboss/weld/extensions/test/bean/generic/Baz.java
===================================================================
--- extensions/trunk/impl/src/test/java/org/jboss/weld/extensions/test/bean/generic/Baz.java	2010-07-24 13:59:59 UTC (rev 6799)
+++ extensions/trunk/impl/src/test/java/org/jboss/weld/extensions/test/bean/generic/Baz.java	2010-07-24 14:17:57 UTC (rev 6800)
@@ -22,7 +22,7 @@
 import org.jboss.weld.extensions.bean.generic.GenericBean;
 
 /**
- * A generic bean for the config annotation Garply
+ * A generic bean for the config annotation Message
  * 
  * @author pmuir
  *

Modified: extensions/trunk/impl/src/test/java/org/jboss/weld/extensions/test/bean/generic/GenericBeanProducer.java
===================================================================
--- extensions/trunk/impl/src/test/java/org/jboss/weld/extensions/test/bean/generic/GenericBeanProducer.java	2010-07-24 13:59:59 UTC (rev 6799)
+++ extensions/trunk/impl/src/test/java/org/jboss/weld/extensions/test/bean/generic/GenericBeanProducer.java	2010-07-24 14:17:57 UTC (rev 6800)
@@ -37,14 +37,40 @@
    @Message("hello2")
    private Baz baz2;
    
+   @SuppressWarnings("unused")
    @Foo(1)
    @Produces
    @Message("bye1")
    private Bar bar1;
    
+   @SuppressWarnings("unused")
    @Foo(2)
    @Produces
    @Message("bye2")
    private Bar bar2;
+   
+   @SuppressWarnings("unused")
+   @Foo(1)
+   @Produces
+   @Message("adios1")
+   private Qux qux1;
+   
+   @SuppressWarnings("unused")
+   @Foo(2)
+   @Produces
+   @Message("adios2")
+   private Qux qux2;
+   
+   @SuppressWarnings("unused")
+   @Foo(1)
+   @Produces
+   @Message("aurevoir1")
+   private Garply garply1;
+   
+   @SuppressWarnings("unused")
+   @Foo(2)
+   @Produces
+   @Message("aurevoir2")
+   private Garply garply2;
 
 }

Modified: extensions/trunk/impl/src/test/java/org/jboss/weld/extensions/test/bean/generic/GenericBeanTest.java
===================================================================
--- extensions/trunk/impl/src/test/java/org/jboss/weld/extensions/test/bean/generic/GenericBeanTest.java	2010-07-24 13:59:59 UTC (rev 6799)
+++ extensions/trunk/impl/src/test/java/org/jboss/weld/extensions/test/bean/generic/GenericBeanTest.java	2010-07-24 14:17:57 UTC (rev 6800)
@@ -54,6 +54,22 @@
    @Inject
    @Foo(2)
    private Bar bar2;
+   
+   @Inject
+   @Foo(1)
+   private Qux qux1;
+   
+   @Inject
+   @Foo(2)
+   private Qux qux2;
+   
+   @Inject
+   @Foo(1)
+   private Garply garply1;
+   
+   @Inject
+   @Foo(2)
+   private Garply garply2;
 
    @Test
    public void testGeneric()
@@ -74,6 +90,18 @@
       assertNotNull(baz2.getMessage());
       assertEquals(baz2.getMessage().value(), "hello2");
       
+      // Check that the generic configuration injection wiring is working for qux (ctor injection)
+      assertNotNull(qux1.getMessage());
+      assertEquals(qux1.getMessage().value(), "adios1");
+      assertNotNull(qux2.getMessage());
+      assertEquals(qux2.getMessage().value(), "adios2");
+      
+   // Check that the generic configuration injection wiring is working for garply (initializer injection)
+      assertNotNull(garply1.getMessage());
+      assertEquals(garply1.getMessage().value(), "aurevoir1");
+      assertNotNull(garply2.getMessage());
+      assertEquals(garply2.getMessage().value(), "aurevoir2");
+      
       // Check that generic beans can inject each other
       assertNotNull(baz1.getBar());
       assertNotNull(baz1.getBar().getInjectedMessage());
@@ -82,6 +110,22 @@
       assertNotNull(baz2.getBar().getInjectedMessage());
       assertEquals(baz2.getBar().getInjectedMessage().value(), "hello2");
       
+      // Check for ctor injection
+      assertNotNull(qux1.getBar());
+      assertNotNull(qux1.getBar().getInjectedMessage());
+      assertEquals(qux1.getBar().getInjectedMessage().value(), "adios1");
+      assertNotNull(qux2.getBar());
+      assertNotNull(qux2.getBar().getInjectedMessage());
+      assertEquals(qux2.getBar().getInjectedMessage().value(), "adios2");
+      
+      // Check for initializer injection
+      assertNotNull(garply1.getQux());
+      assertNotNull(garply1.getQux().getMessage());
+      assertEquals(garply1.getQux().getMessage().value(), "aurevoir1");
+      assertNotNull(garply2.getQux());
+      assertNotNull(garply2.getQux().getMessage());
+      assertEquals(garply2.getQux().getMessage().value(), "aurevoir2");
+      
       // Check that this isn't affecting annotations on the generic bean without @Inject 
       assertNull(baz1.getBar().getMessage());
       assertNull(baz2.getBar().getMessage());



More information about the weld-commits mailing list