openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject svn commit: r1817759 - in /openwebbeans/trunk/webbeans-impl/src: main/java/org/apache/webbeans/util/WebBeansUtil.java test/java/org/apache/webbeans/test/specalization/ProducerMethodSpecialisationTest.java
Date Mon, 11 Dec 2017 10:28:26 GMT
Author: struberg
Date: Mon Dec 11 10:28:26 2017
New Revision: 1817759

URL: http://svn.apache.org/viewvc?rev=1817759&view=rev
Log:
OWB-1215 refine check for ambiguous producer method specialisation

Added:
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/specalization/ProducerMethodSpecialisationTest.java
  (with props)
Modified:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java

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=1817759&r1=1817758&r2=1817759&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
Mon Dec 11 10:28:26 2017
@@ -651,7 +651,7 @@ public final class WebBeansUtil
         // collect all producer method beans
         Set<Bean<?>> beans = webBeansContext.getBeanManagerImpl().getBeans();
         List<ProducerMethodBean> producerBeans = new ArrayList<>();
-        Set<Class> classesDisabledDueToSpecialization = new HashSet<>();
+        Set<String> methodsDisabledDueToSpecialization = new HashSet<>();
 
         for(Bean b : beans)
         {
@@ -661,17 +661,25 @@ public final class WebBeansUtil
 
                 if (((ProducerMethodBean) b).isSpecializedBean())
                 {
-                    Class superClass = b.getBeanClass().getSuperclass();
-                    if (classesDisabledDueToSpecialization.contains(superClass))
+                    Method creatorMethod = ((ProducerMethodBean) b).getCreatorMethod();
+                    Method overloadedMethod = getParentClassMethod(creatorMethod.getDeclaringClass(),
creatorMethod.getName(), creatorMethod.getParameterTypes());
+                    if (overloadedMethod == null)
+                    {
+                        throw new WebBeansDeploymentException("Specialized producer method
doesn't override method from a parent class"
+                            + b.toString());
+                    }
+
+                    String superMethod = overloadedMethod.toString();
+                    if (methodsDisabledDueToSpecialization.contains(superMethod))
                     {
                         throw new WebBeansDeploymentException("Multiple specializations for
the same producer method got detected for type"
                                 + b.toString());
                     }
-                    classesDisabledDueToSpecialization.add(superClass);
+                    methodsDisabledDueToSpecialization.add(superMethod);
                 }
             }
         }
-        classesDisabledDueToSpecialization.clear(); // not needed any longer
+        methodsDisabledDueToSpecialization.clear(); // not needed any longer
 
         // create sorted bean helper.
         SortedListHelper<ProducerMethodBean> producerBeanListHelper = new
@@ -793,6 +801,27 @@ public final class WebBeansUtil
         }
     }
 
+    /**
+     * Get the overloaded method with the exact signature as the given method from the parent
class.
+     */
+    private Method getParentClassMethod(Class<?> clazz, String methodName, Class[]
paramTypes)
+    {
+        Class<?> superClass = clazz.getSuperclass();
+        if (superClass != null && !superClass.equals(Object.class))
+        {
+            try
+            {
+                return superClass.getDeclaredMethod(methodName, paramTypes);
+            }
+            catch (NoSuchMethodException e)
+            {
+                // recurse further to the superclass
+                return getParentClassMethod(superClass, methodName, paramTypes);
+            }
+        }
+
+        return null;
+    }
 
 
     public <T> Constructor<T> getNoArgConstructor(Class<T> clazz)

Added: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/specalization/ProducerMethodSpecialisationTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/specalization/ProducerMethodSpecialisationTest.java?rev=1817759&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/specalization/ProducerMethodSpecialisationTest.java
(added)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/specalization/ProducerMethodSpecialisationTest.java
Mon Dec 11 10:28:26 2017
@@ -0,0 +1,115 @@
+/*
+ * 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;
+
+import javax.enterprise.inject.Produces;
+import javax.enterprise.inject.Specializes;
+import javax.enterprise.util.AnnotationLiteral;
+import javax.inject.Qualifier;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import org.apache.webbeans.test.AbstractUnitTest;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class ProducerMethodSpecialisationTest extends AbstractUnitTest
+{
+    @Test
+    public void testProducerMethodSpecialisation()
+    {
+        startContainer(A.class, B.class);
+
+        assertEquals("B-one", getInstance(String.class, new AnnotationLiteral<OneProducesMethod>(){}));
+        assertEquals("B-two", getInstance(String.class, new AnnotationLiteral<TwoProducesMethod>(){}));
+
+        assertEquals("A-three", getInstance(String.class, new AnnotationLiteral<ThreeProducesMethod>(){}));
+    }
+
+
+    public static class A 
+    {
+        @Produces
+        @OneProducesMethod
+        public String getX() 
+        {
+            return "A-one";
+        }
+
+        @Produces
+        @TwoProducesMethod
+        public String getY()
+        {
+            return "A-two";
+        }
+
+        @Produces
+        @ThreeProducesMethod
+        public String getZ()
+        {
+            return "A-three";
+        }
+    }
+
+    public static class B extends A 
+    {
+        @Produces
+        @Specializes
+        @OneProducesMethod
+        public String getX()
+        {
+            return "B-one";
+        }
+
+        @Produces
+        @Specializes
+        @TwoProducesMethod
+        public String getY()
+        {
+            return "B-two";
+        }
+    }
+
+
+    @Retention(RetentionPolicy.RUNTIME)
+    @Target({ElementType.METHOD, ElementType.TYPE})
+    @Qualifier
+    public @interface OneProducesMethod 
+    {
+    }
+
+    @Retention(RetentionPolicy.RUNTIME)
+    @Target({ElementType.METHOD, ElementType.TYPE})
+    @Qualifier
+    public @interface TwoProducesMethod
+    {
+    }
+
+    @Retention(RetentionPolicy.RUNTIME)
+    @Target({ElementType.METHOD, ElementType.TYPE})
+    @Qualifier
+    public @interface ThreeProducesMethod
+    {
+    }
+
+}

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



Mime
View raw message