Guice implicitly assigns a value to AssistedInject variable

124 views
Skip to first unread message

Yifat Levy

unread,
Dec 16, 2019, 10:21:52 AM12/16/19
to google-guice
when I run the code below, I get the output "Bar got 1234".
it looks like Guice can't find a binding for num2, and implicitly assigns the value of num1.
Is this part of the AssistedInject feature? I couldn't find any mention of this in the wiki.
changing num2's type to float throws this exception (as I'd expected):
"No implementation for java.lang.Float annotated with @com.google.inject.assistedinject.Assisted(value=) was bound."


class Foo {
@Inject
public Foo(@Assisted final int num1, final Bar bar) { } interface FooFactory {
Foo create(final int num1);
}
}class Bar {
@Inject
public Bar(@Assisted final int num2) {
System.out.println("Bar got "+num2);
}
}class BillingModule extends AbstractModule {
@Override
protected void configure() {
install(new FactoryModuleBuilder()
.implement(Foo.class, Foo.class)
.build(Foo.FooFactory.class));
}
}public class App
{
public static void main( String[] args ) {
Injector injector = Guice.createInjector(new BillingModule());
Foo.FooFactory fooFactory = injector.getInstance(Foo.FooFactory.class);
fooFactory.create(1234);
}
}
Reply all
Reply to author
Forward
0 new messages