question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Guice Assisted Inject with multiple implementations of interface

See original GitHub issue

From nishantmishra78 on June 04, 2014 10:44:53

I have an interface A with multiple implementations say B, C and D. I am using guice assisted inject to create these classes. Classes B and C use the same assister parameters(say b, c) in constructor while D has one different parameter(say d). The way I implementing this is have a factory AFactory with 2 methods: create(int b, int c) and createD(int d) In my module, I have created a PrivateModule to bind the concrete class to factory.

The code looks like this:

public static PrivateModule getAFactoryModule(final String factoryBindingKey,final Class<? extends A> targetClassToCreate) {     return new PrivateModule() {

        @Override         public void configure() {             install(new FactoryModuleBuilder().implement(                     A.class, targetClassToCreate).build(                     AFactory.class));

            bind(AFactory.class).annotatedWith(Names.named(factoryBindingKey)).to(                     Key.get(AFactory.class));             expose(Key.get(AFactory.class, Names.named(factoryBindingKey)));         }     }; } I call PrivateModule like this:

install(getAFactoryModule(“B”, B.class)); install(getAFactoryModule(“C”, C.class)); install(getAFactoryModule(“D”, D.class)); But, this gives an error saying:

com.guice.test.B has @ AssistedInject constructors but none of them match the parameters in method com.guice.test.AFactory.createD(). Unable to create assisted inject

com.guice.test.C has @ AssistedInject constructors but none of them match the parameters in method com.guice.test.AFactory.createD(). Unable to create assisted inject

com.guice.test.D has @ AssistedInject constructors but none of them match the parameters in method com.guice.test.AFactory.create(). Unable to create assisted inject It seems Guice is trying to use different create methods than what is expected. Any idea how this can be resolved? Any pointers would be appreciated!

Thanks!

_Original issue: http://code.google.com/p/google-guice/issues/detail?id=809_

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:8

github_iconTop GitHub Comments

2reactions
gissuebotcommented, Jul 7, 2014

From sberlin on June 04, 2014 07:58:52

When implementing the factory, assistedinject has to be able to match every method it sees.  You’re installing the factory 4 times, and in each it’s saying it doesn’t know how to implement one of the methods in the factory.

Do you actually need B & C to be using the exact same method?  The easiest way to do this is to do something like:   interface Factory {     @Named(“A”) Foo createA();     @Named(“B”) Foo createB(int a, int b);     @Named(“C”) Foo createC(int a, int b);     @Named(“D”) Foo createD(int a);   }

and then install it using:

 install(new FactoryModuleBuilder()     .implement(Keys.get(Foo.class, Names.named(“A”)), A.class)     .implement(Keys.get(Foo.class, Names.named(“B”)), B.class)     .implement(Keys.get(Foo.class, Names.named(“C”)), C.class)     .implement(Keys.get(Foo.class, Names.named(“D”)), D.class)     .build(AFactory.class));

… and not mess with PrivateModules at all.  Doing it this way will associate each ‘create’ method with a different implementation.

0reactions
gissuebotcommented, Jul 7, 2014

From nishantmishra78 on June 05, 2014 06:05:12

Ok - thanks for your response.

I would prefer to go with approach 1, the problem I see with second approach is that it requires to unnecessarily create D along with B and C and this would grow with new implementations. Also, in case I need another implementation of A which requires different parameters than B/C or D then this approach will get messy.

If there are no better ways, I will go with option 1.

Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use Guice AssistedInject with multiple implementations ...
The only solution I've currently found does not use Guice: I have my own factory that resolves the class constructor based on the...
Read more >
Disambiguating Between Instances With Google Guice - DZone
Google guice provides a neat way to select a target implementation if there are multiple implementations of an interface.
Read more >
Guice Assisted Inject with multiple implementations of interface
1) No implementation for com.guice.test.AFactory was bound. 2) com.guice.test.A is an interface, not a concrete class. Unable to create ...
Read more >
Assisted Injection - Dagger
Dagger is a fully static, compile-time dependency injection framework for both Java and Android. It is developed by the Java Core Libraries Team...
Read more >
AssistedInject, an easier way to help the Guice Injector build ...
limpb...@gmail.com · 1. Annotate the constructor and assisted parameters on the implementation class (such as RealPayment.java) · 2. Create a Factory interface ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found