Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I have an error when I try to override a generic binding with Guice? (TypeLiteral)

I'd like to override a generic type binding, but I allways got the same "No implementation was bound" error.

I'm using roboguice 3.

Here is a exemple of code that I use:

public interface IParser<I, O> {}

public class Parser1 implements IParser<String, String> {
    IParser<String, String> mParser;

    @Inject
    public Parser1(IParser<String, String> parser) {
        mParser = parser;
    }
}

public class Parser2 extends Parser1 {
    @Inject
    public Parser2(IParser<String, String> parser) {
        super(parser);
    }
}

public class MyModule extends AbstractModule {
    @Override
    protected void configure() {
        bind(new TypeLiteral<IParser<String, String>>() {}).to(new TypeLiteral<Parser1>() {});
    }
}

And here is my injector creation :

RoboGuice.getOrCreateBaseApplicationInjector(this,
                RoboGuice.DEFAULT_STAGE,
                RoboGuice.newDefaultRoboModule(this),
                Modules.override(new MyModule()).with(new AbstractModule() {
                    @Override
                    protected void configure() {
                        bind(new TypeLiteral<IParser<String, String>>() {}).to(new TypeLiteral<Parser2>() {});
                    }
                })
);

If I don't try to override it (only user Parser1), all is fine, when I override standard object with providers, it works well too, but not with TypeLiteral.

My error is :

com.google.inject.CreationException: Unable to create injector, see the following errors:
1) No implementation for IParser<String, String> was bound.

What am I doing wrong?

Thanks.

like image 760
Aurélien Lemaitre Avatar asked Dec 09 '15 10:12

Aurélien Lemaitre


2 Answers

You should change definition of classes which are implemented your interface.

Try this:

public class Parser1<I, O> implements IParser<I, O> {
}
public class Parser2<I, O> extends Parser1<I, O> {
}

And then you can bind your interface to class by this way:

bind(new TypeLiteral<IParser<String, String>>() {}).to(new TypeLiteral<Parser1<String, String>() {});
like image 93
eg04lt3r Avatar answered Oct 31 '22 10:10

eg04lt3r


I'm not shure, but it looks remarkable to me that a binding to a conrete instance is used instead to a Class:

bind(new TypeLiteral>() {}).to(new TypeLiteral() {});

did you try

bind(new TypeLiteral<IParser<String, String>>() {}).to(Parser2.class});

?

like image 45
treeno Avatar answered Oct 31 '22 09:10

treeno