You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by rs...@apache.org on 2014/10/07 15:00:59 UTC

svn commit: r1629883 - in /openwebbeans/branches/owb_1.2.x/webbeans-impl/src: main/java/org/apache/webbeans/component/creation/ main/java/org/apache/webbeans/intercept/ test/java/org/apache/webbeans/newtests/decorators/tests/

Author: rsandtner
Date: Tue Oct  7 13:00:58 2014
New Revision: 1629883

URL: http://svn.apache.org/r1629883
Log:
OWB-992 added checks for parametrizedTypes

Added:
    openwebbeans/branches/owb_1.2.x/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/tests/ExtendedGenericDecoratorTest.java
    openwebbeans/branches/owb_1.2.x/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/tests/RepeatedGenericTypeDecoratorTest.java
Modified:
    openwebbeans/branches/owb_1.2.x/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/DecoratorBeanBuilder.java
    openwebbeans/branches/owb_1.2.x/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorResolutionService.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=1629883&r1=1629882&r2=1629883&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 Tue Oct  7 13:00:58 2014
@@ -34,6 +34,7 @@ import java.io.Serializable;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.AnnotatedElement;
 import java.lang.reflect.Constructor;
+import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.Type;
 import java.util.ArrayList;
 import java.util.HashSet;
@@ -230,11 +231,54 @@ public class DecoratorBeanBuilder<T>
             {
                 if(ClassUtil.isParametrizedType(decType) && ClassUtil.isParametrizedType(delegateType))
                 {
-                    if(!delegateType.equals(decType))
+                    checkParametrizedType((ParameterizedType) decType, (ParameterizedType) delegateType);
+                }
+                else if (ClassUtil.isTypeVariable(decType))
+                {
+                    if (!delegateType.equals(delegateType))
                     {
                         throw new WebBeansConfigurationException("Decorator : " + toString() + " generic delegate attribute must be same with decorated type : " + decType);
                     }
                 }
+
+            }
+        }
+    }
+
+    /**
+     * Checks recursive, if the ParameterizedTypes are equal
+     *
+     * @param decoratedType ParameterizedType of the decoreatedType
+     * @param delegateType ParameterizedType of the delegateType
+     *
+     * @throws WebBeansConfigurationException
+     */
+    private void checkParametrizedType(ParameterizedType decoratedType, ParameterizedType delegateType)
+    {
+        //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);
+        }
+
+        for (int i = 0; i < decTypeArguments.length; i++)
+        {
+            Type decTypeArg = decTypeArguments[i];
+            Type delegateTypeArg = delegateTypeArguments[i];
+
+            if (ClassUtil.isParametrizedType(decTypeArg) && ClassUtil.isParametrizedType(delegateTypeArg))
+            {
+                checkParametrizedType((ParameterizedType) decTypeArg, (ParameterizedType) delegateTypeArg);
+            }
+            else if (!decTypeArg.equals(delegateTypeArg))
+            {
+                throw new WebBeansConfigurationException("Decorator: " + toString() + " delegate attribute must match decorated type: " + decTypeArg);
             }
         }
     }

Modified: openwebbeans/branches/owb_1.2.x/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorResolutionService.java
URL: http://svn.apache.org/viewvc/openwebbeans/branches/owb_1.2.x/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorResolutionService.java?rev=1629883&r1=1629882&r2=1629883&view=diff
==============================================================================
--- openwebbeans/branches/owb_1.2.x/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorResolutionService.java (original)
+++ openwebbeans/branches/owb_1.2.x/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorResolutionService.java Tue Oct  7 13:00:58 2014
@@ -446,7 +446,7 @@ public class InterceptorResolutionServic
                 boolean paramsMatch = true;
                 for (int i = 0; i < method1Params.length; i++)
                 {
-                    if (!method1Params[i].equals(method2Params[i]))
+                    if (!method1Params[i].isAssignableFrom(method2Params[i]))
                     {
                         paramsMatch = false;
                         break;

Added: openwebbeans/branches/owb_1.2.x/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/tests/ExtendedGenericDecoratorTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/branches/owb_1.2.x/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/tests/ExtendedGenericDecoratorTest.java?rev=1629883&view=auto
==============================================================================
--- openwebbeans/branches/owb_1.2.x/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/tests/ExtendedGenericDecoratorTest.java (added)
+++ openwebbeans/branches/owb_1.2.x/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/tests/ExtendedGenericDecoratorTest.java Tue Oct  7 13:00:58 2014
@@ -0,0 +1,174 @@
+/*
+ * 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 javax.decorator.Decorator;
+import javax.decorator.Delegate;
+import javax.enterprise.inject.Any;
+import javax.enterprise.inject.spi.Bean;
+import javax.inject.Inject;
+
+import junit.framework.Assert;
+import org.apache.webbeans.annotation.Decorated;
+import org.apache.webbeans.newtests.AbstractUnitTest;
+import org.junit.Test;
+
+/**
+ * Test to reproduce OWB-992
+ */
+public class ExtendedGenericDecoratorTest extends AbstractUnitTest
+{
+    @Test
+    public void testExtendedGenericDecorator() throws Exception
+    {
+        addDecorator(ExtendedInterfaceDecorator.class);
+
+        startContainer(Interface.class, ExtendedInterface.class, LongService.class, IntegerService.class, StringService.class);
+
+        Assert.assertFalse(LongService.testMethodCalled);
+        Assert.assertFalse(LongService.anotherMethodCalled);
+        Assert.assertFalse(IntegerService.testMethodCalled);
+        Assert.assertFalse(IntegerService.anotherMethodCalled);
+        Assert.assertFalse(ExtendedInterfaceDecorator.anotherMethodDecoratedWithLong);
+        Assert.assertFalse(ExtendedInterfaceDecorator.anotherMethodDecoratedWithInteger);
+        Assert.assertFalse(StringService.testMethodCalled);
+
+        // LongService
+        ExtendedInterface<Long> service = getInstance(LongService.class);
+        service.anotherMethod(2L); // call method from interface - shall be decorated
+        service.test(1L); // call method from super interface - shall not be decorated
+
+        // StringService shall not be decorated
+        StringService stringService = getInstance(StringService.class);
+        stringService.test("test");
+
+        // IntegerService
+        IntegerService integerService = getInstance(IntegerService.class);
+        integerService.anotherMethod(3); // call method from interface - shall be decorated
+        integerService.test(4); // call method from super interface - shall not be decorated
+
+
+        Assert.assertTrue(LongService.anotherMethodCalled);
+        Assert.assertTrue(LongService.testMethodCalled);
+        Assert.assertTrue(ExtendedInterfaceDecorator.anotherMethodDecoratedWithLong);
+        Assert.assertTrue(IntegerService.anotherMethodCalled);
+        Assert.assertTrue(IntegerService.testMethodCalled);
+        Assert.assertTrue(ExtendedInterfaceDecorator.anotherMethodDecoratedWithInteger);
+        Assert.assertTrue(StringService.testMethodCalled);
+
+
+        shutDownContainer();
+    }
+
+
+    public static interface Interface<T> {
+        void test(T input);
+    }
+
+    public static interface ExtendedInterface<T extends Number> extends Interface<T> {
+        void anotherMethod(T input);
+    }
+
+    public static class LongService implements ExtendedInterface<Long> {
+
+        public static boolean anotherMethodCalled = false;
+        public static boolean testMethodCalled = false;
+
+        @Override
+        public void anotherMethod(Long input)
+        {
+            anotherMethodCalled = true;
+        }
+
+        @Override
+        public void test(Long input)
+        {
+            testMethodCalled = true;
+        }
+    }
+
+    public static class IntegerService implements ExtendedInterface<Integer>
+    {
+
+        public static boolean anotherMethodCalled = false;
+        public static boolean testMethodCalled = false;
+
+        @Override
+        public void anotherMethod(Integer input)
+        {
+            anotherMethodCalled = true;
+        }
+
+        @Override
+        public void test(Integer input)
+        {
+            testMethodCalled = true;
+        }
+    }
+
+    public static class StringService implements Interface<String> {
+
+        public static boolean testMethodCalled = false;
+
+        @Override
+        public void test(String input)
+        {
+            testMethodCalled = true;
+        }
+    }
+
+    @Decorator
+    public abstract static class ExtendedInterfaceDecorator<T extends Number> implements ExtendedInterface<T> {
+
+        public static boolean anotherMethodDecoratedWithLong = false;
+        public static boolean anotherMethodDecoratedWithInteger = false;
+
+        public static boolean unexpectedDecorated = false;
+
+        @Delegate
+        @Inject
+        @Any
+        private ExtendedInterface<T> delegate;
+
+        @Inject
+        @Decorated
+        private Bean<Interface<T>> decorated;
+
+
+        @Override
+        public void anotherMethod(T input)
+        {
+            Class<?> beanClass = decorated.getBeanClass();
+            if (beanClass.equals(LongService.class))
+            {
+                anotherMethodDecoratedWithLong = true;
+            }
+            else if (beanClass.equals(IntegerService.class))
+            {
+                anotherMethodDecoratedWithInteger = true;
+            }
+            else
+            {
+                unexpectedDecorated = true;
+            }
+
+            delegate.anotherMethod(input);
+        }
+    }
+}

Added: openwebbeans/branches/owb_1.2.x/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/tests/RepeatedGenericTypeDecoratorTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/branches/owb_1.2.x/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/tests/RepeatedGenericTypeDecoratorTest.java?rev=1629883&view=auto
==============================================================================
--- openwebbeans/branches/owb_1.2.x/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/tests/RepeatedGenericTypeDecoratorTest.java (added)
+++ openwebbeans/branches/owb_1.2.x/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/tests/RepeatedGenericTypeDecoratorTest.java Tue Oct  7 13:00:58 2014
@@ -0,0 +1,87 @@
+/*
+ * 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 javax.decorator.Decorator;
+import javax.decorator.Delegate;
+import javax.enterprise.inject.Any;
+import javax.inject.Inject;
+
+import org.apache.webbeans.newtests.AbstractUnitTest;
+import org.junit.Test;
+
+public class RepeatedGenericTypeDecoratorTest extends AbstractUnitTest
+{
+
+    @Test
+    public void testRepeatedGenericDecorator() throws Exception
+    {
+        addDecorator(MyServiceDecorator.class);
+        startContainer(MyInterface.class, MyService.class, MyServiceDecorator.class);
+
+        MyService myService = getInstance(MyService.class);
+
+        myService.doSomethingStrange(new MyInterface<MyInterface>()
+        {
+            @Override
+            public void doSomethingStrange(MyInterface param)
+            {
+                // have no idea here too
+            }
+        });
+
+        shutDownContainer();
+    }
+
+
+    public static interface MyInterface<T>
+    {
+        void doSomethingStrange(T param);
+    }
+
+
+    public static class MyService implements MyInterface<MyInterface<MyInterface>> {
+
+        @Override
+        public void doSomethingStrange(MyInterface<MyInterface> param)
+        {
+            // don't know what to do here
+            // i have no idea for a usecase
+            param.doSomethingStrange(param);
+        }
+    }
+
+
+    @Decorator
+    public static class MyServiceDecorator implements MyInterface<MyInterface<MyInterface>>
+    {
+
+        @Delegate
+        @Inject
+        @Any
+        private MyInterface<MyInterface<MyInterface>> delegate;
+
+        @Override
+        public void doSomethingStrange(MyInterface<MyInterface> param)
+        {
+            // really have no idea what to do here
+            param.doSomethingStrange(param);
+        }
+    }
+}