openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1849277 - in /openwebbeans/trunk/webbeans-impl/src: main/java/org/apache/webbeans/configurator/BeanConfiguratorImpl.java test/java/org/apache/webbeans/test/injection/named/NamedTests.java
Date Wed, 19 Dec 2018 08:31:37 GMT
Author: rmannibucau
Date: Wed Dec 19 08:31:37 2018
New Revision: 1849277

URL: http://svn.apache.org/viewvc?rev=1849277&view=rev
Log:
OWB-1276 ensure BeanConfigurator manages @Default/@Any qualifiers properly for @Named qualified
beans

Modified:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/configurator/BeanConfiguratorImpl.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/injection/named/NamedTests.java

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/configurator/BeanConfiguratorImpl.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/configurator/BeanConfiguratorImpl.java?rev=1849277&r1=1849276&r2=1849277&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/configurator/BeanConfiguratorImpl.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/configurator/BeanConfiguratorImpl.java
Wed Dec 19 08:31:37 2018
@@ -20,6 +20,7 @@ package org.apache.webbeans.configurator
 
 import javax.enterprise.context.Dependent;
 import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.inject.Any;
 import javax.enterprise.inject.Instance;
 import javax.enterprise.inject.spi.AnnotatedType;
 import javax.enterprise.inject.spi.Bean;
@@ -30,6 +31,8 @@ import javax.enterprise.inject.spi.Passi
 import javax.enterprise.inject.spi.Producer;
 import javax.enterprise.inject.spi.configurator.BeanConfigurator;
 import javax.enterprise.util.TypeLiteral;
+import javax.inject.Named;
+
 import java.lang.annotation.Annotation;
 import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.Type;
@@ -47,8 +50,11 @@ import org.apache.webbeans.component.Web
 import org.apache.webbeans.component.creation.BeanAttributesBuilder;
 import org.apache.webbeans.config.WebBeansContext;
 import org.apache.webbeans.exception.WebBeansConfigurationException;
+import org.apache.webbeans.util.AnnotationUtil;
 import org.apache.webbeans.util.GenericsUtil;
 
+import static java.util.stream.Collectors.joining;
+
 //X TODO finish. Impossible to implement right now as the spec is ambiguous
 //X TODO producer part
 public class BeanConfiguratorImpl<T> implements BeanConfigurator<T>
@@ -253,10 +259,7 @@ public class BeanConfiguratorImpl<T> imp
     @Override
     public BeanConfigurator<T> addQualifiers(Annotation... qualifiers)
     {
-        for (Annotation qualifier : qualifiers)
-        {
-            this.qualifiers.add(qualifier);
-        }
+        this.qualifiers.addAll(Arrays.asList(qualifiers));
         return this;
     }
 
