openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject svn commit: r1682658 - in /openwebbeans/trunk/webbeans-impl/src: main/java/org/apache/webbeans/container/ main/java/org/apache/webbeans/util/ test/java/org/apache/webbeans/test/injection/generics/
Date Sat, 30 May 2015 21:27:41 GMT
Author: struberg
Date: Sat May 30 21:27:41 2015
New Revision: 1682658

URL: http://svn.apache.org/r1682658
Log:
OWB-654 fix manual lookup of beans with generic types

Txs to Daniel Cunha (soro) for the patch!

Modified:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/BeanManagerImpl.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/InjectionResolver.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/GenericsUtil.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/injection/generics/GenericsTest.java

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/BeanManagerImpl.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/BeanManagerImpl.java?rev=1682658&r1=1682657&r2=1682658&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/BeanManagerImpl.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/BeanManagerImpl.java
Sat May 30 21:27:41 2015
@@ -731,7 +731,8 @@ public class BeanManagerImpl implements
             final boolean isProducer = AbstractProducerBean.class.isInstance(bean);
             if(!isProducer && // we have different rules for producers
                !isBeanTypeAssignableToGivenType(bean.getTypes(), beanType, bean instanceof
NewBean, isProducer) &&
-               !GenericsUtil.satisfiesDependency(false, isProducer, beanType, bean.getBeanClass()))
+               !GenericsUtil.satisfiesDependency(false, isProducer, beanType, bean.getBeanClass())
&&
+               !GenericsUtil.satisfiesDependencyRaw(false, isProducer, beanType, bean.getBeanClass()))
             {
                 throw new IllegalArgumentException("Given bean type : " + beanType + " is
not applicable for the bean instance : " + bean);
             }

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/InjectionResolver.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/InjectionResolver.java?rev=1682658&r1=1682657&r2=1682658&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/InjectionResolver.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/InjectionResolver.java
Sat May 30 21:27:41 2015
@@ -489,9 +489,16 @@ public class InjectionResolver
             resolvedComponents = findByQualifier(resolvedComponents, injectionPointType,
qualifiers);
 
             // have an additional round of checks for assignability of parameterized types.
-            resolvedComponents = findByParameterizedType(resolvedComponents, injectionPointType,
isDelegate);
+            Set<Bean<?>> byParameterizedType = findByParameterizedType(resolvedComponents,
injectionPointType, isDelegate);
+            if (byParameterizedType.isEmpty())
+            {
+                resolvedComponents = findByBeanType(resolvedComponents, injectionPointType,
isDelegate);
+            }
+            else
+            {
+                resolvedComponents = byParameterizedType;
+            }
         }
-
         resolvedBeansByType.put(cacheKey, resolvedComponents);
         if (logger.isLoggable(Level.FINE))
         {
@@ -501,6 +508,29 @@ public class InjectionResolver
         return resolvedComponents;
     }
 
+    private Set<Bean<?>> findByBeanType(Set<Bean<?>> allComponents,
Type injectionPointType, boolean isDelegate)
+    {
+        Set<Bean<?>> resolved = new HashSet<Bean<?>>();
+        for (Bean<?> bean : allComponents)
+        {
+            boolean isProducer = AbstractProducerBean.class.isInstance(bean);
+            for (Type type : bean.getTypes())
+            {
+                if (GenericsUtil.satisfiesDependency(isDelegate, isProducer, injectionPointType,
type))
+                {
+                    resolved.add(bean);
+                }
+
+                if (!ClassUtil.isParametrizedType(injectionPointType)
+                        && ClassUtil.isRawClassEquals(injectionPointType, type))
+                {
+                    resolved.add(bean);
+                }
+            }
+        }
+        return resolved;
+    }
+
     private Set<Bean<?>> findByParameterizedType(Set<Bean<?>> allComponents,
Type injectionPointType, boolean isDelegate)
     {
         Bean<?> rawProducerBean = null;

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/GenericsUtil.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/GenericsUtil.java?rev=1682658&r1=1682657&r2=1682658&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/GenericsUtil.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/GenericsUtil.java
Sat May 30 21:27:41 2015
@@ -65,6 +65,48 @@ public final class GenericsUtil
         return false;
     }
 
+    public static boolean satisfiesDependencyRaw(boolean isDelegateOrEvent, boolean isProducer,
Type injectionPointType, Type beanType)
+    {
+        if (beanType instanceof TypeVariable || beanType instanceof WildcardType || beanType
instanceof GenericArrayType)
+        {
+            return isAssignableFrom(isDelegateOrEvent, isProducer, injectionPointType, beanType);
+        }
+        else
+        {
+            Type injectionPointRawType = injectionPointType instanceof ParameterizedType?
((ParameterizedType)injectionPointType).getRawType(): injectionPointType;
+            Type beanRawType = beanType instanceof ParameterizedType? ((ParameterizedType)beanType).getRawType():
beanType;
+
+            if  (ClassUtil.isSame(injectionPointRawType, beanRawType))
+            {
+                return isAssignableFrom(isDelegateOrEvent, isProducer, injectionPointRawType,
beanRawType);
+            }
+            else
+            {
+                Class bean = (Class) beanType;
+                if (bean.getSuperclass() != null && ClassUtil.isRawClassEquals(injectionPointType,
bean.getSuperclass()))
+                {
+                    return true;
+                }
+
+                Class<?>[] interfaces = bean.getInterfaces();
+                if (interfaces == null || interfaces.length == 0)
+                {
+                    return false;
+                }
+
+                for (Class<?> clazz : interfaces)
+                {
+                    if (ClassUtil.isRawClassEquals(injectionPointType, clazz))
+                    {
+                        return true;
+                    }
+                }
+            }
+        }
+
+        return false;
+    }
+
     /**
      * 5.2.3 and 5.2.4
      */

Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/injection/generics/GenericsTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/injection/generics/GenericsTest.java?rev=1682658&r1=1682657&r2=1682658&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/injection/generics/GenericsTest.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/injection/generics/GenericsTest.java
Sat May 30 21:27:41 2015
@@ -127,4 +127,11 @@ public class GenericsTest extends Abstra
         Assert.assertNotNull(bazEnum);
         Assert.assertEquals(BazEnum.YES, bazEnum);
     }
+
+    @Test
+    public void testManualLookupsOfBeansWithGenerics() {
+        startContainer(MyBean.class, MyInterface.class, MyAbstract.class, StringBean.class,
StringBeanAbstract.class);
+        MyBean instance = getInstance(MyBean.class);
+        instance.checkAll();
+    }
 }



Mime
View raw message