Had to use @AssistedInject on a constructor (and add one @Inject 'unnecessary' constructor)

141 views
Skip to first unread message

dindeman

unread,
Jul 2, 2011, 12:58:30 AM7/2/11
to GWTP
Hi everyone.

I have a PresenterWidget whose constructor takes one @Assisted
argument:

@Inject
public MyPresenter(final EventBus eventBus, final MyView view,
@Assisted String param) {
super(eventBus, view);
...
}

and one factory

public interface MyPresenterFactory {
public MyPresenter create(@Assisted String param);
}

And the usual in ClientModule.configure()

install(new
GinFactoryModuleBuilder().build(MyPresenterFactory.class));

This would produce the following error at run-time:

[ERROR] [...] - Binding requested for constant key
Key[type=java.lang.String,
annotation=@com.google.inject.assistedinject.Assisted(value=)] but no
explicit binding was found.

I seriously scratched my head over that one and I ended up finding
that I HAD to use @AssistedInject for my GIN-assisted constructor.
But (as per Guice's documentation) @AssistedInject has to be used only
when there are multiple injectable constructors. So in order to make
the @AssistedInject'd one work, I had to provide another (regular
@Inject'd) constructor that in fact I don't need:

@AssistedInject
public MyPresenter(final EventBus eventBus, final MyView view,
@Assisted String param) {
super(eventBus, view);
...
}

@SuppressWarnings("unused")
@Inject
private MyPresenter(final EventBus eventBus, final MyView view) {
this(eventBus, view, "");
}

Does anyone know why that is?
I'm not sure that this is even a GWTP related issue but it's the only
context where it happened. I already had used @Assisted arguments with
factories before and I never had to do that.

Cheers.

Thomas Broyer

unread,
Jul 2, 2011, 5:23:37 AM7/2/11
to gwt-pl...@googlegroups.com


On Saturday, July 2, 2011 6:58:30 AM UTC+2, dindeman wrote:

[ERROR] [...] - Binding requested for constant key
Key[type=java.lang.String,
annot...@com.google.inject.assistedinject.Assisted(value=)] but no
explicit binding was found.

Generally, this error means you have some direct dependency on MyPresenter, rather than MyPresenterFactory+calling its create() method. And this is what it works with your non-@Assisted @Inject-ed constructor.

(and btw, this has nothing to do with GWTP)

dindeman

unread,
Jul 2, 2011, 6:20:48 AM7/2/11
to GWTP
Thanks Thomas.
You're absolutely right. I had left

Provider<MyPresenter> getMyPresenter();

on the Ginjector interface, and that was the dependency that demanded
some a regular @Inject'd constructor.

Nice one!
Reply all
Reply to author
Forward
0 new messages