Igor
Igor

Reputation: 866

Guice Bind Generic Types

is there a way to bind generic types of the next sort:

public interface A<T extends Number> {
  void print(T t);
}

public class B implements A<Integer> {
  @Override
  public void print(Integer i) {
    System.out.println(i);
  }
}

public class C implements A<Double> {
  @Override
  public void print(Double d) {
    System.out.println(d);
  }
}

how can I exactly bind the above interface and its implementations (using TypeLiteral?) so that I could create a typed instance by some condition?

class Main {
  public static void main(String[] args) {
    A a1 = Guice.createInjector(new MyModule()).getInstance( ??? );
    a1.print(1); //will print 1

    A a2 = Guice.createInjector(new MyModule()).getInstance( ??? );
    a2.print(1.0); //will print 1.0
  }

}

how does MyModule suppose to look like?

Upvotes: 3

Views: 2862

Answers (2)

Olivier Gr&#233;goire
Olivier Gr&#233;goire

Reputation: 35407

You can create provider methods, without having to use TypeLiteral<A<Integer>> in the definition.

class MyModule extends AbstractModule {
  @Provides
  @Singleton
  A<Integer> provideAOfInteger() {
    return new B();
  }
}

And you can also use an implicit binding to access your A<Integer>:

class AOfIntegerHolder {
  @Inject A<Integer> aOfInteger;
}

Injector injector = Guice.createInjector(new MyModule());
A<Integer> a1 = injector.getInstance(AOfIntegerHolder.class).aOfInteger;

The beauty of Guice is that there are more than often several ways to do what you want, and none is better than the other: they're just different ;)

Upvotes: 3

Ryan Leach
Ryan Leach

Reputation: 4470

What you are missing is,

Injector  injector = Guice.createInjector(new MyModule());
A a1 = injector.getInstance(Key.get(new TypeLiteral<A<Integer>>() {}));

And your MyModule

public static class MyModule extends AbstractModule {
    @Override
    protected void configure() {
        bind(new TypeLiteral<A<Integer>>() {}).toInstance(new B());
    }
}

Upvotes: 3

Related Questions