1

I'm new at Guice, so I'm trying to understand AssistedInject. I have very simple project:

Class that I want to Inject:

public class I1 {
}

Class with assisted injection:

public interface ICla {
}

public class Cla implements ICla{
    public Integer t;
    public I1 i;

    @Inject
    public Cla(Integer t, @Assisted I1 i) {
        this.t = t;
        this.i = i;

    }
}

Factory

public interface IClaFactory {
    Cla create(Integer t);
}

And the Main class:

public class Main {
    public static void main(String[] args) {
        Injector injector = Guice.createInjector(new Module());

        IClaFactory factory = injector.getInstance(IClaFactory.class);
    }

    private static class Module extends AbstractModule {
        protected void configure() {
            install(new FactoryModuleBuilder()
                .implement(ICla.class, Cla.class).build(IClaFactory.class));
        }
    }
}

But it still doesn't work and I don't understand, where I'm wrong?

Exception in thread "main" com.google.inject.CreationException: Unable to create injector, see the following errors:

1) No implementation for ru.test.factory.I1 annotated with @com.google.inject.assistedinject.Assisted(value=) was bound.
  while locating ru.test.factory.I1 annotated with @com.google.inject.assistedinject.Assisted(value=)
    for parameter 1 at ru.test.factory.Cla.<init>(Cla.java:11)
  at ru.test.factory.IClaFactory.create(IClaFactory.java:1)
  at com.google.inject.assistedinject.FactoryProvider2.initialize(FactoryProvider2.java:660)
  at com.google.inject.assistedinject.FactoryModuleBuilder$1.configure(FactoryModuleBuilder.java:335) (via modules: ru.test.Main$Module -> com.google.inject.assistedinject.FactoryModuleBuilder$1)

2) Could not find a suitable constructor in java.lang.Integer. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.
  at java.lang.Integer.class(Integer.java:52)
  while locating java.lang.Integer
    for parameter 0 at ru.test.factory.Cla.<init>(Cla.java:11)
  at ru.test.factory.IClaFactory.create(IClaFactory.java:1)
  at com.google.inject.assistedinject.FactoryProvider2.initialize(FactoryProvider2.java:660)
  at com.google.inject.assistedinject.FactoryModuleBuilder$1.configure(FactoryModuleBuilder.java:335) (via modules: ru.test.Main$Module -> com.google.inject.assistedinject.FactoryModuleBuilder$1)
Andrei Koch
  • 898
  • 1
  • 7
  • 23

1 Answers1

2

You're @Assisted-ing the wrong parameter: the parameter to "assist" with is the parameter that is defined in the factory interface. In this case, it's your Integer, not your I1.

This will work:

@Inject
public Cla(I1 i, @Assisted Integer t) {
    this.t = t;
    this.i = i;

}
Olivier Grégoire
  • 33,839
  • 23
  • 96
  • 137
  • It solved my problem. I thought that Guice @Assisted for me, but in fact, It's I guice needed my assist :) – Andrei Koch Nov 24 '17 at 08:51
  • @Andrew I'm glad it sold your issue. Since it did, would you mind marking this answer as accepted? It's clicking on the check mark next below the points of the answer. Thanks ;-) – Olivier Grégoire Nov 24 '17 at 11:21