Re: Annotations and the factory module builder

762 views
Skip to first unread message

Fred Faber

unread,
Jul 14, 2012, 8:24:44 AM7/14/12
to google...@googlegroups.com

It's there...look at the javadoc (it's not obvious). 

On Jul 14, 2012 8:11 AM, "Mike Atkins" <apeh...@gmail.com> wrote:
I would like to use a binding annotation on the binding generated in a FactoryModuleBuilder. FactoryModuleBuilder does not have an obvious way to add this, so I plan to use the Module generated by the FactoryModuleBuilder to create a FactoryProvider via an Injector created from that Module which I can then bind via annotation. Is there a better way to do this and are annotated bindings for assisted injection a planned feature?

Cheers,

Mike

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-guice/-/eKe2L9h0dvEJ.
To post to this group, send email to google...@googlegroups.com.
To unsubscribe from this group, send email to google-guice...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.

Sam Berlin

unread,
Jul 17, 2012, 9:29:41 AM7/17/12
to google...@googlegroups.com, ffa...@faiser.com
The build(Key) method embeds a TypeLiteral/Class & Annotation.  So 

protected void configure() {
   install(new FactoryModuleBuilder()
       .implement(Car.class, Names.named("fast"), BluePorsche.class)
       .implement(Car.class, Names.named("clean"), BluePrius.class)
       .build(Key.get(CarFactory.class, Blue.class))
   install(new FactoryModuleBuilder()
       .implement(Car.class, Names.named("fast"), RedPorsche.class)
       .implement(Car.class, Names.named("clean"), RedPrius.class)
       .build(Key.get(CarFactory.class, Red.class))

will do what you want.
 sam


On Mon, Jul 16, 2012 at 9:25 PM, Mike Atkins <apeh...@gmail.com> wrote:


On Saturday, July 14, 2012 8:24:44 AM UTC-4, Fred wrote:

It's there...look at the javadoc (it's not obvious). 

There is javadoc about creating annotations for the return types of the factory which the FactoryModuleBuilder creates; however, I do not see any for annotating the binding to the factory created by the FactoryModuleBuilder.

To be clear, I do not want to do 

interface CarFactory {
   @Named("fast") Car getFastCar(Color color);
   @Named("clean") Car getCleanCar(Color color);
 }
 ...
 protected void configure() {
   install(new FactoryModuleBuilder()
       .implement(Car.class, Names.named("fast"), Porsche.class)
       .implement(Car.class, Names.named("clean"), Prius.class)
       .build(CarFactory.class));
 }

but rather
 
protected void configure() {
   install(new FactoryModuleBuilder()
       .implement(Car.class, Names.named("fast"), BluePorsche.class)
       .implement(Car.class, Names.named("clean"), BluePrius.class)
       .build(CarFactory.class)
       .annotatedWith(Blue.class));
   install(new FactoryModuleBuilder()
       .implement(Car.class, Names.named("fast"), RedPorsche.class)
       .implement(Car.class, Names.named("clean"), RedPrius.class)
       .build(CarFactory.class)
       .annotatedWith(Red.class));

 }

However, that .annotatedWith does not exist for Module or in the FactoryModuleBuilder.
 
Apologies my initial question was not clear.

On Jul 14, 2012 8:11 AM, "Mike Atkins" wrote:
I would like to use a binding annotation on the binding generated in a FactoryModuleBuilder. FactoryModuleBuilder does not have an obvious way to add this, so I plan to use the Module generated by the FactoryModuleBuilder to create a FactoryProvider via an Injector created from that Module which I can then bind via annotation. Is there a better way to do this and are annotated bindings for assisted injection a planned feature?

Cheers,

Mike

--

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-guice/-/onZ2hX59qDwJ.

Ed Valdez

unread,
Sep 6, 2016, 10:45:33 PM9/6/16
to google-guice, ffa...@faiser.com
Hi Mike,

Did you ever figure out a way to do this?  I'm trying to figure out how to annotate the factory implementations generated by Guice for assisted injections, but can't find a straight forward way.

Thanks,
Ed

Mike Atkins

unread,
Sep 7, 2016, 10:48:34 AM9/7/16
to google-guice
Sam's reply worked for me four years ago, apparently (I sent him a thank you note off list). That's all I can tell you: my memory of this is nonexistent.
Reply all
Reply to author
Forward
0 new messages