使用GIN注入ListEditor的Generics工厂

时间:2013-07-19 00:31:52

标签: gwt generics guice gin gwt-editors

我已经写了一个能够创建任何类型的ListEditor的工厂:

public interface ListEditorFactory<T, E extends Editor<T>> {
    ListEditor<T, E> create(InsertPanel panel);
}

及其实施:

public class ListEditorFactoryImpl<T, E extends Editor<T> & IsWidget> implements ListEditorFactory<T, E> {
    private final Provider<E> editorProvider;

    @Inject
    public ListEditorFactoryImpl(Provider<E> editorProvider) {
        this.editorProvider = editorProvider;
    }

    @Override
    public ListEditor<T, E> create(final InsertPanel panel) {
        return ListEditor.of(new EditorSource<E>() {
            @Override
            public E create(int index) {
                final E e = editorProvider.get();
                panel.add(e.asWidget());
                return e;
            }

            @Override
            public void dispose(E e) {
                e.asWidget().removeFromParent();
            }

            @Override
            public void setIndex(E editor, int index) {
                panel.insert(editor.asWidget(), index);
            }
        });
    }
}

如果我直接使用Gin注入的Provider实例化工厂,那么它非常有效:

ListEditor<NoteDTO, CharacterNoteScreen> notesEditor;
@Inject @UiField(provided = true) FlowPanel notePanel;

@Inject
protected void init(Provider<CharacterNoteScreen> characterNoteProvider) {
    final ListEditorFactory<NoteDTO, CharacterNoteScreen> listEditorFactory = new ListEditorFactoryImpl<NoteDTO, CharacterNoteScreen>(characterNoteProvider);
    notesEditor = listEditorFactory.create(notePanel);

但是如果我想像这样注入整个ListEditorFactory:

@Inject
protected void init(ListEditorFactory<NoteDTO, CharacterNoteScreen> listEditorFactory) {
    notesEditor = listEditorFactory.create(notePanel);

Gin抱怨简单的bind(ListEditorFactory.class).to(ListEditorFactoryImpl.class);绑定:

[ERROR] Generator 'com.google.gwt.inject.rebind.GinjectorGenerator' threw an exception while rebinding 'net.test.client.MyGinjector'
com.google.inject.ConfigurationException: Guice configuration errors:
1) javax.inject.Provider<E> cannot be used as a key; It is not fully specified.

我尝试了bind(new TypeLiteral<ListEditorFactory<?, ?>>() {}).to(new TypeLiteral<ListEditorFactoryImpl<?, ?>>() {});,但得到了

java.lang.IllegalArgumentException: Expected a Class, ParameterizedType, or GenericArrayType, but <?> is of type com.google.inject.internal.MoreTypes$WildcardTypeImpl

绑定bind(new TypeLiteral<ListEditorFactory<NoteDTO, CharacterNoteScreen>>() {}).to(new TypeLiteral<ListEditorFactoryImpl<NoteDTO, CharacterNoteScreen>>() {});正在运行,但通用工厂失去了很多兴趣。

您是否知道声明此绑定的通用方法?

由于

1 个答案:

答案 0 :(得分:1)

我设法通过摆脱ListEditorFactory接口找到解决方案,不再需要在gin模块中声明绑定。

我可以直接注入实现而没有任何问题:

@Inject
protected void init(ListEditorFactoryImpl<NoteDTO, CharacterNoteScreen> listEditorFactory) {
    notesEditor = listEditorFactory.create(notePanel);

如果杜松子酒能够注入这个,我认为没有理由它不能通过接口注入它但是错误的绑定声明。

所以,即使我对我的解决方案感到满意,我也要打开这个问题,因为这可能是宣告绑定的一种方式。