Reconstructing generic types at runtime with Guice via Types and TypeLiterals

十年热恋 提交于 2019-12-01 00:07:22

From the JavaDoc for Binder:

Guice cannot currently bind or inject a generic type, such as Set<E> all type parameters must be fully specified.

You can create bindings for Foo when K and V are bound. If you need to make bindings for Foo for more than one type of key, you can make a method that makes it easier to do these bindings. One way to do that is to create a method like this in your module:

<K, V extends Bar<K>> AnnotatedBindingBuilder<Foo<V, K>> bind(Class<K> keyType,
    Class<V> barType) {
  ParameterizedType bType = Types.newParameterizedType(Bar.class, keyType);
  ParameterizedType fType = Types.newParameterizedType(Foo.class, barType,
      keyType);

  @SuppressWarnings("unchecked")
  TypeLiteral<Foo<V, K>> typeLiteral =
      (TypeLiteral<Foo<V, K>>) TypeLiteral.get(fType);

  return bind(typeLiteral);
}

Then if you have these classes:

class StringValue implements Bar<String> {
  ...
}

class StringValueProcessor implements Foo<StringValue, String> {
  ...
}

You can create a binding like this:

bind(String.class, StringValue.class).to(StringValueProcessor.class);

...so that Guice could inject into a class like this:

static class Target {
  private final Foo<StringValue, String> foo;

  @Inject
  public Target(Foo<StringValue, String> foo) {
    this.foo = foo;
  }
}

Guice's factory cannot build TypeVariable instances. You'll need to implement this interface directly as you need it.

Note that Guice doesn't allow bindings for types that aren't fully-qualified. For example, you can bind a Map<String, Integer> but you can't bind a Map<K, V>.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!