[webbeans-commits] Webbeans SVN: r457 - ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean.

webbeans-commits at lists.jboss.org webbeans-commits at lists.jboss.org
Sun Dec 7 15:38:40 EST 2008


Author: gavin.king at jboss.com
Date: 2008-12-07 15:38:40 -0500 (Sun, 07 Dec 2008)
New Revision: 457

Modified:
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/ProducerBean.java
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/ProducerFieldBean.java
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/ProducerMethodBean.java
Log:
allow static producer methods/fields and refactor slightly

Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/ProducerBean.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/ProducerBean.java	2008-12-07 20:21:17 UTC (rev 456)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/ProducerBean.java	2008-12-07 20:38:40 UTC (rev 457)
@@ -1,8 +1,13 @@
 package org.jboss.webbeans.bean;
 
 import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
 import java.util.HashSet;
 
+import javax.webbeans.DefinitionException;
+import javax.webbeans.Dependent;
+import javax.webbeans.IllegalProductException;
+
 import org.jboss.webbeans.ManagerImpl;
 
 public abstract class ProducerBean<T, S> extends AbstractBean<T, S> {
@@ -69,4 +74,38 @@
       return declaringBean;
    }
 
+   /**
+    * Validates the producer method
+    */
+   protected void checkProducerReturnType() {
+      for (Type type : getAnnotatedItem().getActualTypeArguments())
+      {
+         if (!(type instanceof Class))
+         {
+            throw new DefinitionException("Producer type cannot be parameterized with type parameter or wildcard");
+         }
+      }
+   }
+
+   /**
+    * Initializes the bean and its metadata
+    */
+   @Override
+   protected void init() {
+      super.init();
+      checkProducerReturnType();
+   }
+
+   protected void checkReturnValue(T instance) {
+      if (instance == null && !getScopeType().equals(Dependent.class))
+      {
+         throw new IllegalProductException("Cannot return null from a non-dependent producer method");
+      }
+   }
+
+   protected Object getReceiver() {
+      return getAnnotatedItem().isStatic() ? 
+              null : manager.getInstance(getDeclaringBean());
+   }
+
 }
\ No newline at end of file

Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/ProducerFieldBean.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/ProducerFieldBean.java	2008-12-07 20:21:17 UTC (rev 456)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/ProducerFieldBean.java	2008-12-07 20:38:40 UTC (rev 457)
@@ -18,12 +18,7 @@
 package org.jboss.webbeans.bean;
 
 import java.lang.reflect.Field;
-import java.lang.reflect.Type;
 
-import javax.webbeans.DefinitionException;
-import javax.webbeans.Dependent;
-import javax.webbeans.IllegalProductException;
-
 import org.jboss.webbeans.ManagerImpl;
 import org.jboss.webbeans.introspector.AnnotatedField;
 import org.jboss.webbeans.introspector.jlr.AnnotatedFieldImpl;
@@ -72,47 +67,12 @@
    @Override
    public T create()
    {
-      T instance = field.get(manager.getInstance(getDeclaringBean()));
-      if (instance == null && !getScopeType().equals(Dependent.class))
-      {
-         throw new IllegalProductException("Cannot return null from a non-dependent producer field");
-      }
+      T instance = field.get(getReceiver());
+      checkReturnValue(instance);
       return instance;
    }
 
    /**
-    * Initializes the bean and its metadata
-    */
-   @Override
-   protected void init()
-   {
-      super.init();
-      checkProducerField();
-   }
-   
-   
-   /**
-    * Validates the producer method
-    */
-   protected void checkProducerField()
-   {
-      if (getAnnotatedItem().isStatic())
-      {
-         throw new DefinitionException("Producer method cannot be static " + field);
-      }
-      else if (getAnnotatedItem().getActualTypeArguments().length > 0)
-      {
-         for (Type type : getAnnotatedItem().getActualTypeArguments())
-         {
-            if (!(type instanceof Class))
-            {
-               throw new DefinitionException("Producer field type cannot be parameterized with type parameter or wildcard");
-            }
-         }
-      }
-   }
-
-   /**
     * Gets the annotated item representing the method
     * 
     * @return The annotated item

Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/ProducerMethodBean.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/ProducerMethodBean.java	2008-12-07 20:21:17 UTC (rev 456)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/ProducerMethodBean.java	2008-12-07 20:38:40 UTC (rev 457)
@@ -19,14 +19,11 @@
 
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Method;
-import java.lang.reflect.Type;
 import java.util.Set;
 
 import javax.webbeans.DefinitionException;
-import javax.webbeans.Dependent;
 import javax.webbeans.Destructor;
 import javax.webbeans.Disposes;
-import javax.webbeans.IllegalProductException;
 import javax.webbeans.Observes;
 
 import org.jboss.webbeans.ManagerImpl;
@@ -78,11 +75,8 @@
    @Override
    public T create()
    {
-      T instance = method.invoke(manager, manager.getInstance(getDeclaringBean()));
-      if (instance == null && !getScopeType().equals(Dependent.class))
-      {
-         throw new IllegalProductException("Cannot return null from a non-dependent producer method");
-      }
+      T instance = method.invoke(manager, getReceiver());
+      checkReturnValue(instance);
       return instance;
    }
 
@@ -94,7 +88,7 @@
    {
       super.init();
       checkProducerMethod();
-      initRemoveMethod();
+      initDisposalMethod();
       initInjectionPoints();
    }
    
@@ -123,12 +117,8 @@
     */
    protected void checkProducerMethod()
    {
-      if (getAnnotatedItem().isStatic())
+      if (getAnnotatedItem().isAnnotationPresent(Destructor.class))
       {
-         throw new DefinitionException("Producer method cannot be static " + method);
-      }
-      else if (getAnnotatedItem().isAnnotationPresent(Destructor.class))
-      {
          throw new DefinitionException("Producer method cannot be annotated @Destructor");
       }
       else if (getAnnotatedItem().getAnnotatedParameters(Observes.class).size() > 0)
@@ -139,22 +129,12 @@
       {
          throw new DefinitionException("Producer method cannot have parameter annotated @Disposes");
       }
-      else if (getAnnotatedItem().getActualTypeArguments().length > 0)
-      {
-         for (Type type : getAnnotatedItem().getActualTypeArguments())
-         {
-            if (!(type instanceof Class))
-            {
-               throw new DefinitionException("Producer method return type cannot be parameterized with type parameter or wildcard");
-            }
-         }
-      }
    }
    
    /**
     * Initializes the remove method
     */
-   protected void initRemoveMethod()
+   protected void initDisposalMethod()
    {
       Set<AnnotatedMethod<Object>> disposalMethods = manager.resolveDisposalMethods(getType(), getBindingTypes().toArray(new Annotation[0]));
       if (disposalMethods.size() == 1)




More information about the weld-commits mailing list