OWB-1287 enable to omit @Inject when a qualifier is already present on a field/method

git-svn-id: https://svn.apache.org/repos/asf/openwebbeans/trunk@1859029 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/webbeans-impl/src/main/java/org/apache/webbeans/config/OpenWebBeansConfiguration.java b/webbeans-impl/src/main/java/org/apache/webbeans/config/OpenWebBeansConfiguration.java
index ba0684f..6434914 100644
--- a/webbeans-impl/src/main/java/org/apache/webbeans/config/OpenWebBeansConfiguration.java
+++ b/webbeans-impl/src/main/java/org/apache/webbeans/config/OpenWebBeansConfiguration.java
@@ -122,6 +122,9 @@
     /**Supports conversations*/
     public static final String APPLICATION_SUPPORTS_CONVERSATION = "org.apache.webbeans.application.supportsConversation";
 
+    public static final String APPLICATION_SUPPORTS_IMPLICIT_QUALIFIER_INJECTION =
+            "org.apache.webbeans.application.supportsImplicitQualifierInjection";
+
     /** @Produces with interceptor/decorator support */
     public static final String PRODUCER_INTERCEPTION_SUPPORT = "org.apache.webbeans.application.supportsProducerInterception";
 
@@ -241,6 +244,8 @@
      */
     private Map<String, Set<String>> configuredLists = new HashMap<>();
 
