openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1776257 - in /openwebbeans/trunk: webbeans-impl/src/main/java/org/apache/webbeans/inject/instance/InstanceImpl.java webbeans-impl/src/test/java/org/apache/webbeans/container/OwbCDIProviderTest.java webbeans-tck/testng-dev.xml
Date Wed, 28 Dec 2016 10:36:08 GMT
Author: rmannibucau
Date: Wed Dec 28 10:36:08 2016
New Revision: 1776257

URL: http://svn.apache.org/viewvc?rev=1776257&view=rev
Log:
OWB-1162 better support of CDI.current().select(qualifier)

Modified:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/instance/InstanceImpl.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/container/OwbCDIProviderTest.java
    openwebbeans/trunk/webbeans-tck/testng-dev.xml

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/instance/InstanceImpl.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/instance/InstanceImpl.java?rev=1776257&r1=1776256&r2=1776257&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/instance/InstanceImpl.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/instance/InstanceImpl.java
Wed Dec 28 10:36:08 2016
@@ -37,13 +37,16 @@ import java.util.Set;
 
 import javax.enterprise.context.spi.AlterableContext;
 import javax.enterprise.context.spi.Context;
+import javax.enterprise.inject.Default;
 import javax.enterprise.inject.Instance;
+import javax.enterprise.inject.New;
 import javax.enterprise.inject.spi.Annotated;
 import javax.enterprise.inject.spi.Bean;
 import javax.enterprise.inject.spi.InjectionPoint;
 import javax.enterprise.util.TypeLiteral;
 import javax.inject.Provider;
 
+import org.apache.webbeans.annotation.DefaultLiteral;
 import org.apache.webbeans.config.WebBeansContext;
 import org.apache.webbeans.container.BeanManagerImpl;
 import org.apache.webbeans.container.InjectionResolver;
@@ -178,7 +181,9 @@ public class InstanceImpl<T> implements
     public Instance<T> select(Annotation... qualifiers)
     {
         Annotation[] newQualifiersArray = getAdditionalQualifiers(qualifiers);
-        return new InstanceImpl<T>(injectionClazz, new InstanceInjectionPoint(injectionPoint,
newQualifiersArray), webBeansContext, parentCreationalContext, newQualifiersArray);
+        return new InstanceImpl<T>(
+                injectionClazz, injectionPoint == null ? null : new InstanceInjectionPoint(injectionPoint,
newQualifiersArray),
+                webBeansContext, parentCreationalContext, newQualifiersArray);
     }
 
     /**
@@ -193,14 +198,35 @@ public class InstanceImpl<T> implements
 
         Set<Annotation> newQualifiers = new HashSet<Annotation>(qualifierAnnotations);
 
+        final Set<Class<?>> types = new HashSet<>(newQualifiers.size()
+ (qualifiers != null ? qualifiers.length : 0));
+        for (final Annotation a : newQualifiers)
+        {
+            types.add(a.annotationType());
+        }
+
+        boolean addsDefault = false;
         if (qualifiers != null && qualifiers.length > 0)
         {
             for (int i = 0; i < qualifiers.length; i++)
             {
                 newQualifiers.add(qualifiers[i]);
+                final Class<? extends Annotation> type = qualifiers[i].annotationType();
+                if (type == Default.class)
+                {
+                    addsDefault = true;
+                }
+                else if (!types.add(type))
+                {
+                    throw new IllegalArgumentException("Duplicated qualifier " + qualifiers[i].annotationType());
+                }
             }
         }
 
+        if (!addsDefault && !types.contains(New.class)) // easy way to ensure @New
will not resolve, not yet perfect though
+        {
+            newQualifiers.remove(DefaultLiteral.INSTANCE);
+        }
+
         Annotation[] newQualifiersArray = new Annotation[newQualifiers.size()];
         newQualifiersArray = newQualifiers.toArray(newQualifiersArray);
         

Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/container/OwbCDIProviderTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/container/OwbCDIProviderTest.java?rev=1776257&r1=1776256&r2=1776257&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/container/OwbCDIProviderTest.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/container/OwbCDIProviderTest.java
Wed Dec 28 10:36:08 2016
@@ -21,11 +21,19 @@ package org.apache.webbeans.container;
 import org.apache.webbeans.test.AbstractUnitTest;
 import org.junit.Test;
 
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.inject.Instance;
 import javax.enterprise.inject.spi.CDI;
+import javax.enterprise.util.AnnotationLiteral;
+import javax.inject.Qualifier;
+import java.lang.annotation.Annotation;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
 
+import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
 
 public class OwbCDIProviderTest extends AbstractUnitTest
 {
@@ -39,4 +47,48 @@ public class OwbCDIProviderTest extends
         assertFalse(CDI.current().isAmbiguous());
         assertNotNull(CDI.current().get());
     }
+
+    @Test
+    public void select()
+    {
+        startContainer(ABean.class);
+        final Instance<ABean> select = CDI.current().select(ABean.class);
+        assertFalse(select.isAmbiguous());
+        assertFalse(select.isUnsatisfied());
+        final ABean bean = select.get();
+        assertNotNull(bean);
+    }
+
+    @Test
+    public void selectQualifier()
+    {
+        startContainer(QualifiedBean.class, Qual.class);
+        final Instance<QualifiedBean> select = CDI.current()
+                .select(QualifiedBean.class, new Annotation[0])
+                .select(new AnnotationLiteral<Qual>()
+                {
+                });
+        assertFalse(select.isAmbiguous());
+        assertFalse(select.isUnsatisfied());
+        final QualifiedBean bean = select.get();
+        assertNotNull(bean);
+    }
+
+    @ApplicationScoped
+    public static class ABean
+    {
+    }
+
+    @Target(TYPE)
+    @Retention(RUNTIME)
+    @Qualifier
+    public @interface Qual
+    {
+    }
+
+    @Qual
+    @ApplicationScoped
+    public static class QualifiedBean
+    {
+    }
 }

Modified: openwebbeans/trunk/webbeans-tck/testng-dev.xml
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-tck/testng-dev.xml?rev=1776257&r1=1776256&r2=1776257&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-tck/testng-dev.xml (original)
+++ openwebbeans/trunk/webbeans-tck/testng-dev.xml Wed Dec 28 10:36:08 2016
@@ -18,7 +18,7 @@
 <suite name="JSR-346-TCK" verbose="2" configfailurepolicy="continue">
   <test name="JSR-346 TCK">
     <classes>
-          <class name="org.jboss.cdi.tck.tests.context.DestroyedInstanceReturnedByGetTest"
/>
+          <class name="org.jboss.cdi.tck.tests.lookup.dynamic.DynamicLookupTest" />
       <!--
       <class name="org.jboss.cdi.tck.tests.event.parameterized.ParameterizedEventTest"
/>
       <class name="org.jboss.cdi.tck.tests.event.fires.FireEventTest" />



Mime
View raw message