Just a try, but did you try putting any @Nullable annotation on the field?

On Jan 12, 2011, at 2:46 AM, Ambience wrote:

> I need to inject a field only if it is available in the current scope,
> and null otherwise. For example:
> 
> public class Thinger implements Provider<SomeSuch> {
>    public @Inject(optional=true) HttpServletRequest request;
> 
>    public SomeSuch get() {
>        return request == null ? new WhosIt() : WhatsIt();
>    }
> }
> However, if HttpServletRequest is bound (which it is) but not in
> scope, I get a ProvisioningException. I have not been able to find an
> elegant way to do this so I am relegated to do something like.
> 
> HttpServletRequest request = null;
> try {
>    request = injector.getInstance(HttpServletRequest.class);
> } catch(ProvisioningException e) {}
> 
> Which just feels all manner of wrong. Is there a proper way to do this?
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "google-guice" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/google-guice?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en.

Reply via email to