@@ -321,23 +324,52 @@ public class BeanConfiguratorImpl<T> imp
 
     public Bean<?> getBean()
     {
+        updateQualifiers();
+        setPassivationIdIfNeeded();
+        return new ConstructedBean();
+    }
+
+    private void setPassivationIdIfNeeded()
+    {
+        if (!webBeansContext.getBeanManagerImpl().isPassivatingScope(scope) || passivationId
!= null)
+        {
+            return;
+        }
+        final StringBuilder sb = new StringBuilder("CONFIGURATOR#");
+        sb.append(beanClass != null ? beanClass : typeClosures.stream().filter(Class.class::isInstance).findFirst()
+                .filter(it -> it != Object.class)
+                .orElse(Object.class)).append('#').append(qualifiers.stream().map(Annotation::toString).collect(
+                joining(",")));
+        for (final Annotation qualifier : qualifiers)
+        {
+            sb.append(qualifier.toString()).append(',');
+        }
+        passivationId = sb.toString();
+    }
+
+    // same as org.apache.webbeans.component.creation.BeanAttributesBuilder#defineQualifiers
+    private void updateQualifiers()
+    {
         if (qualifiers.isEmpty())
         {
             qualifiers.add(DefaultLiteral.INSTANCE);
-            qualifiers.add(AnyLiteral.INSTANCE);
         }
-        if (scope != null && webBeansContext.getBeanManagerImpl().isPassivatingScope(scope)
&& passivationId == null)
+        else if (qualifiers.size() == 1)
         {
-            final StringBuilder sb = new StringBuilder("CONFIGURATOR#");
-            sb.append(beanClass != null ? beanClass : typeClosures.stream().filter(Class.class::isInstance).findFirst()
-                    .orElse(Object.class)).append('#');
-            for (final Annotation qualifier : qualifiers)
+            final Class<? extends Annotation> annotationType = qualifiers.iterator().next().annotationType();
+            if (annotationType == Named.class || annotationType == Any.class)
             {
-                sb.append(qualifier.toString()).append(',');
+                qualifiers.add(DefaultLiteral.INSTANCE);
             }
-            passivationId = sb.toString();
         }
-        return new ConstructedBean();
+        else if (qualifiers.size() == 2 && qualifiers.stream().allMatch(it ->
it.annotationType() == Named.class))
+        {
+            qualifiers.add(DefaultLiteral.INSTANCE);
+        }
+        if (AnnotationUtil.getAnnotation(qualifiers, Any.class) == null)
+        {
+            qualifiers.add(AnyLiteral.INSTANCE);
+        }
     }
 
     /**
@@ -354,7 +386,9 @@ public class BeanConfiguratorImpl<T> imp
         {
             //X TODO calculate return type from the typeClosures properly
             this.returnType = beanClass != null ? Class.class.cast(beanClass) : (typeClosures.isEmpty()
? null :
-                    Class.class.cast(typeClosures.stream().filter(Class.class::isInstance).findFirst().orElse(null)));
+                    Class.class.cast(typeClosures.stream().filter(Class.class::isInstance).findFirst()
+                                                 .filter(it -> it != Object.class)
+                                                 .orElse(Object.class)));
 
             dependent = !webBeansContext.getBeanManagerImpl().isNormalScope(scope);
 

Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/injection/named/NamedTests.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/injection/named/NamedTests.java?rev=1849277&r1=1849276&r2=1849277&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/injection/named/NamedTests.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/injection/named/NamedTests.java
Wed Dec 19 08:31:37 2018
@@ -18,11 +18,17 @@
  */
 package org.apache.webbeans.test.injection.named;
 
+import static org.apache.webbeans.util.Asserts.assertNotNull;
+
 import java.util.ArrayList;
 import java.util.Collection;
 
 import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.event.Observes;
+import javax.enterprise.inject.literal.NamedLiteral;
+import javax.enterprise.inject.spi.AfterBeanDiscovery;
 import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.Extension;
 
 import org.apache.webbeans.test.AbstractUnitTest;
 import org.junit.Assert;
@@ -51,9 +57,26 @@ public class NamedTests extends Abstract
         Assert.assertEquals("NamedBean", consumer.getNamed().getName());
         Assert.assertEquals("DefaultNamedBean", consumer.getDefaultNamedBeanWithNamedInjectionPoint().getName());
         Assert.assertEquals("DefaultNamedBean", consumer.getDefaultNamedBean().getName());
-        
+
         shutDownContainer();       
         
     }
 
+    @Test
+    public void beanConfiguratorNamedQualifier()
+    {
+        addExtension(new Extension()
+        {
+            void addBean(@Observes final AfterBeanDiscovery afterBeanDiscovery)
+            {
+                afterBeanDiscovery.addBean()
+                                  .qualifiers(NamedLiteral.of("test"))
+                                  .beanClass(NamedInterface.class)
+                                  .types(NamedInterface.class)
+                                  .createWith(it -> new NamedBean());
+            }
+        });
+        startContainer();
+        assertNotNull(getInstance(NamedInterface.class));
+    }
 }



Mime
View raw message