2013-10-25 22 views
2

什么即时试图做的是创建地图,其中关键是类扩展我的抽象类如何在GinMapBinder中正确使用TypeLiteral?

GinMapBinder<Class<? extends Key>, Value> mapBinder = GinMapBinder 
       .newMapBinder(binder(), 
         new TypeLiteral<Class<? extends Key>>() { 
         }, new TypeLiteral<Value>() { 
         }); 

,但是当我试图填充我的地图

mapBinder.addBinding(KeyImpl.class).to(Value.class); 

我收到提示:

Error injecting @com.google.gwt.inject.client.multibindings.Internal() java.lang.Class<? extends my.test.gwt.gin.objects.Key>: Unable to create or inherit binding: No implementation bound for '@com.google.gwt.inject.client.multibindings.Internal() java.lang.Class<? extendsmy.test.gwt.gin.objects.Key>' and an implicit binding cannot be created because the type is annotated. 
    Path to required node: 

@com.google.gwt.inject.client.multibindings.Internal com.google.gwt.inject.client.multibindings.MapEntry<java.lang.Class<? extends my.test.gwt.gin.objects.Key>, my.test.gwt.gin.objects.Value> [com.google.gwt.inject.client.multibindings.BindingRecorder.bind(BindingRecorder.java:42)] 
    -> com.google.gwt.inject.client.multibindings.MapEntry<java.lang.Class<? extends my.test.gwt.gin.objects.Key>, my.test.gwt.gin.objects.Value> [com.google.gwt.inject.client.multibindings.BindingRecorder.bind(BindingRecorder.java:42)] 
    -> @com.google.gwt.inject.client.multibindings.Internal() java.lang.Class<? extends my.test.gwt.gin.objects.Key> [@Inject constructor of com.google.gwt.inject.client.multibindings.MapEntry<java.lang.Class<? extends my.test.gwt.gin.objects.Key>, my.test.gwt.gin.objects.Value>] 

如果我不会用TypeLiteral这将工作,但我不想与原始类型的类。 所以如果有人能帮我解决这个问题,我会很高兴。 在此先感谢

+0

好奇你是如何获得新的TypeLiteral映射上班。我使用guice 3和杜松子酒2.1.1。 TypeLiteral具有受保护的构造函数。我无法弄清楚如何使用泛型类创建类型文字,就像您所做的那样。 –

+0

@ChrisHinshaw如果您将'TypeLiteral'初始化为匿名类,那么您可以访问它的构造函数 – user902383

回答

2

问题解决了

我创建提供商为每个键

public class KeyImplProvider implements 
     Provider<Class<KeyImpl>> { 

    @Override 
    public Class<KeyImpl> get() { 
     return KeyImpl.class; 
    } 
} 

和我加入的元素通过

mapBinder.addBinding(KeyImplProvider.class).to(Value.class);