AssistedInject with GIN example

1,649 views
Skip to first unread message

opn

unread,
Feb 23, 2011, 5:43:56 AM2/23/11
to GWTP
Hello folks!

I want to upgrade to gwt 2.2 soon and i'm using GWTP in my project. I
know which .jars im going to need etc. and already did assisted
injection in GIN with an older version.

Now my question is if something has changed regarding the binding of
the assisted injection factories?

Until now i had to make an interface, which my class that i want to
assisted inject implements, write a factory interface and bind it in a
kind of complicated way.

install(new GinFactoryModuleBuilder().implement( ... ).build( ...);

Would be nice if you could post an example how you are doing an
assisted injection with the newest version!

Thx a lot and keep up the good work GWTP-Team!

Christian Goudreau

unread,
Feb 23, 2011, 8:07:18 AM2/23/11
to gwt-pl...@googlegroups.com
Well, assisted injection with Gin is based on Guice 3 and didn't change in Gin.

Here's an  example:
public interface BlogItemWidgetFactory {
  BlogItemWidget create(BlogItem blogItem);
}

And how to bin in Gin:
install(new GinFactoryModuleBuilder().build(BlogItemWidgetFactory.class));

Cheers,
--
Christian Goudreau

Philippe Beaudoin

unread,
Feb 23, 2011, 11:39:04 AM2/23/11
to gwt-pl...@googlegroups.com
And BlogItemWidget has an @Assisted parameter for BlogItem in its constructor, right?

Christian Goudreau

unread,
Feb 23, 2011, 11:42:57 AM2/23/11
to gwt-pl...@googlegroups.com
Right :D I didn't mentioned this because he said that he already used Assited Injection before.

Cheers,


On Wed, Feb 23, 2011 at 11:39 AM, Philippe Beaudoin <philippe...@gmail.com> wrote:
And BlogItemWidget has an @Assisted parameter for BlogItem in its constructor, right?



--
Christian Goudreau

opn

unread,
Feb 24, 2011, 3:06:41 AM2/24/11
to GWTP
Ok thanks!
That's basically how I'm doing it right now, except that my binding is
a bit longer.
I hoped that it's maybe possible to get rid of the extra interface /
factory somehow. ; )
Or do you have a hint how to make these interfaces inner interfaces of
some class?
Right now i've got a package "assist" where i put all this stuff into.

Philippe Beaudoin

unread,
Feb 24, 2011, 3:17:29 AM2/24/11
to gwt-pl...@googlegroups.com
No way to get rid of the interface, as this is what Gin/Guice "explores" (with reflection) to figure out which methods can be used for creating an object. However, I'm pretty sure you can declare the factory as an inner interface inside the object it's creating, no?

Ahmed Mohammed

unread,
Apr 12, 2011, 12:30:01 PM4/12/11
to GWTP
I am getting error when trying to use @Assisted feature in Activities.
Its failing to inject factory
[DEBUG] [sam_web] - Rebinding com.sam.client.gin.Injector
[DEBUG] [sam_web] - Invoking generator
com.google.gwt.inject.rebind.GinjectorGenerator
[ERROR] [sam_web] - No implementation bound for
"Key[type=com.sam.client.place.ConfirmationPlace,
annotation=@com.google.inject.assistedinject.Assisted(value=confirmationPlace)]"
and an implicit binding cannot be created because the type is
annotated.
[ERROR] [sam_web] - No implementation bound for
"Key[type=com.sam.client.place.AddEditItemPlace,
annotation=@com.google.inject.assistedinject.Assisted(value=addEditItemPlace)]"
and an implicit binding cannot be created because the type is
annotated.

I am using Gin 1.5 and Guice 3.0 version along GWT 2.2 version app
which uses MVP API and followed
http://google-gin.googlecode.com/svn/trunk/javadoc/com/google/gwt/inject/client/assistedinject/GinFactoryModuleBuilder.html
I am trying to use @Assisted with couple of the Activities which have
Place as constructor variable.
Activity Constructor:
@AssistedInject
public ConfirmationActivity(SysRPCServiceAsync rpcService,
PlaceController placeController, ConfirmationView
view,@Assisted("confirmationPlace") ConfirmationPlace place)

Acitivityfactory defined inside activitymapper.. and inject to get
activity instances.

public class AppActivityMapper implements ActivityMapper {
@Inject
ActivityFactory factory;


@Override
public Activity getActivity(Place place) {
log.info("Place called: " + place);

if (place instanceof ConfirmationPlace) {
return factory.createConfirmationActivity((ConfirmationPlace)
place);
}
return null;
}
/**
* Methods capable of creating presenters given the place that is
passed in
*
*/
public interface ActivityFactory {
AddEditItemActivity
createAddEditItemActivity(@Assisted("addEditItemPlace")AddEditItemPlace
place);
ConfirmationActivity
createConfirmationActivity(@Assisted("confirmationPlace")ConfirmationPlace
place);
}

}

And in Injector module:
install(new GinFactoryModuleBuilder().build(ActivityFactory.class));

Am i missing something? Any help would be greatly appreciated.

Christian Goudreau

unread,
Apr 12, 2011, 12:59:07 PM4/12/11
to gwt-pl...@googlegroups.com
Try to request static injection on ActivityFactory. Also you shouldn't need to name your @Assisted since you don't have two object of the same type inside your Ctor.

You also don't need the @AssistedInject on your ConfirmationActivity. 

Well, maybe it's me, but I always use a multiple factory function with interfaces and named, and I never had problems... 

Here's an example:

    // Gin factory modules

    install(new GinFactoryModuleBuilder()

      .implement(StatisticStrategy.class, Names.named("AveragePriceByCity"), AveragePriceByCityStrategy.class)

      .implement(StatisticStrategy.class, Names.named("AveragePriceByBedroom"),AveragePriceByBedroomStrategy.class)

      .implement(StatisticStrategy.class, Names.named("AveragePriceByAge"),AveragePriceByAgeStrategy.class)

      .build(StatisticStrategyFactory.class));


public interface StatisticStrategyFactory {

  @Named("AveragePriceByCity") StatisticStrategy createAveragePriceByCityStrategy(List<House> houses);

  @Named("AveragePriceByBedroom") StatisticStrategy createAveragePriceByBedroomStrategy(List<House> houses);

  @Named("AveragePriceByAge") StatisticStrategy createAveragePriceByAgeStrategy(List<House> houses);

}


Cheers

--
Christian Goudreau

Christian Goudreau

unread,
Apr 12, 2011, 1:03:06 PM4/12/11
to gwt-pl...@googlegroups.com
Try cleaning first, then try my example :D

I think the problem come from @AssistedInject.
--
Christian Goudreau

Christian Goudreau

unread,
Apr 12, 2011, 1:07:10 PM4/12/11
to gwt-pl...@googlegroups.com
Just a quote from the Javadoc:

When used in tandem with FactoryModuleBuilder, constructors annotated with @AssistedInject indicate that multiple constructors can be injected, each with different parameters. AssistedInject annotations should not be mixed with @Inject annotations. The assisted parameters must exactly match one corresponding factory method within the factory interface, but the parameters do not need to be in the same order. Constructors annotated with AssistedInject are created by Guice and receive all the benefits (such as AOP).

--
Christian Goudreau

Ahmed Mohammed

unread,
Apr 12, 2011, 3:39:31 PM4/12/11
to GWTP
Thanks for the quick response. I made changes based on your suggestion
and still getting the same error only difference is it dosen't display
@assisted value
[DEBUG] [sam_web] - Rebinding com.sam.client.gin.Injector
[DEBUG] [sam_web] - Invoking generator
com.google.gwt.inject.rebind.GinjectorGenerator
[ERROR] [sam_web] - No implementation bound for
"Key[type=com.sam.client.place.ConfirmationPlace,
annotation=@com.google.inject.assistedinject.Assisted(value=)]" and an
implicit binding cannot be created because the type is annotated.
[ERROR] [sam_web] - No implementation bound for
"Key[type=com.sam.client.place.AddEditItemPlace,
annotation=@com.google.inject.assistedinject.Assisted(value=)]" and an
implicit binding cannot be created because the type is annotated.
[ERROR] [sam_web] - No implementation bound for
"Key[type=com.sam.client.place.ListItemsPlace,
annotation=@com.google.inject.assistedinject.Assisted(value=)]" and an
implicit binding cannot be created because the type is annotated.

Below are the changes i made based on your post.
Injector Module:
install(new GinFactoryModuleBuilder()
.implement(AbstractActivity.class, Names.named("ItemsActivity"),
ItemsActivity.class)
.implement(AbstractActivity.class,
Names.named("AddEditItemActivity"), AddEditItemActivity.class)
.implement(AbstractActivity.class,
Names.named("ConfirmationActivity"), ConfirmationActivity.class)
.build(ActivityFactory.class));

ActivityFactory:

public interface ActivityFactory {
@Named("ItemsActivity") AbstractActivity
createItemsActivity(ListItemsPlace place);
@Named("AddEditItemActivity") AbstractActivity
createAddEditItemActivity(AddEditItemPlace place);
@Named("ConfirmationActivity") AbstractActivity
createRequestConfirmationActivity(ConfirmationPlace place);
}
and made Acitvityfactory variable static:
@Inject
static ActivityFactory factory;

And changed from @AssistedInject to Inject and removed names from
@assisted variables
@Inject
public ConfirmationActivity(SysRPCServiceAsync rpcService,
PlaceController placeController, ConfirmationView view,
AppConstants appConstants,Styles.Resources resources,
@Assisted ConfirmationPlace place) {}


And Thanks again.
-Ahmed

Christian Goudreau

unread,
Apr 13, 2011, 9:47:06 AM4/13/11
to gwt-pl...@googlegroups.com
Can you provide the complete ConfirmationPlace ?

Don't forget to requestStaticInjection on your static ActivityFActory. I doubt it'll change anything with your problem, but that could solve one in when we'll figure what your actual problem is.

Thanks
--
Christian Goudreau

Ahmed Mohammed

unread,
Apr 13, 2011, 11:05:59 AM4/13/11
to GWTP
The error is fixed. I was injecting activity using the factory and
earlier i also added direct binding.
Inside Inejctor module:

bind(ConfirmationView.Presenter.class).to(ConfirmationActivity.class)
The latter one was causing the problem as Activity constructor had
@Assisted variable in it.Once removed the above line it worked.

Thanks for your help.
-Ahmed

On Apr 13, 9:47 am, Christian Goudreau <goudreau.christ...@gmail.com>
wrote:
> Can you provide the complete ConfirmationPlace ?
>
> Don't forget to requestStaticInjection on your static ActivityFActory. I
> doubt it'll change anything with your problem, but that could solve one in
> when we'll figure what your actual problem is.
>
> Thanks
>
> On Tue, Apr 12, 2011 at 3:39 PM, Ahmed Mohammed <ahmedmohiud...@gmail.com>wrote:
>
>
>
>
>
>
>
>
>
> > Thanks for the quick response. I made changes based on your suggestion
> > and still getting the same error only difference is it dosen't display
> > @assisted value
> > [DEBUG] [sam_web] - Rebinding com.sam.client.gin.Injector
> >        [DEBUG] [sam_web] - Invoking generator
> > com.google.gwt.inject.rebind.GinjectorGenerator
> >                [ERROR] [sam_web] - No implementation bound for
> > "Key[type=com.sam.client.place.ConfirmationPlace,
> > annotati...@com.google.inject.assistedinject.Assisted(value=)]" and an
> > implicit binding cannot be created because the type is annotated.
> >                [ERROR] [sam_web] - No implementation bound for
> > "Key[type=com.sam.client.place.AddEditItemPlace,
> > annotati...@com.google.inject.assistedinject.Assisted(value=)]" and an
> > implicit binding cannot be created because the type is annotated.
> >                [ERROR] [sam_web] - No implementation bound for
> > "Key[type=com.sam.client.place.ListItemsPlace,
> > annotati...@com.google.inject.assistedinject.Assisted(value=)]" and an

Przemyslaw Galazka

unread,
Dec 14, 2015, 5:10:14 AM12/14/15
to GWTP
I had the same problem. Thanks for hints :)
Reply all
Reply to author
Forward
0 new messages