frjaeger220 / google-guice

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

Map binder doesn't support parameterised types #316

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
It is not possible using the map binder to inject a Map<String, 
List<String>>.
I am not sure if what I am asking for is impossible but it seems as though 
the multibinding API needs to be extended to use TypeLiteral.
Here is a simple testcase
class Foo  {
  @Inject public Map<String, List<String>> m_map;
}

public class testMe
{
  public static void main(String[] args)  {
    Injector injector = Guice.createInjector(new AbstractModule() {
      @Override
      protected void configure() {
        MapBinder<String, List<String>> mapBinder = 
MapBinder.newMapBinder(binder(), String.class, List.class);
        mapBinder.addBinding("1").toInstance(Arrays.asList("one"));
      }});
    Foo foo = injector.getInstance(Foo.class);
    Assert.assertEquals(foo.m_map.get("1"), "one");
  }
}

Original issue reported on code.google.com by NikolayM...@gmail.com on 24 Jan 2009 at 10:21

GoogleCodeExporter commented 9 years ago
Your test case should assert against a list of strings ["one"] rather than just 
the string "one".

Original comment by dha...@gmail.com on 25 Jan 2009 at 1:25

GoogleCodeExporter commented 9 years ago
Yep you are right. I have also found a workaround for this problem:
you can use MapBinder.newMapBinder(binder(), String.class, new 
TypeLiteral<List<String>>(){}.getType())
However I would have expected it to work with just a TypeLiteral.

Original comment by NikolayM...@gmail.com on 25 Jan 2009 at 2:16

GoogleCodeExporter commented 9 years ago
This is fixed in SVN.

Original comment by limpbizkit on 26 Jan 2009 at 1:17