Domanda

I've written a factory able to create ListEditor of any type:

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:

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:

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

Gin complains with a simple 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<?, ?>>() {}); 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>>() {}); is working but the generic factory loses a lot of its interest.

Do you know a generic way to declare this binding?

Thanks

È stato utile?

Soluzione

I managed to find a solution by getting rid of the ListEditorFactory interface, no more need to declare binding in the gin module.

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top