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/06 14:35:47 UTC

svn commit: r1629632 - in /openwebbeans/trunk/webbeans-impl/src: main/java/org/apache/webbeans/component/creation/ main/java/org/apache/webbeans/intercept/ test/java/org/apache/webbeans/test/decorators/tests/

Author: rsandtner
Date: Mon Oct  6 12:35:47 2014
New Revision: 1629632

URL: http://svn.apache.org/r1629632
Log:
OWB-992 added check for ParameterizedTypes

Modified:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/DecoratorBeanBuilder.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorResolutionService.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/decorators/tests/ExtendedGenericDecoratorTest.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=1629632&r1=1629631&r2=1629632&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 Oct  6 12:35:47 2014
@@ -26,6 +26,7 @@ import javax.enterprise.inject.Alternati
 import javax.enterprise.inject.spi.AnnotatedMethod;
 import javax.enterprise.inject.spi.AnnotatedType;
 import javax.enterprise.inject.spi.BeanAttributes;
+import javax.enterprise.inject.spi.DefinitionException;
 import javax.enterprise.inject.spi.InjectionPoint;
 import javax.inject.Inject;
 
@@ -33,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.Collections;
@@ -215,11 +217,54 @@ public class DecoratorBeanBuilder<T> ext
             {
                 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 DefinitionException("Decorator: " + toString() + " delegate attribute must match decorated type: " + decTypeArg);
             }
         }
     }

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorResolutionService.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorResolutionService.java?rev=1629632&r1=1629631&r2=1629632&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorResolutionService.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorResolutionService.java Mon Oct  6 12:35:47 2014
@@ -565,7 +565,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;

Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/decorators/tests/ExtendedGenericDecoratorTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/decorators/tests/ExtendedGenericDecoratorTest.java?rev=1629632&r1=1629631&r2=1629632&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/decorators/tests/ExtendedGenericDecoratorTest.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/decorators/tests/ExtendedGenericDecoratorTest.java Mon Oct  6 12:35:47 2014
@@ -22,6 +22,8 @@ import javax.annotation.Priority;
 import javax.decorator.Decorator;
 import javax.decorator.Delegate;
 import javax.enterprise.inject.Any;
+import javax.enterprise.inject.Decorated;
+import javax.enterprise.inject.spi.Bean;
 import javax.inject.Inject;
 
 import junit.framework.Assert;
@@ -35,36 +37,45 @@ import org.junit.Test;
 public class ExtendedGenericDecoratorTest extends AbstractUnitTest
 {
     @Test
-    @Ignore
     public void testExtendedGenericDecorator() throws Exception
     {
         addDecorator(ExtendedInterfaceDecorator.class);
 
-        startContainer(Interface.class, ExtendedInterface.class, LongService.class, IntegerService.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.testMethodDecorated);
-        Assert.assertFalse(ExtendedInterfaceDecorator.anotherMethodDecorated);
+        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
 
-        // call method from interface
-        service.anotherMethod(2L);
+        // 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(ExtendedInterfaceDecorator.anotherMethodDecorated);
-
-        // call method from super interface
-        service.test(1L);
 
+        Assert.assertTrue(LongService.anotherMethodCalled);
         Assert.assertTrue(LongService.testMethodCalled);
-        Assert.assertTrue(ExtendedInterfaceDecorator.testMethodDecorated);
+        Assert.assertTrue(ExtendedInterfaceDecorator.anotherMethodDecoratedWithLong);
+        Assert.assertTrue(IntegerService.anotherMethodCalled);
+        Assert.assertTrue(IntegerService.testMethodCalled);
+        Assert.assertTrue(ExtendedInterfaceDecorator.anotherMethodDecoratedWithInteger);
+        Assert.assertTrue(StringService.testMethodCalled);
 
-        shutDownContainer();
 
+        shutDownContainer();
     }
 
 
@@ -113,31 +124,54 @@ public class ExtendedGenericDecoratorTes
         }
     }
 
+    public static class StringService implements Interface<String> {
+
+        public static boolean testMethodCalled = false;
+
+        @Override
+        public void test(String input)
+        {
+            testMethodCalled = true;
+        }
+    }
+
     @Decorator
     @Priority(value = 10)
-    public static class ExtendedInterfaceDecorator<T extends Number> implements ExtendedInterface<T> {
+    public abstract static class ExtendedInterfaceDecorator<T extends Number> implements ExtendedInterface<T> {
 
-        public static boolean testMethodDecorated = false;
-        public static boolean anotherMethodDecorated = false;
+        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)
         {
-            anotherMethodDecorated = true;
-            delegate.anotherMethod(input);
-        }
+            Class<?> beanClass = decorated.getBeanClass();
+            if (beanClass.equals(LongService.class))
+            {
+                anotherMethodDecoratedWithLong = true;
+            }
+            else if (beanClass.equals(IntegerService.class))
+            {
+                anotherMethodDecoratedWithInteger = true;
+            }
+            else
+            {
+                unexpectedDecorated = true;
+            }
 
-        @Override
-        public void test(T input)
-        {
-            testMethodDecorated = true;
-            delegate.test(input);
+            delegate.anotherMethod(input);
         }
     }
 }



Fwd: svn commit: r1629632 - in /openwebbeans/trunk/webbeans-impl/src: main/java/org/apache/webbeans/component/creation/ main/java/org/apache/webbeans/intercept/ test/java/org/apache/webbeans/test/decorators/tests/

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Hi

