frjaeger220 / google-guice

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

[RFE] Add annotations for AssistedInjected factories #441

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I just thought Guice may use annotations for AssistedInject as it does
normally.

@ImplementedBy(BasicStuff.class)
interface Stuff {  }

// Class is invalid (2x same method signature), but it's just for the show
// Equivalent to bind(StuffFactory)
//    .to(FactoryProvider.newFactory(StuffFactory, XYZ))
@AssistedFactory
interface StuffFactory {
  // Using default implementation for replacement of XYZ.
  public Stuff create(String name);

  // Using defined implementation, replaces XYZ.
  @ImplementedBy(NiceStuff.class)
  public Stuff create(String name);
}

class BasicStuff implements Stuff {
  StuffImpl (@Assisted String name) { }
}

class NiceStuff implements Stuff {
  NiceStuff (@Assisted String name) { }
}

This way a simple call like the following will work.

Stuff myStuff = Guice
                   .createInjector()
                   .getInstance(StuffFactory.class)
                   .create("myStuff");

Maybe this would require to put the assisted injection package in the main
distribution, but since it's 25kb long, is it really a problem?

Original issue reported on code.google.com by ogregoire on 26 Oct 2009 at 1:14

GoogleCodeExporter commented 9 years ago
No plans right now to move AssistedInject into the main distribution.  If you 
would
like fancier abilities in your factories, such as converting the factory 
parameter to
something else, consider manually implementing your factory to do it & 
constructing
the dependent class.

Original comment by sberlin on 2 May 2010 at 12:26