xiaodududu / google-guice

Automatically exported from code.google.com/p/google-guice
Apache License 2.0
0 stars 0 forks source link

Assisted inject doesn't work with boolean #811

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Description of the issue:
Assisted inject doesn't work with boolean, version 3.0

Steps to reproduce:
1. Code for Factory interface:
public interface RedirectorFactory {
    public Redirector  build(
            @Assisted("endpoints") EndpointsConfig endpoints,
        @Assisted("redirectUrl") String redirectUrl,
        @Assisted("redirectQuery") String redirectQuery,
        @Assisted("mobile") boolean mobile);
}
2. Constructor for Object:
    @Inject
    public Redirector(
            EndpointsConfig endpoints, String redirectUrl,
            String redirectQuery,
            boolean mobile) {...}
3. Code that executes:
        Redirector redirector = redirectorFactory.build(endpoints, pureRedirectUrl, pureRedirectQuery, pureMobile);
4. Getting:
1) Could not find a suitable constructor in java.lang.Boolean. Classes must 
have either one (and only one) constructor annotated with @Inject or a 
zero-argument constructor that is not private.
  at java.lang.Boolean.class(Boolean.java:43)
  while locating java.lang.Boolean
    for parameter 3 at com.nytimes.lire.service.Redirector.<init>(Redirector.java:65)
  at com.nytimes.lire.service.RedirectorFactory.build(RedirectorFactory.java:1)
  at com.google.inject.assistedinject.FactoryProvider2.initialize(FactoryProvider2.java:539)
  at com.google.inject.assistedinject.FactoryModuleBuilder$1.configure(FactoryModuleBuilder.java:335)

1 error
    at com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:435)
    at com.google.inject.internal.InternalInjectorCreator.injectDynamically(InternalInjectorCreator.java:175)
    at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:109)
    at com.google.inject.Guice.createInjector(Guice.java:95)
    at com.google.inject.Guice.createInjector(Guice.java:72)
    at com.google.inject.Guice.createInjector(Guice.java:62)

Original issue reported on code.google.com by the...@gmail.com on 24 Jun 2014 at 11:27

GoogleCodeExporter commented 9 years ago
The @Inject'd class doesn't have the assisted parameters annotated with 
@Assisted.  The injection is failing because there's no boolean injection point 
in your main injector, and assistedinject doesn't know that it needs to assist 
it.

Original comment by sberlin on 24 Jun 2014 at 1:57

GoogleCodeExporter commented 9 years ago
It doesn't work without @Assisted("mobile") too.

Original comment by the...@gmail.com on 24 Jun 2014 at 2:02

GoogleCodeExporter commented 9 years ago
Please post the newer stack trace & a minimal test-case that produces the 
failure.

I'm 100% certain that it has nothing to do with "boolean" not working with 
assistedinject, as we have a ton of code that successfully does that.

Keep in mind that if you intended to assist the other parameters, you should 
mark those as @Assisted too, otherwise Guice will inject them from the main 
injector, not from your factory-supplied arguments.

Original comment by sberlin on 24 Jun 2014 at 2:07

GoogleCodeExporter commented 9 years ago
Solution:
Looks like I've understand your point. Posting here a solution in case of 
somebody else is as dumb as me. 

Factory:

public interface RedirectorFactory {
    public Redirector  build(
             EndpointsConfig endpoints,
            @Assisted("redirectUrl") String redirectUrl,
            @Assisted("redirectQuery") String redirectQuery,
            boolean mobile);
}
Constructor:
    @Inject
    public Redirector(
            @Assisted EndpointsConfig endpoints,
            @Assisted("redirectUrl") String redirectUrl,
            @Assisted("redirectQuery") String redirectQuery,
            @Assisted boolean mobile) {

Original comment by the...@gmail.com on 24 Jun 2014 at 2:13