I guess a common advice is to stay away from .toInstance().
But assuming you have to do that (maybe it's a test), I would do it this way
to avoid messing with reflection directly:
abstract class ListModule<T> extends AbstractModule {
protected ListModule(List<T> list) {
this.list = list;
}
@Provides List<T> provideList() {
return list;
}
}
new ListModule<String>(new ArrayList<String>()) {};
FeedBurner has a convenience class that can be used as:
new BindingModule<List<String>>(Lists.newArrayList()) {};
On Thu, Jul 9, 2009 at 4:39 PM, Dmitry Skavish <[email protected]> wrote:
> Hi,
> I can't figure out how to bind class parametrized with a given Class?
>
> For example if I have something like this:
>
> Module createModule(Class clazz, List list) {
> return new AbstractModule() {
> protected void configure() {
> bind(??????).toInstance(list);
> }
> };
> }
>
> then after this call: createModule(String.class, new ArrayList())
>
> I have binding from List<String> to my list
>
> This binding should be equivalent to this one: bind(new
> TypeLiteral<List<String>>() {}).toInstance(list);
>
> I tried to the following, but compiler does not like it and I am not really
> sure this is the right way:
>
> bind(Key.get(Types.newParameterizedType(List.class,
> clazz))).toInstance(list);
>
> Thanks!
> --
> Dmitry Skavish
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---