You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by st...@apache.org on 2014/11/25 21:59:52 UTC

svn commit: r1641694 - in /openwebbeans/trunk: ./ webbeans-impl/src/main/java/org/apache/webbeans/config/ webbeans-impl/src/main/java/org/apache/webbeans/container/ webbeans-impl/src/main/java/org/apache/webbeans/util/

Author: struberg
Date: Tue Nov 25 20:59:51 2014
New Revision: 1641694

URL: http://svn.apache.org/r1641694
Log:
OWB-1031 improve error handling for ambiguous injection points

Modified:
    openwebbeans/trunk/   (props changed)
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java
    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/InjectionExceptionUtil.java

Propchange: openwebbeans/trunk/
------------------------------------------------------------------------------
  Merged /openwebbeans/branches/owb_1.2.x:r1641684

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java?rev=1641694&r1=1641693&r2=1641694&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java Tue Nov 25 20:59:51 2014
@@ -785,7 +785,7 @@ public class BeansDeployer
                         {
                             try
                             {
-                                resolver.resolve(beans);
+                                resolver.resolve(beans, null);
                             }
                             catch(AmbiguousResolutionException are)
                             {

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=1641694&r1=1641693&r2=1641694&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 Tue Nov 25 20:59:51 2014
@@ -1121,7 +1121,7 @@ public class BeanManagerImpl implements 
     @Override
     public <X> Bean<? extends X> resolve(Set<Bean<? extends X>> beans)
     {
-        return injectionResolver.resolve(beans);
+        return injectionResolver.resolve(beans, null);
     }
 
     /**

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=1641694&r1=1641693&r2=1641694&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 Tue Nov 25 20:59:51 2014
@@ -145,7 +145,8 @@ public class InjectionResolver
         }
 
         // not that happy about this check here and at runtime but few TCKs test Weld behavior only...
-        final Bean<?> bean = resolve(implResolveByType(false, type, injectionPoint.getQualifiers().toArray(new Annotation[injectionPoint.getQualifiers().size()])));
+        final Bean<?> bean = resolve(implResolveByType(false, type, injectionPoint.getQualifiers().toArray(new Annotation[injectionPoint.getQualifiers().size()])),
+                                     injectionPoint);
         if (bean != null && ManagedBean.class.isInstance(bean))
         {
             try
@@ -203,7 +204,7 @@ public class InjectionResolver
                 }
             }
 
-            Bean<?> bean = resolve(beanSet);
+            Bean<?> bean = resolve(beanSet, injectionPoint);
 
             if (bean == null)
             {
@@ -267,7 +268,7 @@ public class InjectionResolver
             }
         }
 
-        return resolve(beanSet);
+        return resolve(beanSet, injectionPoint);
     }
 
     private void createNewBean(InjectionPoint injectionPoint, Type type, Annotation[] qualifiers, Set<Bean<?>> beanSet)
@@ -576,8 +577,14 @@ public class InjectionResolver
      * resolve any ambiguity by checking for Alternatives.
      * If any &#064;Alternative exists, then we pick the one with the
      * highest priority.
+     *
+     * @param beans
+     * @param injectionPoint only used for logging. Can be null.
+     * @param <X>
+     * @return the single resolved bean, null if none is activated
+     * @throws javax.enterprise.inject.AmbiguousResolutionException if more than 1 bean is active
      */
-    public <X> Bean<? extends X> resolve(Set<Bean<? extends X>> beans)
+    public <X> Bean<? extends X> resolve(Set<Bean<? extends X>> beans, InjectionPoint injectionPoint)
     {
         if (beans == null || beans.isEmpty())
         {
@@ -599,7 +606,7 @@ public class InjectionResolver
 
         if(set.size() > 1)
         {
-            throwAmbiguousResolutionException(set);
+            throwAmbiguousResolutionException(set, null, injectionPoint);
         }
 
         return (Bean<? extends X>)set.iterator().next();

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/InjectionExceptionUtil.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/InjectionExceptionUtil.java?rev=1641694&r1=1641693&r2=1641694&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/InjectionExceptionUtil.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/InjectionExceptionUtil.java Tue Nov 25 20:59:51 2014
@@ -55,7 +55,7 @@ public class InjectionExceptionUtil
         ViolationMessageBuilder violationMessage =
                 newViolation("Api type [", type.getName(), "] is not found with the qualifiers ");
 
-        violationMessage.addLine(createQualifierMessage(qualifiers));
+        violationMessage.addLine(createQualifierMessage(injectionPoint, qualifiers));
 
         if (injectionPoint != null)
         {
@@ -78,22 +78,21 @@ public class InjectionExceptionUtil
 
     public static void throwAmbiguousResolutionException(Set<Bean<?>> beans, Class type, InjectionPoint injectionPoint, Annotation... qualifiers)
     {
-        String qualifierMessage = createQualifierMessage(qualifiers);
+        String qualifierMessage = createQualifierMessage(injectionPoint, qualifiers);
 
-        ViolationMessageBuilder violationMessage;
-
-        if(type != null)
+        String classString = type != null ? ClassUtil.getClass(type).getName() : null;
+        if (classString == null && injectionPoint != null)
         {
-            violationMessage = newViolation("There is more than one api type with : ",
-                    ClassUtil.getClass(type).getName(), " with qualifiers : ", qualifierMessage);
-            if (injectionPoint != null)
-            {
-                violationMessage.addLine("for injection into ", injectionPoint.toString());
-            }
+            classString = ClassUtil.getClass(injectionPoint.getType()).getName();
         }
-        else
+
+        ViolationMessageBuilder violationMessage = newViolation("There is more than one Bean ",
+                classString != null ? "with type " + classString : ""
+                , qualifierMessage);
+
+        if (injectionPoint != null)
         {
-            violationMessage = newViolation("Ambiguous resolution");
+            violationMessage.addLine("for injection into ", injectionPoint.toString());
         }
 
         throwAmbiguousResolutionExceptionForBeans(beans, violationMessage);
@@ -128,11 +127,18 @@ public class InjectionExceptionUtil
         }
     }
 
-    private static String createQualifierMessage(Annotation... qualifiers)
+    private static String createQualifierMessage(InjectionPoint injectionPoint, Annotation... qualifiers)
     {
         if(qualifiers == null || qualifiers.length == 0)
         {
-            return null;
+            if (injectionPoint != null)
+            {
+                qualifiers = injectionPoint.getQualifiers().toArray(new Annotation[injectionPoint.getQualifiers().size()]);
+            }
+            else
+            {
+                return "@Default";
+            }
         }
 
         //reused source-code