You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by ge...@apache.org on 2009/10/16 19:02:36 UTC

svn commit: r826004 - in /incubator/openwebbeans/trunk/webbeans-impl/src: main/java/org/apache/webbeans/config/ test/java/org/apache/webbeans/test/component/definition/ test/java/org/apache/webbeans/test/unittests/definition/

Author: gerdogdu
Date: Fri Oct 16 17:02:35 2009
New Revision: 826004

URL: http://svn.apache.org/viewvc?rev=826004&view=rev
Log:
Adding support for @BeanTypes. BeanTypes are used for adding user defined api types.

Added:
    incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/component/definition/
    incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/component/definition/BeanTypesDefinedBean.java   (with props)
    incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/unittests/definition/
    incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/unittests/definition/BeanTypesTest.java   (with props)
Modified:
    incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/DefinitionUtil.java

Modified: incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/DefinitionUtil.java
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/DefinitionUtil.java?rev=826004&r1=826003&r2=826004&view=diff
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/DefinitionUtil.java (original)
+++ incubator/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/DefinitionUtil.java Fri Oct 16 17:02:35 2009
@@ -30,6 +30,7 @@
 import javax.enterprise.context.Dependent;
 import javax.enterprise.context.NormalScope;
 import javax.enterprise.event.Observes;
+import javax.enterprise.inject.BeanTypes;
 import javax.enterprise.inject.Disposes;
 import javax.enterprise.inject.NonBinding;
 import javax.enterprise.inject.Produces;
@@ -49,6 +50,7 @@
 import org.apache.webbeans.annotation.deployment.Standard;
 import org.apache.webbeans.component.AbstractBean;
 import org.apache.webbeans.component.AbstractInjectionTargetBean;
+import org.apache.webbeans.component.AbstractProducerBean;
 import org.apache.webbeans.component.BaseBean;
 import org.apache.webbeans.component.IBeanHasParent;
 import org.apache.webbeans.component.InjectionTargetBean;
@@ -159,37 +161,80 @@
      * Configures the web bean api types.
      * 
      * @param <T> generic class type
-     * @param component configuring web beans component
+     * @param bean configuring web beans component
      * @param clazz bean implementation class
      */
-    public static <T> void defineApiTypes(AbstractBean<T> component, Class<T> clazz)
+    public static <T> void defineApiTypes(AbstractBean<T> bean, Class<T> clazz)
+    {
+        Annotation[] annots = clazz.getDeclaredAnnotations();
+        
+        //Looking for bean types
+        if(AnnotationUtil.hasAnnotation(annots, BeanTypes.class))
+        {
+            BeanTypes beanTypes = (BeanTypes) AnnotationUtil.getAnnotation(annots, BeanTypes.class);
+            defineUserDefinedBeanTypes(bean, beanTypes);
+        }
+        else
+        {
+            ClassUtil.setTypeHierarchy(bean.getTypes(), clazz);   
+        }        
+    }
+     
+    
+    private static <T> void defineUserDefinedBeanTypes(AbstractBean<T> bean, BeanTypes beanTypes)
     {
-        ClassUtil.setTypeHierarchy(component.getTypes(), clazz);
+        Class<?> beanClazz = bean.getReturnType();        
+        Class<?>[] types = beanTypes.value();
+        
+        Set<Type> apiTypes = bean.getTypes();
+        for(Class<?> type : types)
+        {
+            if(!type.isAssignableFrom(beanClazz))
+            {
+                throw new WebBeansConfigurationException("@BeanType values must be assignable. Bean type : " + beanClazz.getName() + " @BeanType value is : " + type.getName());
+            }
+            
+            apiTypes.add(type);
+        }
+        
     }
+    
+    
 
     /**
      * Configures the producer method web bean api types.
      * 
      * @param <T> generic class type
-     * @param component configuring web beans component
+     * @param producerBean configuring web beans component
      * @param type bean implementation class
      */
