java - How to bind to CheckedProvider in guice? -


guice provides way bind provider:

    bind(a.class).toprovider(aprovider.class); 

although if provider needs throw exception seems checkedprovider right base interface:

    public interface configcheckedprovider<t> extends checkedprovider<t> {         t get() throws configexception;     }      public aprovider implements configcheckedprovider<a> { ... } 

but of classes need instance of injected. , can't change. looks toprovider method doesn't accept checkedprovider types.

how can use providers based on checkedprovider inject instances not providers?

as requested, i'm posting comment answer.


if have class t , checked provider tprovider extends checkedprovider<t>, cannot inject t:

@inject someclass(t t) {  // won't work     ... } 

as able if had used plain provider<t>. done intentionally. checked providers needed when creation of object may fail particular type of exception, , failure must handled user code. plain providers not have such feature.

provider<t> not allow throwing checked exceptions get() method, , unchecked exceptions throws may wrapped provisionexception, cannot reliably catch exception. also, if inject t directly , provider's get() method fails, error during injection, may lead incomprehensible stacktraces (especially if not using injectors directly) or absence.

checked providers allow throw predeclared types of exceptions providers, , these exceptions guaranteed passed code calls get() on checked provider. way can reliably catch them, in return lose ability inject objects created provider directly.

see this manual on throwing , checked providers more background on have written.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

c++ - Clear the memory after returning a vector in a function -

erlang - Saving a digraph to mnesia is hindered because of its side-effects -