I need to enter a field only if it is available in the current area, and null otherwise. For instance:
public class Thinger implements Provider<SomeSuch> { public @Inject(optional=true) HttpServletRequest request; public SomeSuch get() { return request == null ? new WhosIt() : WhatsIt(); } }
However, if the HttpServletRequest is attached (this is it), but not in scope, I get a ProvisioningException. I was able to find an elegant way to do this, so I stepped back to do something like.
HttpServletRequest request = null; try { request = injector.getInstance(HttpServletRequest.class); } catch(ProvisioningException e) {}
Which just feels all kinds of mistakes. Is there any way to do this?
source share