-    public static <T> void defineProducerMethodApiTypes(AbstractBean<T> component, Type type)
+    public static <T> void defineProducerMethodApiTypes(AbstractProducerBean<T> producerBean, Type type, Annotation[] annots)
     {
-        Set<Type> types = component.getTypes();
-        types.add(Object.class);
-        
-        Class<?> clazz  = ClassUtil.getClazz(type);
         
-        if (clazz.isPrimitive() || clazz.isArray())
+        //Looking for bean types
+        if(AnnotationUtil.hasAnnotation(annots, BeanTypes.class))
         {
-            types.add(clazz);
-
+            BeanTypes beanTypes = (BeanTypes) AnnotationUtil.getAnnotation(annots, BeanTypes.class);
+            defineUserDefinedBeanTypes(producerBean, beanTypes);
         }
+        
         else
         {
-            ClassUtil.setTypeHierarchy(component.getTypes(), type);
-        }
+            Set<Type> types = producerBean.getTypes();
+            types.add(Object.class);
+            
+            Class<?> clazz  = ClassUtil.getClazz(type);
+            
+            if (clazz.isPrimitive() || clazz.isArray())
+            {
+                types.add(clazz);
+
+            }
+            else
+            {
+                ClassUtil.setTypeHierarchy(producerBean.getTypes(), type);
+            }            
+        }        
     }
 
     /**
@@ -636,7 +681,7 @@
 
         Annotation[] methodAnns = method.getDeclaredAnnotations();
 
-        DefinitionUtil.defineProducerMethodApiTypes(component, method.getGenericReturnType());
+        DefinitionUtil.defineProducerMethodApiTypes(component, method.getGenericReturnType(), methodAnns);
         DefinitionUtil.defineScopeType(component, methodAnns, "WebBeans producer method : " + method.getName() + " in class " + parent.getReturnType().getName() + " must declare default @Scope annotation");
         WebBeansUtil.checkProducerGenericType(component,method);        
         DefinitionUtil.defineQualifiers(component, methodAnns);
@@ -698,7 +743,7 @@
         
         Annotation[] fieldAnns = field.getDeclaredAnnotations();
 
-        DefinitionUtil.defineProducerMethodApiTypes(component, returnType);
+        DefinitionUtil.defineProducerMethodApiTypes(component, returnType, fieldAnns);
         DefinitionUtil.defineScopeType(component, fieldAnns, "WebBeans producer method : " + field.getName() + " in class " + parent.getReturnType().getName() + " must declare default @Scope annotation");
         WebBeansUtil.checkProducerGenericType(component,field);
         DefinitionUtil.defineQualifiers(component, fieldAnns);

Added: incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/component/definition/BeanTypesDefinedBean.java
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/component/definition/BeanTypesDefinedBean.java?rev=826004&view=auto
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/component/definition/BeanTypesDefinedBean.java (added)
+++ incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/component/definition/BeanTypesDefinedBean.java Fri Oct 16 17:02:35 2009
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You 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.apache.webbeans.test.component.definition;
+
+import java.io.Serializable;
+
+import javax.enterprise.inject.BeanTypes;
+import javax.enterprise.inject.Produces;
+import javax.inject.Named;
+
+import org.apache.webbeans.test.component.CheckWithCheckPayment;
+
+@BeanTypes(value={BeanTypesDefinedBean.class})
+public class BeanTypesDefinedBean implements Serializable
+{
+    private @Produces @Named("paymentField") @BeanTypes(value={CheckWithCheckPayment.class}) CheckWithCheckPayment payment;
+    
+    @Produces @Named("paymentMethod") @BeanTypes(value={CheckWithCheckPayment.class})
+    public CheckWithCheckPayment produce()
+    {
+        return null;
+    }
+}

Propchange: incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/component/definition/BeanTypesDefinedBean.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/unittests/definition/BeanTypesTest.java
URL: http://svn.apache.org/viewvc/incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/unittests/definition/BeanTypesTest.java?rev=826004&view=auto
==============================================================================
--- incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/unittests/definition/BeanTypesTest.java (added)
+++ incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/unittests/definition/BeanTypesTest.java Fri Oct 16 17:02:35 2009
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You 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.apache.webbeans.test.unittests.definition;
+
+import java.lang.reflect.Type;
+import java.util.Set;
+
+import javax.enterprise.inject.spi.Bean;
+
+import junit.framework.Assert;
+
+import org.apache.webbeans.test.component.CheckWithCheckPayment;
+import org.apache.webbeans.test.component.definition.BeanTypesDefinedBean;
+import org.apache.webbeans.test.servlet.TestContext;
+import org.junit.Before;
+import org.junit.Test;
+
+public class BeanTypesTest extends TestContext
+{
+    public BeanTypesTest()
+    {
+        super(BeanTypesTest.class.getName());
+    }
+    
+    @Before
+    public void init()
+    {
+        super.init();
+    }
+
+    @Test
+    public void testBeanTypes()
+    {
+        clear();
+        
+        Bean<BeanTypesDefinedBean> bean = defineSimpleWebBean(BeanTypesDefinedBean.class);
+        Set<Type> apiTypes = bean.getTypes();
+        
+        Assert.assertEquals(1, apiTypes.size());        
+        Assert.assertTrue(apiTypes.iterator().next().equals(BeanTypesDefinedBean.class));
+        
+        Set<Bean<?>> beans = getManager().getBeans("paymentField");
+        Assert.assertEquals(1, beans.size());
+        
+        Bean<?> pbean = beans.iterator().next();
+        apiTypes = pbean.getTypes();
+        
+        Assert.assertEquals(1, apiTypes.size());        
+        Assert.assertTrue(apiTypes.iterator().next().equals(CheckWithCheckPayment.class));
+        
+        beans = getManager().getBeans("paymentMethod");
+        Assert.assertEquals(1, beans.size());
+        
+        pbean = beans.iterator().next();
+        apiTypes = pbean.getTypes();
+        
+        Assert.assertEquals(1, apiTypes.size());        
+        Assert.assertTrue(apiTypes.iterator().next().equals(CheckWithCheckPayment.class));
+        
+        
+        
+    }
+}

Propchange: incubator/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/unittests/definition/BeanTypesTest.java
------------------------------------------------------------------------------
    svn:eol-style = native