Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## com.google.gwt.inject.client.assistedinject
Class GinFactoryModuleBuilder

    java.lang.Object
      com.google.gwt.inject.client.assistedinject.GinFactoryModuleBuilder

    public class GinFactoryModuleBuilder

extends java.lang.Object

Copied and modified from `FactoryModuleBuilder`. Usage is mostly the same, with the exception of forwarded bindings (see at the bottom of this documentation). Provides a factory that combines the caller's arguments with injector-supplied values to construct objects.

### Defining a factory

Create an interface whose methods return the constructed type, or any of its supertypes. The method's parameters are the arguments required to build the constructed type.

    public interface PaymentFactory {
       Payment create(Date startDate, Money amount);
     }

You can name your factory methods whatever you like, such as create, createPayment or newPayment.

### Creating a type that accepts factory parameters

`constructedType` is a concrete class with an @`Inject`-annotated constructor. In addition to injector- supplied parameters, the constructor should have parameters that match each of the factory method's parameters. Each factory-supplied parameter requires an @`Assisted` annotation. This serves to document that the parameter is not bound by your application's modules.

    public class RealPayment implements Payment {
       @Inject
       public RealPayment(
          CreditService creditService,
          AuthService authService,
          @Assisted Date startDate,
          @Assisted Money amount) {
         ...
       }
     }

### Multiple factory methods for the same type

If the factory contains many methods that return the same type, you can create multiple constructors in your concrete class, each constructor marked with with @`AssistedInject`, in order to match the different parameters types of the factory methods.

    public interface PaymentFactory {
        Payment create(Date startDate, Money amount);
        Payment createWithoutDate(Money amount);
     }

     public class RealPayment implements Payment {
      @AssistedInject
       public RealPayment(
          CreditService creditService,
          AuthService authService,
         @Assisted Date startDate,
         @Assisted Money amount) {
         ...
       }

      @AssistedInject
       public RealPayment(
          CreditService creditService,
          AuthService authService,
         @Assisted Money amount) {
         ...
       }
     }

### Configuring simple factories

