如何解决“... PlaceHistoryMapperWithFactory不能多次实现......”

时间:2012-05-10 13:44:56

标签: java gwt

我刚刚尝试了PlaceHistoryMapperWithFactory作为建议的here。所以我的AppPlaceHistoryMapper看起来像这样:

    @WithTokenizers({ InfoPlace.Tokenizer.class, LogPlace.Tokenizer.class })
public interface AppPlaceHistoryMapper extends PlaceHistoryMapperWithFactory<TokenizerFactory> {

}

我也改变了gin模块:

bind(PlaceHistoryMapperWithFactory.class).to(AppPlaceHistoryMapper.class);

但我不会gwt编译。我得到

[INFO] Compiling module de.stalabw.zensus2011.adb.AuswertungsDB
[INFO]    Scanning for additional dependencies: ...ClientInjectorImpl.java
[INFO]       Adding '57' new generated units
[INFO]          Validating newly compiled units
[INFO]             Ignored 1 unit with compilation errors in first pass.
[INFO] Compile with -strict or with -logLevel set to TRACE or DEBUG to see all errors.
[INFO]    [ERROR] Errors in '...PlaceHistoryMapperWithFactoryImpl.java'
[INFO]       [ERROR] Line 10:  The interface PlaceHistoryMapperWithFactory cannot be implemented more than once with different arguments: PlaceHistory
MapperWithFactory<Void> and PlaceHistoryMapperWithFactory
[INFO]    [ERROR] Cannot proceed due to previous errors

我刚刚在单元测试中修复了同样的错误。我有一个模拟实现。我刚刚改变了

public class PlaceHistoryMapperMock extends AbstractPlaceHistoryMapper<void> implements
AppPlaceHistoryMapper

public class PlaceHistoryMapperMock extends AbstractPlaceHistoryMapper<TokenizerFactory> implements
AppPlaceHistoryMapper

错误消失了。但如何为我的“真实”代码修复它?

2 个答案:

答案 0 :(得分:2)

问题可能是代码中某处有GWT.create(PlaceHistoryMapperWithFactory.class)(可能是由GIN生成的),生成器试图生成一个PlaceHistoryMapperWithFactoryImpl类(该类名称是对症的),它实现了PlaceHistoryMapperWithFactory(传递给GWT.create())和PlaceHistoryMapperWithFactory<Void>(因为生成的类扩展了AbstractPlaceHistoryMapper<Void>)。

这实际上取决于您在代码中声明对PlaceHistoryMapperWithFactory的依赖性。

IMO,您应该依赖代码中的PlaceHistoryMapper,而不是某些PlaceHistoryMapperWithFactory。如果您需要该依赖项(因为那是您调用setFactory的地方),您应该依赖于参数化类型PlaceHistoryMapperWithFactory<TokenizerFactory>,该参数化类型必须使用TypeLiteral在GIN中映射:< / p>

bind(new TypeLiteral<PlaceHistoryMapperWithFactory<TokenizerFactory>>() {}).to(AppPlaceHistoryMapper.class);

答案 1 :(得分:0)

好吧,我明白了。问题是

bind(PlaceHistoryMapperWithFactory.class).to(AppPlaceHistoryMapper.class);

由于没有PlaceHistoryMapperWithFactory.class,这将绑定错误的实现。我的解决方案是提供商:

@Provides
@Singleton
public final PlaceHistoryMapperWithFactory<TokenizerFactory> getPlaceHistoryMapper(AppPlaceHistoryMapper hm) {
    return hm;
}

似乎有用。