+    private boolean implicitQualifierInjection;
+
 
     /**
      * you can configure this externally as well.
@@ -284,6 +289,7 @@
             configProperties.putAll(newConfigProperties);
         }
 
+        implicitQualifierInjection = Boolean.parseBoolean(getProperty(APPLICATION_SUPPORTS_IMPLICIT_QUALIFIER_INJECTION));
     }
 
     /**
@@ -538,4 +544,9 @@
 
         return generatorJavaVersion;
     }
+
+    public boolean supportsImplicitQualifierInjection()
+    {
+        return implicitQualifierInjection;
+    }
 }
diff --git a/webbeans-impl/src/main/java/org/apache/webbeans/inject/impl/InjectionPointFactory.java b/webbeans-impl/src/main/java/org/apache/webbeans/inject/impl/InjectionPointFactory.java
index 0823266..38b044e 100644
--- a/webbeans-impl/src/main/java/org/apache/webbeans/inject/impl/InjectionPointFactory.java
+++ b/webbeans-impl/src/main/java/org/apache/webbeans/inject/impl/InjectionPointFactory.java
@@ -21,6 +21,7 @@
 import org.apache.webbeans.annotation.AnnotationManager;
 import org.apache.webbeans.annotation.NamedLiteral;
 import org.apache.webbeans.config.WebBeansContext;
+import org.apache.webbeans.container.BeanManagerImpl;
 import org.apache.webbeans.exception.WebBeansConfigurationException;
 import org.apache.webbeans.portable.events.generics.GProcessInjectionPoint;
 import org.apache.webbeans.util.AnnotationUtil;
@@ -30,6 +31,7 @@
 import javax.enterprise.event.ObservesAsync;
 import javax.enterprise.inject.Disposes;
 import javax.enterprise.inject.Produces;
+import javax.enterprise.inject.spi.Annotated;
 import javax.enterprise.inject.spi.AnnotatedCallable;
 import javax.enterprise.inject.spi.AnnotatedConstructor;
 import javax.enterprise.inject.spi.AnnotatedField;
@@ -88,14 +90,14 @@
                             + owner.getBeanClass().getName());
                 }
             }                
-            if (field.isAnnotationPresent(Inject.class))
+            if (isInjecting(field))
             {
                 injectionPoints.add(buildInjectionPoint(owner, field));
             }
         }
         for (AnnotatedMethod<? super X> method: webBeansContext.getAnnotatedElementFactory().getFilteredAnnotatedMethods(annotatedType))
         {
-            if (method.isAnnotationPresent(Inject.class) && !Modifier.isStatic(method.getJavaMember().getModifiers()))
+            if (!Modifier.isStatic(method.getJavaMember().getModifiers()) && isInjecting(method))
             {
                 validateInitializerMethod(method);
                 buildInjectionPoints(owner, method, injectionPoints);
@@ -104,6 +106,21 @@
         return injectionPoints;
     }
 
+    private boolean isInjecting(final Annotated field)
+    {
+        if (field.isAnnotationPresent(Inject.class))
+        {
+            return true;
+        }
+        if (!webBeansContext.getOpenWebBeansConfiguration().supportsImplicitQualifierInjection())
+        {
+            return false;
+        }
+        final BeanManagerImpl mgr = webBeansContext.getBeanManagerImpl();
+        return field.getAnnotations().stream().anyMatch(a -> mgr.isQualifier(a.annotationType()))
+                && field.getAnnotations().stream().noneMatch(it -> it.annotationType() == Produces.class);
+    }
+
     public <X> InjectionPoint buildInjectionPoint(Bean<?> owner, AnnotatedField<X> annotField, boolean fireEvent)
     {
         Asserts.assertNotNull(annotField, "annotField");
diff --git a/webbeans-impl/src/main/java/org/apache/webbeans/inject/impl/InjectionPointImpl.java b/webbeans-impl/src/main/java/org/apache/webbeans/inject/impl/InjectionPointImpl.java
index e44acad..1f91906 100644
--- a/webbeans-impl/src/main/java/org/apache/webbeans/inject/impl/InjectionPointImpl.java
+++ b/webbeans-impl/src/main/java/org/apache/webbeans/inject/impl/InjectionPointImpl.java
@@ -59,7 +59,7 @@
 {
     private static final long serialVersionUID = 1047233127758068484L;
 
-    private Set<Annotation> qualifierAnnotations = new HashSet<>();
+    private Set<Annotation> qualifierAnnotations;
     
     private Bean<?> ownerBean;
     
diff --git a/webbeans-impl/src/main/resources/META-INF/openwebbeans/openwebbeans.properties b/webbeans-impl/src/main/resources/META-INF/openwebbeans/openwebbeans.properties
index 517d25f..803ab54 100644
--- a/webbeans-impl/src/main/resources/META-INF/openwebbeans/openwebbeans.properties
+++ b/webbeans-impl/src/main/resources/META-INF/openwebbeans/openwebbeans.properties
@@ -97,6 +97,10 @@
 org.apache.webbeans.application.supportsConversation=false
 ################################################################################################
 
+################################# Injection Support #########################################
+org.apache.webbeans.application.supportsImplicitQualifierInjection=true
+################################################################################################
+
 ################################### Default Conversation Service ###############################
 # Default implementation of org.apache.webbeans.corespi.ConversationService.
 # This one does not support conversation propagation. It's basically a no-op implementation
diff --git a/webbeans-impl/src/test/java/org/apache/webbeans/test/configurator/AnnotatedTypeConfiguratorImplTest.java b/webbeans-impl/src/test/java/org/apache/webbeans/test/configurator/AnnotatedTypeConfiguratorImplTest.java
index da6fc76..fb534b5 100644
--- a/webbeans-impl/src/test/java/org/apache/webbeans/test/configurator/AnnotatedTypeConfiguratorImplTest.java
+++ b/webbeans-impl/src/test/java/org/apache/webbeans/test/configurator/AnnotatedTypeConfiguratorImplTest.java
@@ -228,9 +228,7 @@
     {
         checkAnnotatedType(pat -> pat.configureAnnotatedType()
                                      .filterFields(af -> "field2".equals(af.getJavaMember().getName()))
-                                     .findFirst()
-                                     .get()
-                                     .remove(a -> a.annotationType() == TheQualifier.class),
+                                     .forEach(c -> c.remove(a -> a.annotationType() == TheQualifier.class)),
                            pba ->
                            {
                                Assert.assertTrue(pba.getAnnotated() instanceof AnnotatedType);
diff --git a/webbeans-impl/src/test/java/org/apache/webbeans/test/qualifier/QualifierWithOptionalInjectTest.java b/webbeans-impl/src/test/java/org/apache/webbeans/test/qualifier/QualifierWithOptionalInjectTest.java
new file mode 100644
index 0000000..8af9960
--- /dev/null
+++ b/webbeans-impl/src/test/java/org/apache/webbeans/test/qualifier/QualifierWithOptionalInjectTest.java
@@ -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.test.qualifier;
+
+import org.apache.webbeans.config.OwbParametrizedTypeImpl;
+import org.apache.webbeans.test.AbstractUnitTest;
+import org.junit.Test;
+
+import javax.enterprise.context.Dependent;
+import javax.enterprise.inject.Produces;
+import javax.enterprise.inject.spi.InjectionPoint;
+import javax.enterprise.util.Nonbinding;
+import javax.inject.Qualifier;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+import java.util.function.Supplier;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+import static org.junit.Assert.assertEquals;
+
+public class QualifierWithOptionalInjectTest extends AbstractUnitTest
+{
+    @Test
+    public void run()
+    {
+        System.setProperty("org.apache.webbeans.application.supportsImplicitQualifierInjection", "true");
+        startContainer(Producing.class, Injected.class);
+        final OwbParametrizedTypeImpl type = new OwbParametrizedTypeImpl(null, Supplier.class, String.class);
+        final Supplier<String> injected = getInstance(type);
+        assertEquals("yes/no", injected.get());
+        System.clearProperty("org.apache.webbeans.application.supportsImplicitQualifierInjection");
+    }
+
+    @Dependent
+    public static class Producing
+    {
+        @Produces
+        @TheQualifier("whatever")
+        String yes(final InjectionPoint injectionPoint) {
+            return new StringBuilder(
+                    injectionPoint.getAnnotated().getAnnotation(TheQualifier.class).value()).reverse().toString();
+        }
+    }
+
+    @Dependent
+    public static class Injected implements Supplier<String>
+    {
+        @TheQualifier("sey")
+        private String yes;
+
+        @TheQualifier("on")
+        private String no;
+
+        @Override
+        public String get()
+        {
+            return yes + '/' + no;
+        }
+    }
+
+    @Qualifier
+    @Target({FIELD, METHOD})
+    @Retention(RUNTIME)
+    public @interface TheQualifier
+    {
+        @Nonbinding
+        String value();
+    }
+}
diff --git a/webbeans-impl/src/test/resources/META-INF/openwebbeans/openwebbeans.properties b/webbeans-impl/src/test/resources/META-INF/openwebbeans/openwebbeans.properties
index 38ec5d1..5068acb 100644
--- a/webbeans-impl/src/test/resources/META-INF/openwebbeans/openwebbeans.properties
+++ b/webbeans-impl/src/test/resources/META-INF/openwebbeans/openwebbeans.properties
@@ -32,3 +32,7 @@
 
 org.apache.webbeans.proxy.mapping.javax.enterprise.context.RequestScoped=org.apache.webbeans.intercept.NormalScopedBeanInterceptorHandler
 org.apache.webbeans.proxy.mapping.javax.enterprise.context.ApplicationScoped=org.apache.webbeans.intercept.ApplicationScopedBeanInterceptorHandler
+
+
+# some tests misused that so revert it to ensure they pass
+org.apache.webbeans.application.supportsImplicitQualifierInjection=false
\ No newline at end of file
diff --git a/webbeans-tck/src/main/resources/META-INF/openwebbeans/openwebbeans.properties b/webbeans-tck/src/main/resources/META-INF/openwebbeans/openwebbeans.properties
index 1eef41a..801ad35 100644
--- a/webbeans-tck/src/main/resources/META-INF/openwebbeans/openwebbeans.properties
+++ b/webbeans-tck/src/main/resources/META-INF/openwebbeans/openwebbeans.properties
@@ -42,3 +42,6 @@
 
 org.apache.webbeans.container.InjectionResolver.fastMatching = false
 
+# only org.jboss.cdi.tck.tests.extensions.beanManager.beanAttributes.CreateBeanAttributesTest currently
+# we can write an arquillian extension to avoid to set it globally
+org.apache.webbeans.application.supportsImplicitQualifierInjection=false
diff --git a/webbeans-tck/testng-dev.xml b/webbeans-tck/testng-dev.xml
index 988b043..7894cbe 100644
--- a/webbeans-tck/testng-dev.xml
+++ b/webbeans-tck/testng-dev.xml
@@ -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.alternative.selection.stereotype.SelectedBeanWithUnselectedStereotypeTest" />
+          <class name="org.jboss.cdi.tck.tests.extensions.beanManager.beanAttributes.CreateBeanAttributesTest" />
       <!--
 
       <class name="org.jboss.cdi.tck.tests.event.parameterized.ParameterizedEventTest" />