You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by st...@apache.org on 2014/09/06 00:00:45 UTC

svn commit: r1622810 - in /openwebbeans/trunk/webbeans-impl/src: main/java/org/apache/webbeans/component/ main/java/org/apache/webbeans/component/creation/ main/java/org/apache/webbeans/config/ main/java/org/apache/webbeans/util/ test/java/org/apache/w...

Author: struberg
Date: Fri Sep  5 22:00:45 2014
New Revision: 1622810

URL: http://svn.apache.org/r1622810
Log:
properly detect  multiple @Specialized producers and beans for the same base class

Added:
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/specalization/multiple/BeanB2.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/specalization/multiple/MultipleSpecializedProducerMethodsTest.java   (with props)
Modified:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/AbstractOwbBean.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/BeanAttributesBuilder.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/ProducerMethodBeanBuilder.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/specalization/multiple/MultipleSpecializationTest.java

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/AbstractOwbBean.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/AbstractOwbBean.java?rev=1622810&r1=1622809&r2=1622810&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/AbstractOwbBean.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/AbstractOwbBean.java Fri Sep  5 22:00:45 2014
@@ -334,8 +334,8 @@ public abstract class AbstractOwbBean<T>
     {
         StringBuilder builder = new StringBuilder();
         final String simpleName = getReturnType().getSimpleName();
-        builder.append(simpleName).append(", ");
-        builder.append("Name:").append(getName()).append(", WebBeans Type:").append(getWebBeansType());
+        builder.append(simpleName);
+        builder.append(", WebBeansType:").append(getWebBeansType()).append(", Name:").append(getName());
         builder.append(", API Types:[");
         
         int size = getTypes().size();

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/BeanAttributesBuilder.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/BeanAttributesBuilder.java?rev=1622810&r1=1622809&r2=1622810&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/BeanAttributesBuilder.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/BeanAttributesBuilder.java Fri Sep  5 22:00:45 2014
@@ -39,6 +39,7 @@ import javax.enterprise.inject.spi.Annot
 import javax.enterprise.inject.spi.AnnotatedMethod;
 import javax.enterprise.inject.spi.AnnotatedParameter;
 import javax.enterprise.inject.spi.AnnotatedType;
+import javax.enterprise.inject.spi.DeploymentException;
 import javax.enterprise.util.Nonbinding;
 import javax.inject.Named;
 import javax.inject.Scope;
@@ -581,7 +582,7 @@ public abstract class BeanAttributesBuil
                 // TODO XXX We have to check stereotypes here, too
                 if (getAnnotated().getJavaClass().isAnnotationPresent(Named.class))
                 {
-                    throw new DefinitionException("@Specialized Class : " + getAnnotated().getJavaClass().getName()
+                    throw new DeploymentException("@Specialized Class : " + getAnnotated().getJavaClass().getName()
                             + " may not explicitly declare a bean name");
                 }
             }

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/ProducerMethodBeanBuilder.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/ProducerMethodBeanBuilder.java?rev=1622810&r1=1622809&r2=1622810&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/ProducerMethodBeanBuilder.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/ProducerMethodBeanBuilder.java Fri Sep  5 22:00:45 2014
@@ -58,14 +58,14 @@ public class ProducerMethodBeanBuilder<T
 
         if (superMethod == null)
         {
-            throw new WebBeansConfigurationException("Anontated producer method specialization is failed : " + annotatedMethod.getJavaMember().getName()
+            throw new WebBeansConfigurationException("Annotated producer method specialization failed : " + annotatedMethod.getJavaMember().getName()
                                                      + " not found in super class : " + annotatedMethod.getDeclaringType().getJavaClass().getSuperclass().getName()
                                                      + " for annotated method : " + annotatedMethod);
         }
         
         if (!AnnotationUtil.hasAnnotation(superMethod.getAnnotations(), Produces.class))
         {
-            throw new WebBeansConfigurationException("Anontated producer method specialization is failed : " + annotatedMethod.getJavaMember().getName()
+            throw new WebBeansConfigurationException("Annotated producer method specialization failed : " + annotatedMethod.getJavaMember().getName()
                                                      + " found in super class : " + annotatedMethod.getDeclaringType().getJavaClass().getSuperclass().getName()
                                                      + " is not annotated with @Produces" + " for annotated method : " + annotatedMethod);
         }

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java?rev=1622810&r1=1622809&r2=1622810&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java Fri Sep  5 22:00:45 2014
@@ -278,10 +278,6 @@ public class BeansDeployer
         {
             throw new DefinitionException(e);
         }
-        catch (DefinitionException de)
-        {
-            throw new DeploymentException(de);
-        }
         catch (Exception e)
         {
             throw ExceptionUtil.throwAsRuntimeException(e);

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java?rev=1622810&r1=1622809&r2=1622810&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java Fri Sep  5 22:00:45 2014
@@ -99,6 +99,8 @@ import javax.enterprise.inject.spi.Bean;
 import javax.enterprise.inject.spi.BeanManager;
 import javax.enterprise.inject.spi.BeforeBeanDiscovery;
 import javax.enterprise.inject.spi.BeforeShutdown;
+import javax.enterprise.inject.spi.DefinitionException;
+import javax.enterprise.inject.spi.DeploymentException;
 import javax.enterprise.inject.spi.Extension;
 import javax.enterprise.inject.spi.InjectionPoint;
 import javax.enterprise.inject.spi.ObserverMethod;
@@ -685,6 +687,7 @@ public final class WebBeansUtil
 
     /**
      * Configure direct/indirect specialized producer method beans.
+     * @deprecated remove
      */
     public void configureProducerMethodSpecializations()
     {
@@ -696,13 +699,27 @@ public final class WebBeansUtil
         // collect all producer method beans
         Set<Bean<?>> beans = webBeansContext.getBeanManagerImpl().getBeans();
         List<ProducerMethodBean> producerBeans = new ArrayList<ProducerMethodBean>();
+        Set<Class> classesDisabledDueToSpecialization = new HashSet<Class>();
+
         for(Bean b : beans)
         {
             if (b instanceof ProducerMethodBean)
             {
                 producerBeans.add((ProducerMethodBean)b);
+
+                if (((ProducerMethodBean) b).isSpecializedBean())
+                {
+                    Class superClass = b.getBeanClass().getSuperclass();
+                    if (classesDisabledDueToSpecialization.contains(superClass))
+                    {
+                        throw new DeploymentException("Multiple specializations for the same producer method got detected for type"
+                                + b.toString());
+                    }
+                    classesDisabledDueToSpecialization.add(superClass);
+                }
             }
         }
+        classesDisabledDueToSpecialization.clear(); // not needed any longer
 
         // create sorted bean helper.
         SortedListHelper<ProducerMethodBean> producerBeanListHelper = new
@@ -724,13 +741,15 @@ public final class WebBeansUtil
                     }
                 });
 
+        Set<Method> disabledProducerMethods = new HashSet<Method>();
+
         while(true)
         {
             pbean = null;
             method = null;
             producerBeanListHelper.clear();
 
-            //locate a specialized bean
+            //locate the first specialized bean
             for(ProducerMethodBean pb : producerBeans)
             {
                 if (pb.isSpecializedBean())
@@ -767,6 +786,12 @@ public final class WebBeansUtil
                         //Added by GE, method check is necessary otherwise getting wrong method qualifier annotations
                         if (superMethod != null && superMethod.equals(pb.getCreatorMethod()))
                         {
+                            if (disabledProducerMethods.contains(superMethod))
+                            {
+                                throw new DefinitionException("Multiple specializations for the same producer method got detected for type"
+                                        + pbean.toString());
+                            }
+                            disabledProducerMethods.add(superMethod);
                             producerBeanListHelper.add(pb);
                             pLeft = (pb.isSpecializedBean()) ? pb : null;
                         }

Added: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/specalization/multiple/BeanB2.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/specalization/multiple/BeanB2.java?rev=1622810&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/specalization/multiple/BeanB2.java (added)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/specalization/multiple/BeanB2.java Fri Sep  5 22:00:45 2014
@@ -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.specalization.multiple;
+
+import javax.enterprise.inject.Specializes;
+
+/**
+ * invalid as there must not be multiple classes which specialize the same bean.
+ */
+@Specializes
+public class BeanB2 extends BeanA
+{
+    @Override
+    public Class getBeanClass()
+    {
+        return BeanB2.class;
+    }
+}

Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/specalization/multiple/MultipleSpecializationTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/specalization/multiple/MultipleSpecializationTest.java?rev=1622810&r1=1622809&r2=1622810&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/specalization/multiple/MultipleSpecializationTest.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/specalization/multiple/MultipleSpecializationTest.java Fri Sep  5 22:00:45 2014
@@ -23,7 +23,9 @@ import java.util.Collection;
 import junit.framework.Assert;
 import org.apache.webbeans.exception.WebBeansConfigurationException;
 import javax.enterprise.inject.spi.DefinitionException;
+import javax.enterprise.inject.spi.DeploymentException;
 
+import org.apache.webbeans.exception.WebBeansDeploymentException;
 import org.apache.webbeans.test.AbstractUnitTest;
 import org.junit.Test;
 
@@ -71,6 +73,7 @@ public class MultipleSpecializationTest 
             beanClasses.add(BeanD.class);
 
             startContainer(beanClasses, null);
+            Assert.fail();
         }
         catch (Exception e)
         {
@@ -79,8 +82,24 @@ public class MultipleSpecializationTest 
 
         Assert.assertNotNull(occuredException);
         Assert.assertEquals(WebBeansConfigurationException.class.getName(), occuredException.getClass().getName());
-        Assert.assertEquals(DefinitionException.class.getName(), occuredException.getCause().getClass().getName());
+        Assert.assertEquals(DeploymentException.class.getName(), occuredException.getCause().getClass().getName());
         
         shutDownContainer();
     }
+
+    @Test
+    public void testFailMultipleSpecializationOfSameType()
+    {
+        try
+        {
+            startContainer(BeanA.class, BeanB.class, BeanB2.class);
+            Assert.fail();
+        }
+        catch (Exception e)
+        {
+            Assert.assertTrue(e instanceof WebBeansConfigurationException);
+            Assert.assertTrue(e.getCause() instanceof WebBeansDeploymentException);
+            Assert.assertTrue(e.getCause().getMessage().contains("More than one class specialized"));
+        }
+    }
 }

Added: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/specalization/multiple/MultipleSpecializedProducerMethodsTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/specalization/multiple/MultipleSpecializedProducerMethodsTest.java?rev=1622810&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/specalization/multiple/MultipleSpecializedProducerMethodsTest.java (added)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/specalization/multiple/MultipleSpecializedProducerMethodsTest.java Fri Sep  5 22:00:45 2014
@@ -0,0 +1,80 @@
+/*
+ * 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.specalization.multiple;
+
+import javax.enterprise.inject.Produces;
+import javax.enterprise.inject.Specializes;
+import javax.enterprise.inject.spi.DeploymentException;
+
+import junit.framework.Assert;
+import org.apache.webbeans.exception.WebBeansConfigurationException;
+import org.apache.webbeans.test.AbstractUnitTest;
+import org.junit.Test;
+
+
+/**
+ * Test that a DeploymentException happens if there are multiple
+ * specializations for the same producer method.
+ */
+public class MultipleSpecializedProducerMethodsTest extends AbstractUnitTest
+{
+
+    @Test
+    public void testFailMultipleSpecializedProducerMethods()
+    {
+        try
+        {
+            startContainer(BaseProducerMethod.class, SpecializedProducer1.class, SpecializedProducer2.class);
+            Assert.fail("OWB doesn't properly detect conflicting producer method specialization");
+        }
+        catch (Exception e)
+        {
+            Assert.assertTrue(e instanceof WebBeansConfigurationException);
+            Assert.assertTrue(e.getCause() instanceof DeploymentException);
+            Assert.assertTrue(e.getCause().getMessage().contains("Multiple specializations"));
+        }
+    }
+
+    public static class BaseProducerMethod
+    {
+        @Produces
+        public String producerMethod()
+        {
+            return "BASE";
+        }
+    }
+
+    public static class SpecializedProducer1 extends BaseProducerMethod
+    {
+        @Produces
+        @Specializes
+        public String producerMethod()
+        {
+            return "SPEZ1";
+        }
+    }
+
+    public static class SpecializedProducer2 extends BaseProducerMethod
+    {
+        @Produces
+        @Specializes
+        public String producerMethod()
+        {
+            return "SPEZ2";
+        }
+    }
+}

Propchange: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/specalization/multiple/MultipleSpecializedProducerMethodsTest.java
------------------------------------------------------------------------------
    svn:eol-style = native