[ 
https://issues.apache.org/jira/browse/WICKET-2241?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12702974#action_12702974
 ] 

Eduardo S. Nunes edited comment on WICKET-2241 at 4/26/09 8:30 PM:
-------------------------------------------------------------------

I think there is something wrong with this patch:
                // if the Inject annotation is marked optional and no binding 
is found
                // then skip this injection (WICKET-2241)
                if (optional && injector.getBinding(key) == null)
                {
                        return null;
                }

But here (latest version of guice) injector.getBinding(..) throws an exception 
instead of return null. Consider do like Jesse Wilson said.

A solution that works for guice 1.0 and 2.0 is:
                if (optional)
                {
                        try
                        {
                                if (injector.getBinding(key) == null)
                                {
                                        return null;
                                }
                        }
                        catch (RuntimeException e)
                        {
                                return null;
                        }
                }



      was (Author: esnunes):
    I think there is something wrong with this patch:
                // if the Inject annotation is marked optional and no binding 
is found
                // then skip this injection (WICKET-2241)
                if (optional && injector.getBinding(key) == null)
                {
                        return null;
                }

But here (latest version of guice) injector.getBinding(..) throws an exception 
instead of return null. Consider do like Jesse Wilson said.
  
> Guice integration doesn't honour optional bindings
> --------------------------------------------------
>
>                 Key: WICKET-2241
>                 URL: https://issues.apache.org/jira/browse/WICKET-2241
>             Project: Wicket
>          Issue Type: Bug
>          Components: wicket-guice
>            Reporter: Jesse Wilson
>            Assignee: Igor Vaynberg
>            Priority: Minor
>             Fix For: 1.4-RC3
>
>         Attachments: 
> 0001-WICKET-2241-Support-for-Guice-s-Inject-optional.patch
>
>
> When @Inject(optional=true) is applied and the binding isn't available, 
> wicket should skip the injection silently.
> http://groups.google.com/group/google-guice/browse_thread/thread/2abe9b55ee26a198
> To check whether the binding is available, you need to use a try/catch block:
>   try {
>     Binding binding = injector.getBinding(key);
>     // use the binding
>   } catch (ConfigurationException e) {
>     // the binding isn't available
>   }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to