You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@openwebbeans.apache.org by David Jencks <da...@yahoo.com> on 2010/12/27 07:07:13 UTC

Is there a reason to produce non-working proxies from a ResourceBean?

Currently ResourceBean will happily produce proxies that can't possibly work if the ResourceInjectionService returns null when asked for the resource.  I think this is really confusing for users.  It certainly wasted several hours of my time.  I would prefer that the ResourceBean throw an NPE if the resource can't be found clearly indicating that there's a problem before the unfortunate user tries to use the proxy.

Is there a reason for the current behavior or would this change be fine?

thanks
david jencks


proposed patch:

Index: webbeans-impl/src/main/java/org/apache/webbeans/component/ResourceBean.java
===================================================================
--- webbeans-impl/src/main/java/org/apache/webbeans/component/ResourceBean.java (revision 1051320)
+++ webbeans-impl/src/main/java/org/apache/webbeans/component/ResourceBean.java (working copy)
@@ -53,8 +53,13 @@
             ResourceInjectionService resourceService = ServiceLoader.getService(ResourceInjectionService.class);
             X instance = resourceService.getResourceReference(this.resourceReference);
 
-            if (instance != null && Modifier.isFinal(instance.getClass().getModifiers()))
+            if (instance == null)
             {
+                throw new NullPointerException("No instance: " + this);
+            }
+
+            if (Modifier.isFinal(instance.getClass().getModifiers()))
+            {
                 return instance;
             }

Re: Is there a reason to produce non-working proxies from a ResourceBean?

Posted by Mark Struberg <st...@yahoo.de>.
Hi David!

Since ResourceBeans are defined as implicitly being @Dependent there is also no 'late resolution' taking place. So it seems ok to throw an exception or better: to inject null if the resource cannot be found.

So 

if (resource.found and ok)
  inject resource proxy
else
  inject null

This might be needed if we have to deal with optional resources which get null-checked in the application.

LieGrue,
strub


--- On Mon, 12/27/10, David Jencks <da...@yahoo.com> wrote:

> From: David Jencks <da...@yahoo.com>
> Subject: Is there a reason to produce non-working proxies from a ResourceBean?
> To: dev@openwebbeans.apache.org
> Date: Monday, December 27, 2010, 6:07 AM
> Currently ResourceBean will happily
> produce proxies that can't possibly work if the
> ResourceInjectionService returns null when asked for the
> resource.  I think this is really confusing for
> users.  It certainly wasted several hours of my
> time.  I would prefer that the ResourceBean throw an
> NPE if the resource can't be found clearly indicating that
> there's a problem before the unfortunate user tries to use
> the proxy.
> 
> Is there a reason for the current behavior or would this
> change be fine?
> 
> thanks
> david jencks
> 
> 
> proposed patch:
> 
> Index:
> webbeans-impl/src/main/java/org/apache/webbeans/component/ResourceBean.java
> ===================================================================
> ---
> webbeans-impl/src/main/java/org/apache/webbeans/component/ResourceBean.java
> (revision 1051320)
> +++
> webbeans-impl/src/main/java/org/apache/webbeans/component/ResourceBean.java
> (working copy)
> @@ -53,8 +53,13 @@
>          
>    ResourceInjectionService resourceService =
> ServiceLoader.getService(ResourceInjectionService.class);
>              X
> instance =
> resourceService.getResourceReference(this.resourceReference);
>  
> -            if (instance !=
> null &&
> Modifier.isFinal(instance.getClass().getModifiers()))
> +            if (instance ==
> null)
>              {
> +               
> throw new NullPointerException("No instance: " + this);
> +            }
> +
> +            if
> (Modifier.isFinal(instance.getClass().getModifiers()))
> +            {
>              
>    return instance;
>          
>    }