简体   繁体   English

使用GIN注入ListEditor的泛型工厂

[英]Use GIN to inject a Generics factory of ListEditor

I've written a factory able to create ListEditor of any type: 我写了一个工厂,可以创建任何类型的ListEditor:

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

And its implementation: 及其实现:

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);
            }
        });
    }
}

It works very well if I instantiate directly the factory with a Provider injected by Gin: 如果我直接用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);

But if I want to inject the whole ListEditorFactory like this: 但是,如果我想像这样注入整个ListEditorFactory:

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

Gin complains with a simple bind(ListEditorFactory.class).to(ListEditorFactoryImpl.class); 杜松子酒抱怨一个简单的bind(ListEditorFactory.class).to(ListEditorFactoryImpl.class); binding: 捆绑:

[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.

I tried with bind(new TypeLiteral<ListEditorFactory<?, ?>>() {}).to(new TypeLiteral<ListEditorFactoryImpl<?, ?>>() {}); 我尝试使用bind(new TypeLiteral<ListEditorFactory<?, ?>>() {}).to(new TypeLiteral<ListEditorFactoryImpl<?, ?>>() {}); but got an 但是得到了

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

The binding bind(new TypeLiteral<ListEditorFactory<NoteDTO, CharacterNoteScreen>>() {}).to(new TypeLiteral<ListEditorFactoryImpl<NoteDTO, CharacterNoteScreen>>() {}); 绑定bind(new TypeLiteral<ListEditorFactory<NoteDTO, CharacterNoteScreen>>() {}).to(new TypeLiteral<ListEditorFactoryImpl<NoteDTO, CharacterNoteScreen>>() {}); is working but the generic factory loses a lot of its interest. 正在运作,但通用工厂失去了很多兴趣。

Do you know a generic way to declare this binding? 您知道声明此绑定的通用方法吗?

Thanks 谢谢

I managed to find a solution by getting rid of the ListEditorFactory interface, no more need to declare binding in the gin module. 我设法摆脱了ListEditorFactory接口,找到了一个解决方案,不再需要在gin模块中声明绑定。

I can directly inject the implementation without any problem: 我可以直接注入实现而没有任何问题:

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

If gin is able to inject this, I see no reason it can't inject it trough an interface but wrong binding declaration. 如果杜松子酒能够注入这个,我没有理由不能通过接口注入它,但是绑定声明错误。

So even if I'm happy with my solution I let the question open because it might be a way to declare the binding. 因此,即使我对自己的解决方案感到满意,我也会打开问题,因为这可能是声明绑定的一种方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM