openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rsandt...@apache.org
Subject svn commit: r1640195 - in /openwebbeans/branches/owb_1.2.x/webbeans-impl/src: main/java/org/apache/webbeans/component/creation/DecoratorBeanBuilder.java test/java/org/apache/webbeans/newtests/decorators/tests/ExtendedMulitpleGenericDecoratorTest.java
Date Mon, 17 Nov 2014 17:31:42 GMT
Author: rsandtner
Date: Mon Nov 17 17:31:42 2014
New Revision: 1640195

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

Added:
    openwebbeans/branches/owb_1.2.x/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/tests/ExtendedMulitpleGenericDecoratorTest.java
Modified:
    openwebbeans/branches/owb_1.2.x/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/DecoratorBeanBuilder.java

Modified: openwebbeans/branches/owb_1.2.x/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/DecoratorBeanBuilder.java
URL: http://svn.apache.org/viewvc/openwebbeans/branches/owb_1.2.x/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/DecoratorBeanBuilder.java?rev=1640195&r1=1640194&r2=1640195&view=diff
==============================================================================
--- openwebbeans/branches/owb_1.2.x/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/DecoratorBeanBuilder.java
(original)
+++ openwebbeans/branches/owb_1.2.x/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/DecoratorBeanBuilder.java
Mon Nov 17 17:31:42 2014
@@ -50,6 +50,7 @@ import org.apache.webbeans.component.Web
 import org.apache.webbeans.config.OWBLogConst;
 import org.apache.webbeans.config.WebBeansContext;
 import org.apache.webbeans.exception.WebBeansConfigurationException;
+import org.apache.webbeans.exception.inject.DefinitionException;
 import org.apache.webbeans.logger.WebBeansLoggerFacade;
 import org.apache.webbeans.util.Asserts;
 import org.apache.webbeans.util.ClassUtil;
@@ -231,7 +232,7 @@ public class DecoratorBeanBuilder<T>
             {
                 if(ClassUtil.isParametrizedType(decType) && ClassUtil.isParametrizedType(delegateType))
                 {
-                    checkParametrizedType((ParameterizedType) decType, (ParameterizedType)
delegateType);
+                    checkParametrizedType();
                 }
                 else if (ClassUtil.isTypeVariable(decType))
                 {
@@ -248,37 +249,34 @@ public class DecoratorBeanBuilder<T>
     /**
      * 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 WebBeansConfigurationException("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/branches/owb_1.2.x/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/tests/ExtendedMulitpleGenericDecoratorTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/branches/owb_1.2.x/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/tests/ExtendedMulitpleGenericDecoratorTest.java?rev=1640195&view=auto
==============================================================================
--- openwebbeans/branches/owb_1.2.x/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/tests/ExtendedMulitpleGenericDecoratorTest.java
(added)
+++ openwebbeans/branches/owb_1.2.x/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/tests/ExtendedMulitpleGenericDecoratorTest.java
Mon Nov 17 17:31:42 2014
@@ -0,0 +1,168 @@
+/*
+ * 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.newtests.decorators.tests;
+
+import junit.framework.Assert;
+import org.apache.webbeans.newtests.AbstractUnitTest;
+import org.junit.Test;
+
+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
+    {
+        addDecorator(ExtendingInterfaceDecorator.class);
+        addDecorator(SwapedExtendingInterfaceDecorator.class);
+        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
+    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
+    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