Package com.google.inject.assistedinject
Class FactoryProvider2<F>
java.lang.Object
com.google.inject.assistedinject.FactoryProvider2<F>
- All Implemented Interfaces:
AssistedInjectBinding<F>
,Provider<F>
,HasDependencies
,ProviderWithExtensionVisitor<F>
,InvocationHandler
,javax.inject.Provider<F>
final class FactoryProvider2<F>
extends Object
implements InvocationHandler, ProviderWithExtensionVisitor<F>, HasDependencies, AssistedInjectBinding<F>
The newer implementation of factory provider. This implementation uses a child injector to create
values.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static class
All the data necessary to perform an assisted inject.private static class
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final com.google.common.collect.ImmutableMap<Method,
FactoryProvider2.AssistData> Mapping from method to the data about how the method will be assisted.private final BindingCollector
The binding collector, for equality/hashing purposes.(package private) static final Assisted
if a factory method parameter isn't annotated, it gets this annotation.private final F
the factory interface, implemented and providedThe key that this is bound to.private Injector
the hosting injector, or null if we haven't been initialized yet(package private) static final Logger
private final com.google.common.collect.ImmutableMap<Method,
MethodHandle> Mapping from method to method handle, for generated default methods.private static final Constructor<MethodHandles.Lookup>
(package private) static final Annotation
A constant annotation to denote the return value, instead of creating a new one each time. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<T,
V> V acceptExtensionVisitor
(BindingTargetVisitor<T, V> visitor, ProviderInstanceBinding<? extends T> binding) Instructs the extension determine if the visitor is an instance of a custom extension visitor, and if so, visit it using that method.private <T> Key<T>
Returns a key similar tokey
, but with an @Assisted binding annotation.(package private) static boolean
canRethrow
(Method invoked, Throwable thrown) Returns true ifthrown
can be thrown byinvoked
without wrapping.private boolean
constructorHasMatchingParams
(TypeLiteral<?> type, Constructor<?> constructor, List<Key<?>> paramList, Errors errors) Matching logic for constructors annotated with AssistedInject.private static MethodHandle
createMethodHandle
(Method method, Object proxy) boolean
private <T> InjectionPoint
findMatchingConstructorInjectionPoint
(Method method, Key<?> returnType, TypeLiteral<T> implementation, List<Key<?>> paramList) Finds a constructor suitable for the method.private static Constructor<MethodHandles.Lookup>
get()
Provides an instance ofT
.Returns anAssistedMethod
for each method in the factory.Binding<?>
getBindingFromNewInjector
(Method method, Object[] args, FactoryProvider2.AssistData data) Creates a child injector that binds the args, and returns the binding for the method's result.Set<Dependency<?>>
Returns the known dependencies for this type.private Set<Dependency<?>>
getDependencies
(InjectionPoint ctorPoint, TypeLiteral<?> implementation) Calculates all dependencies required by the implementation and constructor.getKey()
Returns theKey
for the factory binding.int
hashCode()
(package private) void
initialize
(Injector injector) At injector-creation time, we initialize the invocation handler.When a factory method is invoked, we create a child injector that binds all parameters, then use that to get an instance of the return type.private boolean
isCompatible
(Method src, Method dst) (package private) static boolean
private boolean
isInjectorOrAssistedProvider
(Dependency<?> dependency) private boolean
isTypeNotSpecified
(TypeLiteral<?> typeLiteral, ConfigurationException ce) Returns true if the ConfigurationException is due to an error of TypeLiteral not being fully specified.private boolean
isValidForOptimizedAssistedInject
(Set<Dependency<?>> dependencies, Class<?> implementation, TypeLiteral<?> factoryType) Returns true if all dependencies are suitable for the optimized version of AssistedInject.private Set<Dependency<?>>
removeAssistedDeps
(Set<Dependency<?>> deps) Return all non-assisted dependencies.toString()
private void
validateFactoryReturnType
(Errors errors, Class<?> returnType, Class<?> factoryType)
-
Field Details
-
RETURN_ANNOTATION
A constant annotation to denote the return value, instead of creating a new one each time. -
logger
-
DEFAULT_ANNOTATION
if a factory method parameter isn't annotated, it gets this annotation. -
assistDataByMethod
private final com.google.common.collect.ImmutableMap<Method,FactoryProvider2.AssistData> assistDataByMethodMapping from method to the data about how the method will be assisted. -
methodHandleByMethod
Mapping from method to method handle, for generated default methods. -
injector
the hosting injector, or null if we haven't been initialized yet -
factory
the factory interface, implemented and provided -
factoryKey
The key that this is bound to. -
collector
The binding collector, for equality/hashing purposes. -
methodHandlesLookupCxtor
-
-
Constructor Details
-
FactoryProvider2
FactoryProvider2(Key<F> factoryKey, BindingCollector collector) - Parameters:
factoryKey
- a key for a Java interface that defines one or more create methods.collector
- binding configuration that maps method return types to implementation types.
-
-
Method Details
-
isDefault
-
isCompatible
-
get
Description copied from interface:Provider
Provides an instance ofT
. -
getDependencies
Description copied from interface:HasDependencies
Returns the known dependencies for this type. If this has dependencies whose values are not known statically, a dependency for theInjector
will be included in the returned set.- Specified by:
getDependencies
in interfaceHasDependencies
- Returns:
- a possibly empty set
-
getKey
Description copied from interface:AssistedInjectBinding
Returns theKey
for the factory binding.- Specified by:
getKey
in interfaceAssistedInjectBinding<F>
-
getAssistedMethods
Description copied from interface:AssistedInjectBinding
Returns anAssistedMethod
for each method in the factory.- Specified by:
getAssistedMethods
in interfaceAssistedInjectBinding<F>
-
acceptExtensionVisitor
public <T,V> V acceptExtensionVisitor(BindingTargetVisitor<T, V> visitor, ProviderInstanceBinding<? extends T> binding) Description copied from interface:ProviderWithExtensionVisitor
Instructs the extension determine if the visitor is an instance of a custom extension visitor, and if so, visit it using that method. If the visitor is not an instance of the custom extension visitor, this method MUST call visitor.visit(binding).Due to issues with generics, the type parameters of this method do not relate to the type of the provider. In practice, the 'B' type will always be a supertype of 'T'.
- Specified by:
acceptExtensionVisitor
in interfaceProviderWithExtensionVisitor<F>
-
validateFactoryReturnType
-
isTypeNotSpecified
Returns true if the ConfigurationException is due to an error of TypeLiteral not being fully specified. -
findMatchingConstructorInjectionPoint
private <T> InjectionPoint findMatchingConstructorInjectionPoint(Method method, Key<?> returnType, TypeLiteral<T> implementation, List<Key<?>> paramList) throws ErrorsException Finds a constructor suitable for the method. If the implementation contained any constructors marked withAssistedInject
, this requires allAssisted
parameters to exactly match the parameters (in any order) listed in the method. Otherwise, if noAssistedInject
constructors exist, this will default to looking for an @Inject
constructor.- Throws:
ErrorsException
-
constructorHasMatchingParams
private boolean constructorHasMatchingParams(TypeLiteral<?> type, Constructor<?> constructor, List<Key<?>> paramList, Errors errors) throws ErrorsException Matching logic for constructors annotated with AssistedInject. This returns true if and only if all @Assisted parameters in the constructor exactly match (in any order) all @Assisted parameters the method's parameter.- Throws:
ErrorsException
-
getDependencies
Calculates all dependencies required by the implementation and constructor. -
removeAssistedDeps
Return all non-assisted dependencies. -
isValidForOptimizedAssistedInject
private boolean isValidForOptimizedAssistedInject(Set<Dependency<?>> dependencies, Class<?> implementation, TypeLiteral<?> factoryType) Returns true if all dependencies are suitable for the optimized version of AssistedInject. The optimized version caches the binding & uses a ThreadLocal Provider, so can only be applied if the assisted bindings are immediately provided. This looks for hints that the values may be lazily retrieved, by looking for injections of Injector or a Provider for the assisted values. -
isInjectorOrAssistedProvider
-
assistKey
Returns a key similar tokey
, but with an @Assisted binding annotation. This fails if another binding annotation is clobbered in the process. If the key already has the @Assisted annotation, it is returned as-is to preserve any String value.- Throws:
ErrorsException
-
initialize
At injector-creation time, we initialize the invocation handler. At this time we make sure all factory methods will be able to build the target types. -
getBindingFromNewInjector
public Binding<?> getBindingFromNewInjector(Method method, Object[] args, FactoryProvider2.AssistData data) Creates a child injector that binds the args, and returns the binding for the method's result. -
invoke
When a factory method is invoked, we create a child injector that binds all parameters, then use that to get an instance of the return type.- Specified by:
invoke
in interfaceInvocationHandler
- Throws:
Throwable
-
toString
-
hashCode
public int hashCode() -
equals
-
canRethrow
Returns true ifthrown
can be thrown byinvoked
without wrapping. -
findMethodHandlesLookupCxtor
-
createMethodHandle
-