openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rsandt...@apache.org
Subject svn commit: r1640191 - in /openwebbeans/trunk/webbeans-impl/src: main/java/org/apache/webbeans/component/creation/DecoratorBeanBuilder.java test/java/org/apache/webbeans/test/decorators/tests/ExtendedMulitpleGenericDecoratorTest.java
Date Mon, 17 Nov 2014 17:10:25 GMT
Author: rsandtner
Date: Mon Nov 17 17:10:24 2014
New Revision: 1640191

URL: http://svn.apache.org/r1640191
Log:
OWB-1024 only check the decorated interface

removed check for the complete hierachry of the decorator

Added:
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/decorators/tests/ExtendedMulitpleGenericDecoratorTest.java
Modified:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/DecoratorBeanBuilder.java

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/DecoratorBeanBuilder.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/DecoratorBeanBuilder.java?rev=1640191&r1=1640190&r2=1640191&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/DecoratorBeanBuilder.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/DecoratorBeanBuilder.java
Mon Nov 17 17:10:24 2014
@@ -217,11 +217,11 @@ public class DecoratorBeanBuilder<T> ext
             {
                 if(ClassUtil.isParametrizedType(decType) && ClassUtil.isParametrizedType(delegateType))
                 {
-                    checkParametrizedType((ParameterizedType) decType, (ParameterizedType)
delegateType);
+                    checkParametrizedType();
                 }
                 else if (ClassUtil.isTypeVariable(decType))
                 {
-                    if (!delegateType.equals(delegateType))
+                    if (!decType.equals(delegateType))
                     {
                         throw new WebBeansConfigurationException("Decorator : " + toString()
+ " generic delegate attribute must be same with decorated type : " + decType);
                     }
@@ -234,37 +234,34 @@ public class DecoratorBeanBuilder<T> ext
     /**
      * Checks recursive, if the ParameterizedTypes are equal
      *
-     * @param decoratedType ParameterizedType of the decoreatedType
-     * @param delegateType ParameterizedType of the delegateType
-     *
-     * @throws WebBeansConfigurationException
+     * @throws DefinitionException
      */
-    private void checkParametrizedType(ParameterizedType decoratedType, ParameterizedType
delegateType)
+    private void checkParametrizedType()
     {
-        //X TODO maybe we could move this to GenericsUtil
-
-        Type[] decTypeArguments = ClassUtil.getActualTypeArguments(decoratedType);
-        Type[] delegateTypeArguments = ClassUtil.getActualTypeArguments(delegateType);
-
-        if (decTypeArguments.length != delegateTypeArguments.length)
-        {
-            throw new WebBeansConfigurationException("Decorator: " + toString() + " " +
-                    "Number of TypeArguments must match - Decorated Type:  " + decTypeArguments.length
+
-                    " Delegate Type: " + delegateTypeArguments.length);
-        }
+        Type[] delegeteTypes = ((ParameterizedType) delegateType).getActualTypeArguments();
+        Type[] interfaceTypes = annotatedType.getJavaClass().getGenericInterfaces();
 
-        for (int i = 0; i < decTypeArguments.length; i++)
+        for (Type interfaceType : interfaceTypes)
         {
-            Type decTypeArg = decTypeArguments[i];
-            Type delegateTypeArg = delegateTypeArguments[i];
+            if (!ClassUtil.isClassAssignable(ClassUtil.getClass(delegateType), ClassUtil.getClass(interfaceType)))
+            {
+                // only check the interface from the decorated type
+                continue;
+            }
 
-            if (ClassUtil.isParametrizedType(decTypeArg) && ClassUtil.isParametrizedType(delegateTypeArg))
+            Type[] arguments = ClassUtil.getActualTypeArguments(interfaceType);
+            if (arguments.length != delegeteTypes.length)
             {
-                checkParametrizedType((ParameterizedType) decTypeArg, (ParameterizedType)
delegateTypeArg);
+                throw new DefinitionException("Decorator: " + toString() + " Number of TypeArguments
must match - Decorated Type:  " + arguments.length +
+                                              " Delegate Type: " + delegeteTypes.length);
             }
-            else if (!decTypeArg.equals(delegateTypeArg))
+
+            for (int i = 0; i < delegeteTypes.length; i++)
             {
-                throw new DefinitionException("Decorator: " + toString() + " delegate attribute
must match decorated type: " + decTypeArg);
+                if (!delegeteTypes[i].equals(arguments[i]))
+                {
+                    throw new DefinitionException("Decorator: " + toString() + " delegate
attribute must match decorated type: " + delegeteTypes[i]);
+                }
             }
         }
     }

Added: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/decorators/tests/ExtendedMulitpleGenericDecoratorTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/decorators/tests/ExtendedMulitpleGenericDecoratorTest.java?rev=1640191&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/decorators/tests/ExtendedMulitpleGenericDecoratorTest.java
(added)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/decorators/tests/ExtendedMulitpleGenericDecoratorTest.java
Mon Nov 17 17:10:24 2014
@@ -0,0 +1,169 @@
+/*
+ * 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.decorators.tests;
+
+import junit.framework.Assert;
+import org.apache.webbeans.test.AbstractUnitTest;
+import org.junit.Test;
+
+import javax.annotation.Priority;
+import javax.decorator.Decorator;
+import javax.decorator.Delegate;
+import javax.enterprise.inject.Any;
+import javax.inject.Inject;
+import java.io.Serializable;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+ * Test to reproduce OWB-1024
+ */
+public class ExtendedMulitpleGenericDecoratorTest extends AbstractUnitTest
+{
+
+    @Test
+    public void testMultipleTypeParameters() throws Exception
+    {
+        startContainer(Interface.class,
+                       ExtendingInterface.class,
+                       Service.class,
+                       ExtendingInterfaceDecorator.class,
+                       SwapedExtendingInterfaceDecorator.class,
+                       SwapedService.class,
+                       SwapedParameterExtendingInterface.class);
+
+        Service service = getInstance(Service.class);
+        SwapedService swapedService = getInstance(SwapedService.class);
+
+        Assert.assertNotNull(service);
+        Assert.assertNotNull(swapedService);
+
+        Number result = service.doSomethingOthers("aString");
+
+        Assert.assertNotNull(result);
+        Assert.assertEquals(2, result.intValue());
+
+        Assert.assertTrue(ExtendingInterfaceDecorator.called);
+        Assert.assertEquals(1, ExtendingInterfaceDecorator.callCount.get());
+
+        String swapedResult = swapedService.doSomethingWithWrappedTypeParams(5L);
+
+        Assert.assertNotNull(swapedResult);
+        Assert.assertEquals("doSomethingWithWrappedTypeParams: your number is 5", swapedResult);
+
+        Assert.assertTrue(SwapedExtendingInterfaceDecorator.called);
+        Assert.assertEquals(1, SwapedExtendingInterfaceDecorator.callCount.get());
+
+
+        // shutdown is called @After in AbstractUnitTest
+    }
+
+
+
+    public static interface Interface<P, T> {
+        T doSomething(P param);
+    }
+
+    public static interface ExtendingInterface<P extends Serializable> extends Interface<P,
Number> {
+        Number doSomethingOthers(P param);
+    }
+
+    public static interface SwapedParameterExtendingInterface<T extends Serializable>
extends Interface<Number, T>
+    {
+        T doSomethingWithWrappedTypeParams(Number param);
+    }
+
+    public static class Service implements ExtendingInterface<String>
+    {
+
+        @Override
+        public Number doSomethingOthers(String param)
+        {
+            return param.length() % 5;
+        }
+
+        @Override
+        public Number doSomething(String param)
+        {
+            return (param.length() / 2) * 6;
+        }
+    }
+
+    public static class SwapedService implements SwapedParameterExtendingInterface<String>
{
+
+        @Override
+        public String doSomethingWithWrappedTypeParams(Number param)
+        {
+            return "doSomethingWithWrappedTypeParams: your number is " + param.longValue();
+        }
+
+        @Override
+        public String doSomething(Number param)
+        {
+            return "doSomething: parameter = " + param;
+        }
+    }
+
+    @Decorator
+    @Priority(value = 1)
+    public abstract static class ExtendingInterfaceDecorator<T extends Serializable>
implements ExtendingInterface<T>
+    {
+        public static boolean called;
+        public static AtomicInteger callCount = new AtomicInteger(0);
+
+
+        @Inject
+        @Any
+        @Delegate
+        private ExtendingInterface<T> delegate;
+
+
+        @Override
+        public Number doSomethingOthers(T param)
+        {
+            called = true;
+            callCount.incrementAndGet();
+
+            return delegate.doSomethingOthers(param);
+        }
+    }
+
+    @Decorator
+    @Priority(value = 1)
+    public abstract static class SwapedExtendingInterfaceDecorator<T extends Serializable>
implements SwapedParameterExtendingInterface<T>
+    {
+        public static boolean called;
+        public static AtomicInteger callCount = new AtomicInteger(0);
+
+
+        @Inject
+        @Any
+        @Delegate
+        private SwapedParameterExtendingInterface<T> delegete;
+
+
+        @Override
+        public T doSomethingWithWrappedTypeParams(Number param)
+        {
+            called = true;
+            callCount.incrementAndGet();
+
+            return delegete.doSomethingWithWrappedTypeParams(param);
+        }
+    }
+}



Mime
View raw message