I guess checkParametrizedType should be protected against loops since
you can be parameterized with yourself (stupid but not forbidden),
wdyt?


Romain Manni-Bucau
@rmannibucau
http://www.tomitribe.com
http://rmannibucau.wordpress.com
https://github.com/rmannibucau



---------- Forwarded message ----------
From:  <rs...@apache.org>
Date: 2014-10-06 14:35 GMT+02:00
Subject: svn commit: r1629632 - in
/openwebbeans/trunk/webbeans-impl/src:
main/java/org/apache/webbeans/component/creation/
main/java/org/apache/webbeans/intercept/
test/java/org/apache/webbeans/test/decorators/tests/
To: commits@openwebbeans.apache.org


Author: rsandtner
Date: Mon Oct  6 12:35:47 2014
New Revision: 1629632

URL: http://svn.apache.org/r1629632
Log:
OWB-992 added check for ParameterizedTypes

Modified:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/DecoratorBeanBuilder.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorResolutionService.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/decorators/tests/ExtendedGenericDecoratorTest.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=1629632&r1=1629631&r2=1629632&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 Oct  6 12:35:47 2014
@@ -26,6 +26,7 @@ import javax.enterprise.inject.Alternati
 import javax.enterprise.inject.spi.AnnotatedMethod;
 import javax.enterprise.inject.spi.AnnotatedType;
 import javax.enterprise.inject.spi.BeanAttributes;
+import javax.enterprise.inject.spi.DefinitionException;
 import javax.enterprise.inject.spi.InjectionPoint;
 import javax.inject.Inject;

@@ -33,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.Collections;
@@ -215,11 +217,54 @@ public class DecoratorBeanBuilder<T> ext
             {
                 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 DefinitionException("Decorator: " +
toString() + " delegate attribute must match decorated type: " +
decTypeArg);
             }
         }
     }

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorResolutionService.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorResolutionService.java?rev=1629632&r1=1629631&r2=1629632&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorResolutionService.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorResolutionService.java
Mon Oct  6 12:35:47 2014
@@ -565,7 +565,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;

Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/decorators/tests/ExtendedGenericDecoratorTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/decorators/tests/ExtendedGenericDecoratorTest.java?rev=1629632&r1=1629631&r2=1629632&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/decorators/tests/ExtendedGenericDecoratorTest.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/decorators/tests/ExtendedGenericDecoratorTest.java
Mon Oct  6 12:35:47 2014
@@ -22,6 +22,8 @@ import javax.annotation.Priority;
 import javax.decorator.Decorator;
 import javax.decorator.Delegate;
 import javax.enterprise.inject.Any;
+import javax.enterprise.inject.Decorated;
+import javax.enterprise.inject.spi.Bean;
 import javax.inject.Inject;

 import junit.framework.Assert;
@@ -35,36 +37,45 @@ import org.junit.Test;
 public class ExtendedGenericDecoratorTest extends AbstractUnitTest
 {
     @Test
-    @Ignore
     public void testExtendedGenericDecorator() throws Exception
     {
         addDecorator(ExtendedInterfaceDecorator.class);

-        startContainer(Interface.class, ExtendedInterface.class,
LongService.class, IntegerService.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.testMethodDecorated);
-        Assert.assertFalse(ExtendedInterfaceDecorator.anotherMethodDecorated);
+        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

-        // call method from interface
-        service.anotherMethod(2L);
+        // 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(ExtendedInterfaceDecorator.anotherMethodDecorated);
-
-        // call method from super interface
-        service.test(1L);

+        Assert.assertTrue(LongService.anotherMethodCalled);
         Assert.assertTrue(LongService.testMethodCalled);
-        Assert.assertTrue(ExtendedInterfaceDecorator.testMethodDecorated);
+        Assert.assertTrue(ExtendedInterfaceDecorator.anotherMethodDecoratedWithLong);
+        Assert.assertTrue(IntegerService.anotherMethodCalled);
+        Assert.assertTrue(IntegerService.testMethodCalled);
+        Assert.assertTrue(ExtendedInterfaceDecorator.anotherMethodDecoratedWithInteger);
+        Assert.assertTrue(StringService.testMethodCalled);

-        shutDownContainer();

+        shutDownContainer();
     }


@@ -113,31 +124,54 @@ public class ExtendedGenericDecoratorTes
         }
     }

+    public static class StringService implements Interface<String> {
+
+        public static boolean testMethodCalled = false;
+
+        @Override
+        public void test(String input)
+        {
+            testMethodCalled = true;
+        }
+    }
+
     @Decorator
     @Priority(value = 10)
-    public static class ExtendedInterfaceDecorator<T extends Number>
implements ExtendedInterface<T> {
+    public abstract static class ExtendedInterfaceDecorator<T extends
Number> implements ExtendedInterface<T> {

-        public static boolean testMethodDecorated = false;
-        public static boolean anotherMethodDecorated = false;
+        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)
         {
-            anotherMethodDecorated = true;
-            delegate.anotherMethod(input);
-        }
+            Class<?> beanClass = decorated.getBeanClass();
+            if (beanClass.equals(LongService.class))
+            {
+                anotherMethodDecoratedWithLong = true;
+            }
+            else if (beanClass.equals(IntegerService.class))
+            {
+                anotherMethodDecoratedWithInteger = true;
+            }
+            else
+            {
+                unexpectedDecorated = true;
+            }

-        @Override
-        public void test(T input)
-        {
-            testMethodDecorated = true;
-            delegate.test(input);
+            delegate.anotherMethod(input);
         }
     }
 }