Guice SPI:通过通配符类型查找绑定 [英] Guice SPI: find bindings by wildcard types

查看:133
本文介绍了Guice SPI:通过通配符类型查找绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Guice提供了查找给定类型的所有绑定的方法( Injector#findBindingsByType ),它还提供了 TypeLiteral类,从中可以构建通配符类型。我想要做的是找到所有通过通配符类型参数化的类型的绑定,但我无法弄清楚如何去做。看看guice src表明我可能会吼出错误的树,但我想我会问问无论如何...所以例如给定一个类型

  Foo< E扩展Bar> 
BarImplOne实现Bar
BarImplTwo实现Bar

以及一些绑定,如


  $(new TypeLiteral< Foo< BarImplTwo>>(){})。to(MyFooTwo.class); 

然后我希望能够发现与

类似的绑定

  Injector.findBindingsByType(TypeLiteral.get(Types.newParameterizedType(Foo.class,Types.subtypeOf(Bar.class)))); 

有什么想法?

干杯
Matt

解决方案

不幸的是,没有开箱即用的API会告诉你一个TypeLiteral是否可以从另一个。你需要用一堆可怕的instanceof检查来做一个老派的循环。这样的总体情况:

pre $ for(Map.Entry< Key<>,Binding<>> entry
:injector.getBindings()。entrySet()){
Type type = entry.getKey()。getTypeLiteral()。getType();
if(!(类型instanceof ParameterizedType))继续;

ParameterizedType parameterized =(ParameterizedType)类型;
if(parameterizedType.getRawType()!= Foo.class)continue;

类型参数= .getActualTypeArguments()[0]
if(!(parameterof instanceof Class))continue;

Class<?> parameterClass =(Class<>)参数;
if(!Bar.class.isAssignableFrom(parameterClass))continue;

results.add(entry);
}

当然,使用off-the -shelf API。我希望贡献Guice来实现和测试TypeLiteral.isAssignableFrom(TypeLiteral)。请联系我们的用户列表以进行志愿!


Guice provides a means to find all bindings for a given type (Injector#findBindingsByType) and it also provides a TypeLiteral class from which it seems possible to construct a wildcard type. What I would like to do is find all bindings for some type that is parameterised by a wildcard type but I can't figure out how to do it. A look at the guice src suggests I might be barking up the wrong tree but I figured I'd ask around anyway... so for example given a type

Foo<E extends Bar>
BarImplOne implements Bar
BarImplTwo implements Bar

and some bindings like

bind(new TypeLiteral<Foo<BarImplOne>>() {}).to(MyFooOne.class);
bind(new TypeLiteral<Foo<BarImplTwo>>() {}).to(MyFooTwo.class);

then I want to be able to discover both bindings with something like

Injector.findBindingsByType(TypeLiteral.get(Types.newParameterizedType(Foo.class, Types.subtypeOf(Bar.class))));

Any ideas?

Cheers Matt

解决方案

Unfortunately, there's no out-of-the-box API that will tell you whether one TypeLiteral is assignable from another. You'll need to do an old-school loop with a bunch of hideous instanceof checks. Something gross like this:

for (Map.Entry<Key<?>, Binding<?>> entry 
    : injector.getBindings().entrySet()) {
  Type type = entry.getKey().getTypeLiteral().getType();
  if (!(type instanceof ParameterizedType)) continue;

  ParameterizedType parameterized = (ParameterizedType) type;
  if (parameterizedType.getRawType() != Foo.class) continue;

  Type parameter = .getActualTypeArguments()[0]
  if (!(parameter instanceof Class)) continue;

  Class<?> parameterClass = (Class<?>) parameter;
  if (!Bar.class.isAssignableFrom(parameterClass)) continue;

  results.add(entry);
}

Of course, it would be much nicer to do something using an off-the-shelf API. I'd welcome an contribution to Guice that implements and tests TypeLiteral.isAssignableFrom(TypeLiteral). Contact our user's list to volunteer!

这篇关于Guice SPI:通过通配符类型查找绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