Don't forget the cast

bind( (TypeLiteral<List>) typeLiteral ).toInstance(list);

Cheers
Alen

On Jul 10, 2:32 am, Dmitry Skavish <[email protected]> wrote:
> ok, I guess I found the answer, I was very close:
>  TypeLiteral typeLiteral =
> TypeLiteral.get(Types.newParameterizedType(List.class, clazz));
>  bind(typeLiteral).toInstance(list);
>
>
>
> On Thu, Jul 9, 2009 at 7:47 PM, Dmitry Skavish <[email protected]> wrote:
> > Ben,
> > I think my explanation was confusing. Let me try to explain it better. I am
> > getting class names from some configuration files. So basically I have a
> > list of Class objects. I want to create bindings from List<each of thouse
> > classes> to some providers of these lists (or instances, it does not really
> > matter).
>
> > For example if I have configuration with class names:
> > java.lang.Integer, java.lang.String, java.lang.Long then the bindings should
> > be equivalent to the following:
>
> >   bind(new TypyLiteral<List<Integer>>() {}).to(provider of List<Integer>)
> >   bind(new TypyLiteral<List<String>>() {}).to(provider of List<String>)
> >   bind(new TypyLiteral<List<Long>>() {}).to(provider of List<Long>)
>
> > so that if I have a class:
>
> > class A {
> >   @Inject
> >   A(List<String> strings, List<Integer> ints) {}
> > }
>
> > then the right list is injected into the constructor.
>
> > the problem is that those configurations are known at runtime and I have to
> > construct the bindings at runtime as well. I don't know the types at compile
> > times and I cannot create those bindings statically in the code.
>
> > Any ideas how to do that?
>
> > Thanks!
>
> > On Thu, Jul 9, 2009 at 7:35 PM, Ben <[email protected]> wrote:
>
> >> On Thu, Jul 9, 2009 at 6:32 PM, Ben <[email protected]> wrote:
>
> >>> 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()) {};
>
> >> Oops. I thought this is another list. Please ignore this part, it's
> >> irrelevant.
>
> >>> 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
>
> > --
> > Dmitry Skavish
>
> --
> 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
-~----------~----~----~----~------~----~------~--~---

Reply via email to