google-code-export / google-guice

Automatically exported from code.google.com/p/google-guice
Apache License 2.0
2 stars 1 forks source link

Allow getInstance/getProvider by TypeLiteral #300

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Currently you can

Binder.bind(Key<T>)
Binder.bind(Class<T>)
Binder.bind(TypeLiteral<T>) 

However you can only 

getInstance/Provider(Key<T>)
getInstance/Provider(Class<T>)

Seems natural to add 

T Injector.getInstance(TypeLiteral<T> type)
Provider<T> Injector.getProvider(TypeLiteral<T> type)

I have done so in this 10-liner patch I'm attaching and everything seems to
work fine. I'm also attaching a small demo piece of code that shows how
these methods save me the typing of an additional Key.get() when getting
stuff out of an Injector :)

Original issue reported on code.google.com by Rinsvind@gmail.com on 2 Jan 2009 at 6:02

Attachments:

GoogleCodeExporter commented 9 years ago
do you have any workaround for 2.0 in the mean time?

Original comment by fernc...@gmail.com on 18 Mar 2009 at 1:24

GoogleCodeExporter commented 9 years ago
No. I think this is a convenience/cosmetic (as opposed to functional) issue so 
no
real work around is needed. I just use

Injector.getInstance(Key.get(<type literal>)); 

rather than directly (as I wish I could)

Injector.getInstance(<type literal>);

I suppose you can try adding somewhere a convenience method:

public static <T> T getInstance(Injector injector, TypeListeral<T> literal) {
  return injector.get(Key.get(literal));
}

My patch simply adds this directly to the injector anyway.

Original comment by Rinsvind@gmail.com on 18 Mar 2009 at 1:42

GoogleCodeExporter commented 9 years ago
the above may be good fodder for the FAQ.  Thanks!  works fine.

Original comment by fernc...@gmail.com on 18 Mar 2009 at 2:04

GoogleCodeExporter commented 9 years ago

Original comment by limpbizkit on 26 Apr 2009 at 9:44

GoogleCodeExporter commented 9 years ago

Original comment by sberlin on 19 Feb 2011 at 10:13