F
- The factory interfaceFactoryModuleBuilder
instead.@Deprecated public class FactoryProvider<F> extends java.lang.Object implements Provider<F>, HasDependencies
FactoryModuleBuilder
for its more concise API and
additional capability.
Provides a factory that combines the caller's arguments with injector-supplied values to construct objects.
public interface PaymentFactory { Payment create(Date startDate, Money amount); }You can name your factory methods whatever you like, such as create, createPayment or newPayment.
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) { ... } }Any parameter that permits a null value should also be annotated
@Nullable
.
module
, bind the factory interface to the returned
factory:
bind(PaymentFactory.class).toProvider( FactoryProvider.newFactory(PaymentFactory.class, RealPayment.class));As a side-effect of this binding, Guice will inject the factory to initialize it for use. The factory cannot be used until the injector has been initialized.
public class PaymentAction { @Inject private PaymentFactory paymentFactory; public void doPayment(Money amount) { Payment payment = paymentFactory.create(new Date(), amount); payment.apply(); } }
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) { ... } }
AssistedInject
. This triggers a limited backwards-compatability mode.
Instead of matching factory method arguments to constructor parameters using their names, the parameters are matched by their order. The first factory method argument is used for the first @Assisted constructor parameter, etc.. Annotation names have no effect.
Returned values are not created by Guice. These types are not eligible for method interception. They do receive post-construction member injection.
Modifier and Type | Field and Description |
---|---|
private java.util.Map<java.lang.reflect.Method,AssistedConstructor<?>> |
factoryMethodToConstructor
Deprecated.
|
private TypeLiteral<F> |
factoryType
Deprecated.
|
private TypeLiteral<?> |
implementationType
Deprecated.
|
private Injector |
injector
Deprecated.
|
Modifier | Constructor and Description |
---|---|
private |
FactoryProvider(TypeLiteral<F> factoryType,
TypeLiteral<?> implementationType,
java.util.Map<java.lang.reflect.Method,AssistedConstructor<?>> factoryMethodToConstructor)
Deprecated.
|
Modifier and Type | Method and Description |
---|---|
private void |
checkDeclaredExceptionsMatch()
Deprecated.
|
private static java.util.Map<java.lang.reflect.Method,AssistedConstructor<?>> |
createMethodMapping(TypeLiteral<?> factoryType,
TypeLiteral<?> implementationType)
Deprecated.
|
boolean |
equals(java.lang.Object obj)
Deprecated.
|
F |
get()
Deprecated.
Provides an instance of
T . |
java.util.Set<Dependency<?>> |
getDependencies()
Deprecated.
Returns the known dependencies for this type.
|
int |
hashCode()
Deprecated.
|
private boolean |
isConstructorExceptionCompatibleWithFactoryExeception(java.lang.Class<?> constructorException,
java.lang.Class<?>[] factoryExceptions)
Deprecated.
|
private static ConfigurationException |
newConfigurationException(java.lang.String format,
java.lang.Object... args)
Deprecated.
|
static <F> Provider<F> |
newFactory(java.lang.Class<F> factoryType,
java.lang.Class<?> implementationType)
Deprecated.
|
static <F> Provider<F> |
newFactory(TypeLiteral<F> factoryType,
TypeLiteral<?> implementationType)
Deprecated.
|
private boolean |
paramCanBeInjected(Parameter parameter,
Injector injector)
Deprecated.
|
(package private) void |
setInjectorAndCheckUnboundParametersAreInjectable(Injector injector)
Deprecated.
|
private Injector injector
private final TypeLiteral<F> factoryType
private final TypeLiteral<?> implementationType
private final java.util.Map<java.lang.reflect.Method,AssistedConstructor<?>> factoryMethodToConstructor
private FactoryProvider(TypeLiteral<F> factoryType, TypeLiteral<?> implementationType, java.util.Map<java.lang.reflect.Method,AssistedConstructor<?>> factoryMethodToConstructor)
public static <F> Provider<F> newFactory(java.lang.Class<F> factoryType, java.lang.Class<?> implementationType)
public static <F> Provider<F> newFactory(TypeLiteral<F> factoryType, TypeLiteral<?> implementationType)
@Inject void setInjectorAndCheckUnboundParametersAreInjectable(Injector injector)
private void checkDeclaredExceptionsMatch()
private boolean isConstructorExceptionCompatibleWithFactoryExeception(java.lang.Class<?> constructorException, java.lang.Class<?>[] factoryExceptions)
private boolean paramCanBeInjected(Parameter parameter, Injector injector)
private static java.util.Map<java.lang.reflect.Method,AssistedConstructor<?>> createMethodMapping(TypeLiteral<?> factoryType, TypeLiteral<?> implementationType)
public java.util.Set<Dependency<?>> getDependencies()
HasDependencies
Injector
will be
included in the returned set.getDependencies
in interface HasDependencies
public F get()
Provider
T
. Must never return null
.get
in interface javax.inject.Provider<F>
public int hashCode()
hashCode
in class java.lang.Object
public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
private static ConfigurationException newConfigurationException(java.lang.String format, java.lang.Object... args)