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>,java.lang.reflect.InvocationHandler,javax.inject.Provider<F>
final class FactoryProvider2<F> extends java.lang.Object implements java.lang.reflect.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 Classes Modifier and Type Class Description private static classFactoryProvider2.AssistDataAll the data necessary to perform an assisted inject.private static classFactoryProvider2.MethodHandleWrapperWrapper around MethodHandles/MethodHandle, so we can compile+run on java6.private static classFactoryProvider2.ThreadLocalProvider
-
Field Summary
Fields Modifier and Type Field Description private com.google.common.collect.ImmutableMap<java.lang.reflect.Method,FactoryProvider2.AssistData>assistDataByMethodMapping from method to the data about how the method will be assisted.private BindingCollectorcollectorThe binding collector, for equality/hashing purposes.(package private) static AssistedDEFAULT_ANNOTATIONif a factory method parameter isn't annotated, it gets this annotation.private Ffactorythe factory interface, implemented and providedprivate Key<F>factoryKeyThe key that this is bound to.private Injectorinjectorthe hosting injector, or null if we haven't been initialized yet(package private) static java.util.logging.Loggerloggerprivate com.google.common.collect.ImmutableMap<java.lang.reflect.Method,FactoryProvider2.MethodHandleWrapper>methodHandleByMethodMapping from method to method handle, for generated default methods.(package private) static java.lang.annotation.AnnotationRETURN_ANNOTATIONA constant annotation to denote the return value, instead of creating a new one each time.
-
Constructor Summary
Constructors Constructor Description FactoryProvider2(Key<F> factoryKey, BindingCollector collector)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description <T,V>
VacceptExtensionVisitor(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>assistKey(java.lang.reflect.Method method, Key<T> key, Errors errors)Returns a key similar tokey, but with an @Assisted binding annotation.(package private) static booleancanRethrow(java.lang.reflect.Method invoked, java.lang.Throwable thrown)Returns true ifthrowncan be thrown byinvokedwithout wrapping.private booleanconstructorHasMatchingParams(TypeLiteral<?> type, java.lang.reflect.Constructor<?> constructor, java.util.List<Key<?>> paramList, Errors errors)Matching logic for constructors annotated with AssistedInject.booleanequals(java.lang.Object obj)private <T> InjectionPointfindMatchingConstructorInjectionPoint(java.lang.reflect.Method method, Key<?> returnType, TypeLiteral<T> implementation, java.util.List<Key<?>> paramList)Finds a constructor suitable for the method.Fget()Provides an instance ofT.java.util.Collection<AssistedMethod>getAssistedMethods()Returns anAssistedMethodfor each method in the factory.Binding<?>getBindingFromNewInjector(java.lang.reflect.Method method, java.lang.Object[] args, FactoryProvider2.AssistData data)Creates a child injector that binds the args, and returns the binding for the method's result.java.util.Set<Dependency<?>>getDependencies()Returns the known dependencies for this type.private java.util.Set<Dependency<?>>getDependencies(InjectionPoint ctorPoint, TypeLiteral<?> implementation)Calculates all dependencies required by the implementation and constructor.Key<F>getKey()Returns theKeyfor the factory binding.inthashCode()(package private) voidinitialize(Injector injector)At injector-creation time, we initialize the invocation handler.java.lang.Objectinvoke(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args)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 booleanisCompatible(java.lang.reflect.Method src, java.lang.reflect.Method dst)(package private) static booleanisDefault(java.lang.reflect.Method method)private booleanisInjectorOrAssistedProvider(Dependency<?> dependency)private booleanisTypeNotSpecified(TypeLiteral<?> typeLiteral, ConfigurationException ce)Returns true if the ConfigurationException is due to an error of TypeLiteral not being fully specified.private booleanisValidForOptimizedAssistedInject(java.util.Set<Dependency<?>> dependencies, java.lang.Class<?> implementation, TypeLiteral<?> factoryType)Returns true if all dependencies are suitable for the optimized version of AssistedInject.private java.util.Set<Dependency<?>>removeAssistedDeps(java.util.Set<Dependency<?>> deps)Return all non-assisted dependencies.java.lang.StringtoString()private voidvalidateFactoryReturnType(Errors errors, java.lang.Class<?> returnType, java.lang.Class<?> factoryType)
-
-
-
Field Detail
-
RETURN_ANNOTATION
static final java.lang.annotation.Annotation RETURN_ANNOTATION
A constant annotation to denote the return value, instead of creating a new one each time.
-
logger
static final java.util.logging.Logger logger
-
DEFAULT_ANNOTATION
static final Assisted DEFAULT_ANNOTATION
if a factory method parameter isn't annotated, it gets this annotation.
-
assistDataByMethod
private final com.google.common.collect.ImmutableMap<java.lang.reflect.Method,FactoryProvider2.AssistData> assistDataByMethod
Mapping from method to the data about how the method will be assisted.
-
methodHandleByMethod
private final com.google.common.collect.ImmutableMap<java.lang.reflect.Method,FactoryProvider2.MethodHandleWrapper> methodHandleByMethod
Mapping from method to method handle, for generated default methods.
-
injector
private Injector injector
the hosting injector, or null if we haven't been initialized yet
-
factory
private final F factory
the factory interface, implemented and provided
-
collector
private final BindingCollector collector
The binding collector, for equality/hashing purposes.
-
-
Constructor Detail
-
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 Detail
-
isDefault
static boolean isDefault(java.lang.reflect.Method method)
-
isCompatible
private boolean isCompatible(java.lang.reflect.Method src, java.lang.reflect.Method dst)
-
getDependencies
public java.util.Set<Dependency<?>> getDependencies()
Description copied from interface:HasDependenciesReturns the known dependencies for this type. If this has dependencies whose values are not known statically, a dependency for theInjectorwill be included in the returned set.- Specified by:
getDependenciesin interfaceHasDependencies- Returns:
- a possibly empty set
-
getKey
public Key<F> getKey()
Description copied from interface:AssistedInjectBindingReturns theKeyfor the factory binding.- Specified by:
getKeyin interfaceAssistedInjectBinding<F>
-
getAssistedMethods
public java.util.Collection<AssistedMethod> getAssistedMethods()
Description copied from interface:AssistedInjectBindingReturns anAssistedMethodfor each method in the factory.- Specified by:
getAssistedMethodsin interfaceAssistedInjectBinding<F>
-
acceptExtensionVisitor
public <T,V> V acceptExtensionVisitor(BindingTargetVisitor<T,V> visitor, ProviderInstanceBinding<? extends T> binding)
Description copied from interface:ProviderWithExtensionVisitorInstructs 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:
acceptExtensionVisitorin interfaceProviderWithExtensionVisitor<F>
-
validateFactoryReturnType
private void validateFactoryReturnType(Errors errors, java.lang.Class<?> returnType, java.lang.Class<?> factoryType)
-
isTypeNotSpecified
private boolean isTypeNotSpecified(TypeLiteral<?> typeLiteral, ConfigurationException ce)
Returns true if the ConfigurationException is due to an error of TypeLiteral not being fully specified.
-
findMatchingConstructorInjectionPoint
private <T> InjectionPoint findMatchingConstructorInjectionPoint(java.lang.reflect.Method method, Key<?> returnType, TypeLiteral<T> implementation, java.util.List<Key<?>> paramList) throws ErrorsException
Finds a constructor suitable for the method. If the implementation contained any constructors marked withAssistedInject, this requires allAssistedparameters to exactly match the parameters (in any order) listed in the method. Otherwise, if noAssistedInjectconstructors exist, this will default to looking for an @Injectconstructor.- Throws:
ErrorsException
-
constructorHasMatchingParams
private boolean constructorHasMatchingParams(TypeLiteral<?> type, java.lang.reflect.Constructor<?> constructor, java.util.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
private java.util.Set<Dependency<?>> getDependencies(InjectionPoint ctorPoint, TypeLiteral<?> implementation)
Calculates all dependencies required by the implementation and constructor.
-
removeAssistedDeps
private java.util.Set<Dependency<?>> removeAssistedDeps(java.util.Set<Dependency<?>> deps)
Return all non-assisted dependencies.
-
isValidForOptimizedAssistedInject
private boolean isValidForOptimizedAssistedInject(java.util.Set<Dependency<?>> dependencies, java.lang.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
private boolean isInjectorOrAssistedProvider(Dependency<?> dependency)
-
assistKey
private <T> Key<T> assistKey(java.lang.reflect.Method method, Key<T> key, Errors errors) throws ErrorsException
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
@Inject @Toolable void initialize(Injector injector)
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(java.lang.reflect.Method method, java.lang.Object[] args, FactoryProvider2.AssistData data)
Creates a child injector that binds the args, and returns the binding for the method's result.
-
invoke
public java.lang.Object invoke(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args) throws java.lang.ThrowableWhen 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:
invokein interfacejava.lang.reflect.InvocationHandler- Throws:
java.lang.Throwable
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
canRethrow
static boolean canRethrow(java.lang.reflect.Method invoked, java.lang.Throwable thrown)Returns true ifthrowncan be thrown byinvokedwithout wrapping.
-
-