In your `module`, install a `GinFactoryModuleBuilder` that creates the factory:

    install(new GinFactoryModuleBuilder()
         .implement(Payment.class, RealPayment.class)
         .build(PaymentFactory.class);

As a side-effect of this binding, Gin will inject the factory to initialize it for use. The factory cannot be used until the injector has been initialized.

### Using the factory

Inject your factory into your application classes. When you use the factory, your arguments will be combined with values from the injector to construct an instance.

    public class PaymentAction {
       @Inject private PaymentFactory paymentFactory;

       public void doPayment(Money amount) {
         Payment payment = paymentFactory.create(new Date(), amount);
         payment.apply();
       }
     }

### Making parameter types distinct

The types of the factory method's parameters must be distinct. To use multiple parameters of the same type, use a named @`Assisted` annotation to disambiguate the parameters. The names must be applied to the factory method's parameters:

    public interface PaymentFactory {
       Payment create(
           @Assisted("startDate") Date startDate,
           @Assisted("dueDate") Date dueDate,
           Money amount);
     }

...and to the concrete type's constructor parameters:

    public class RealPayment implements Payment {
       @Inject
       public RealPayment(
          CreditService creditService,
          AuthService authService,
          @Assisted("startDate") Date startDate,
          @Assisted("dueDate") Date dueDate,
          @Assisted Money amount) {
         ...
       }
     }

### Values are created by Gin

Returned factories use child injectors to create values. The values are eligible for method interception. In addition, @`Inject` members will be injected before they are returned.

### More configuration options

In addition to simply specifying an implementation class for any returned type, factories' return values can be automatic or can be configured to use annotations:

If you just want to return the types specified in the factory, do not configure any implementations:

    public interface FruitFactory {
       Apple getApple(Color color);
     }
     ...
     protected void configure() {
       install(new GinFactoryModuleBuilder().build(FruitFactory.class));
     }

Note that any type returned by the factory in this manner needs to be an implementation class.

To return two different implementations for the same interface from your factory, use binding annotations on your return types:

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

In difference to regular Guice Assisted Inject, in Gin, return types in your factory are not further resolved using your regular injector configuration. This means that in the following example you'll still get a `Chicken` and not a `Rooster`:

    interface Animal {}
     public class Chicken implements Animal {}
     public class Rooster extends Chicken {}
     interface AnimalFactory {
       Animal getAnimal();
     }
     ...
     protected void configure() {
       bind(Chicken.class).to(Rooster.class);
       install(new GinFactoryModuleBuilder()
           .implement(Animal.class, Chicken.class)
           .build(AnimalFactory.class));
     }

Constructor Summary
`GinFactoryModuleBuilder()`

Method Summary
`
`<F> GinModule`
` | `build(java.lang.Class<F> factoryInterface)` See the factory configuration examples at `GinFactoryModuleBuilder`.
`
`<F> GinModule`
` | `build(Key<F> factoryInterface)`
`
`<F> GinModule`
` | `build(TypeLiteral<F> factoryInterface)` See the factory configuration examples at `GinFactoryModuleBuilder`.
`
`<T> GinFactoryModuleBuilder`
` | `implement(java.lang.Class<T> source, java.lang.annotation.Annotation annotation, java.lang.Class<? extends T> target)` See the factory configuration examples at `GinFactoryModuleBuilder`.
`
`<T> GinFactoryModuleBuilder`
` | `implement(java.lang.Class<T> source, java.lang.annotation.Annotation annotation, TypeLiteral<? extends T> target)` See the factory configuration examples at `GinFactoryModuleBuilder`.
`
`<T> GinFactoryModuleBuilder`
` | `implement(java.lang.Class<T> source, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType, java.lang.Class<? extends T> target)` See the factory configuration examples at `GinFactoryModuleBuilder`.
`
`<T> GinFactoryModuleBuilder`
` | `implement(java.lang.Class<T> source, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType, TypeLiteral<? extends T> target)` See the factory configuration examples at `GinFactoryModuleBuilder`.
`
`<T> GinFactoryModuleBuilder`
` | `implement(java.lang.Class<T> source, java.lang.Class<? extends T> target)` See the factory configuration examples at `GinFactoryModuleBuilder`.
`
`<T> GinFactoryModuleBuilder`
` | `implement(java.lang.Class<T> source, TypeLiteral<? extends T> target)` See the factory configuration examples at `GinFactoryModuleBuilder`.
`
`<T> GinFactoryModuleBuilder`
` | `implement(Key<T> source, java.lang.Class<? extends T> target)` See the factory configuration examples at `GinFactoryModuleBuilder`.
`
`<T> GinFactoryModuleBuilder`
` | `implement(Key<T> source, TypeLiteral<? extends T> target)` See the factory configuration examples at `GinFactoryModuleBuilder`.
`
`<T> GinFactoryModuleBuilder`
` | `implement(TypeLiteral<T> source, java.lang.annotation.Annotation annotation, java.lang.Class<? extends T> target)` See the factory configuration examples at `GinFactoryModuleBuilder`.
`
`<T> GinFactoryModuleBuilder`
` | `implement(TypeLiteral<T> source, java.lang.annotation.Annotation annotation, TypeLiteral<? extends T> target)` See the factory configuration examples at `GinFactoryModuleBuilder`.
`
`<T> GinFactoryModuleBuilder`
` | `implement(TypeLiteral<T> source, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType, java.lang.Class<? extends T> target)` See the factory configuration examples at `GinFactoryModuleBuilder`.
`
`<T> GinFactoryModuleBuilder`
` | `implement(TypeLiteral<T> source, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType, TypeLiteral<? extends T> target)` See the factory configuration examples at `GinFactoryModuleBuilder`.
`
`<T> GinFactoryModuleBuilder`
` | `implement(TypeLiteral<T> source, java.lang.Class<? extends T> target)` See the factory configuration examples at `GinFactoryModuleBuilder`.
`
`<T> GinFactoryModuleBuilder`
` | `implement(TypeLiteral<T> source, TypeLiteral<? extends T> target)` See the factory configuration examples at `GinFactoryModuleBuilder`.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Constructor Detail

### GinFactoryModuleBuilder

    public GinFactoryModuleBuilder()

Method Detail

### implement

    public <T> GinFactoryModuleBuilder implement(java.lang.Class<T> source,
                                                 java.lang.Class<? extends T> target)

  See the factory configuration examples at `GinFactoryModuleBuilder`.

### implement

    public <T> GinFactoryModuleBuilder implement(java.lang.Class<T> source,
                                                 TypeLiteral<? extends T> target)

  See the factory configuration examples at `GinFactoryModuleBuilder`.

### implement

    public <T> GinFactoryModuleBuilder implement(TypeLiteral<T> source,
                                                 java.lang.Class<? extends T> target)

  See the factory configuration examples at `GinFactoryModuleBuilder`.

### implement

    public <T> GinFactoryModuleBuilder implement(TypeLiteral<T> source,
                                                 TypeLiteral<? extends T> target)

  See the factory configuration examples at `GinFactoryModuleBuilder`.

### implement

    public <T> GinFactoryModuleBuilder implement(java.lang.Class<T> source,
                                                 java.lang.annotation.Annotation annotation,
                                                 java.lang.Class<? extends T> target)

  See the factory configuration examples at `GinFactoryModuleBuilder`.

### implement

    public <T> GinFactoryModuleBuilder implement(java.lang.Class<T> source,
                                                 java.lang.annotation.Annotation annotation,
                                                 TypeLiteral<? extends T> target)

  See the factory configuration examples at `GinFactoryModuleBuilder`.

### implement

    public <T> GinFactoryModuleBuilder implement(TypeLiteral<T> source,
                                                 java.lang.annotation.Annotation annotation,
                                                 java.lang.Class<? extends T> target)

  See the factory configuration examples at `GinFactoryModuleBuilder`.

### implement

    public <T> GinFactoryModuleBuilder implement(TypeLiteral<T> source,
                                                 java.lang.annotation.Annotation annotation,
                                                 TypeLiteral<? extends T> target)

  See the factory configuration examples at `GinFactoryModuleBuilder`.

### implement

    public <T> GinFactoryModuleBuilder implement(java.lang.Class<T> source,
                                                 java.lang.Class<? extends java.lang.annotation.Annotation> annotationType,
                                                 java.lang.Class<? extends T> target)

  See the factory configuration examples at `GinFactoryModuleBuilder`.

### implement

    public <T> GinFactoryModuleBuilder implement(java.lang.Class<T> source,
                                                 java.lang.Class<? extends java.lang.annotation.Annotation> annotationType,
                                                 TypeLiteral<? extends T> target)

  See the factory configuration examples at `GinFactoryModuleBuilder`.

### implement

    public <T> GinFactoryModuleBuilder implement(TypeLiteral<T> source,
                                                 java.lang.Class<? extends java.lang.annotation.Annotation> annotationType,
                                                 java.lang.Class<? extends T> target)

  See the factory configuration examples at `GinFactoryModuleBuilder`.

### implement

    public <T> GinFactoryModuleBuilder implement(TypeLiteral<T> source,
                                                 java.lang.Class<? extends java.lang.annotation.Annotation> annotationType,
                                                 TypeLiteral<? extends T> target)

  See the factory configuration examples at `GinFactoryModuleBuilder`.

### implement

    public <T> GinFactoryModuleBuilder implement(Key<T> source,
                                                 java.lang.Class<? extends T> target)

  See the factory configuration examples at `GinFactoryModuleBuilder`.

### implement

    public <T> GinFactoryModuleBuilder implement(Key<T> source,
                                                 TypeLiteral<? extends T> target)

  See the factory configuration examples at `GinFactoryModuleBuilder`.

### build

    public <F> GinModule build(java.lang.Class<F> factoryInterface)

  See the factory configuration examples at `GinFactoryModuleBuilder`.

### build

    public <F> GinModule build(TypeLiteral<F> factoryInterface)

  See the factory configuration examples at `GinFactoryModuleBuilder`.

### build

    public <F> GinModule build(Key<F> factoryInterface)

Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD