final class FactoryProvider2<F> extends java.lang.Object implements java.lang.reflect.InvocationHandler, ProviderWithExtensionVisitor<F>, HasDependencies, AssistedInjectBinding<F>
Modifier and Type | Class and Description |
---|---|
private static class |
FactoryProvider2.AssistData
All the data necessary to perform an assisted inject.
|
private static class |
FactoryProvider2.MethodHandleWrapper
Wrapper around MethodHandles/MethodHandle, so we can compile+run on java6.
|
private static class |
FactoryProvider2.ThreadLocalProvider |
Modifier and Type | Field and Description |
---|---|
private 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.
|
private BindingCollector |
collector
The binding collector, for equality/hashing purposes.
|
(package private) static Assisted |
DEFAULT_ANNOTATION
if a factory method parameter isn't annotated, it gets this annotation.
|
private F |
factory
the factory interface, implemented and provided
|
private Key<F> |
factoryKey
The key that this is bound to.
|
private Injector |
injector
the hosting injector, or null if we haven't been initialized yet
|
(package private) static java.util.logging.Logger |
logger |
private com.google.common.collect.ImmutableMap<java.lang.reflect.Method,FactoryProvider2.MethodHandleWrapper> |
methodHandleByMethod
Mapping from method to method handle, for generated default methods.
|
(package private) static java.lang.annotation.Annotation |
RETURN_ANNOTATION
A constant annotation to denote the return value, instead of creating a new one each time.
|
Constructor and Description |
---|
FactoryProvider2(Key<F> factoryKey,
BindingCollector collector) |
Modifier and Type | Method and Description |
---|---|
<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> |
assistKey(java.lang.reflect.Method method,
Key<T> key,
Errors errors)
Returns a key similar to
key , but with an @Assisted binding annotation. |
(package private) static boolean |
canRethrow(java.lang.reflect.Method invoked,
java.lang.Throwable thrown)
Returns true if
thrown can be thrown by invoked without wrapping. |
private boolean |
constructorHasMatchingParams(TypeLiteral<?> type,
java.lang.reflect.Constructor<?> constructor,
java.util.List<Key<?>> paramList,
Errors errors)
Matching logic for constructors annotated with AssistedInject.
|
boolean |
equals(java.lang.Object obj) |
private <T> InjectionPoint |
findMatchingConstructorInjectionPoint(java.lang.reflect.Method method,
Key<?> returnType,
TypeLiteral<T> implementation,
java.util.List<Key<?>> paramList)
Finds a constructor suitable for the method.
|
F |
get()
Provides an instance of
T . |
java.util.Collection<AssistedMethod> |
getAssistedMethods()
Returns an
AssistedMethod for 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 the
Key for the factory binding. |
int |
hashCode() |
(package private) void |
initialize(Injector injector)
At injector-creation time, we initialize the invocation handler.
|
java.lang.Object |
invoke(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 boolean |
isCompatible(java.lang.reflect.Method src,
java.lang.reflect.Method dst) |
(package private) static boolean |
isDefault(java.lang.reflect.Method method) |
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(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.String |
toString() |
private void |
validateFactoryReturnType(Errors errors,
java.lang.Class<?> returnType,
java.lang.Class<?> factoryType) |
static final java.lang.annotation.Annotation RETURN_ANNOTATION
static final java.util.logging.Logger logger
static final Assisted DEFAULT_ANNOTATION
private final com.google.common.collect.ImmutableMap<java.lang.reflect.Method,FactoryProvider2.AssistData> assistDataByMethod
private final com.google.common.collect.ImmutableMap<java.lang.reflect.Method,FactoryProvider2.MethodHandleWrapper> methodHandleByMethod
private Injector injector
private final F factory
private final BindingCollector collector
FactoryProvider2(Key<F> factoryKey, BindingCollector collector)
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.static boolean isDefault(java.lang.reflect.Method method)
private boolean isCompatible(java.lang.reflect.Method src, java.lang.reflect.Method dst)
public F get()
Provider
T
. Must never return null
.get
in interface javax.inject.Provider<F>
public java.util.Set<Dependency<?>> getDependencies()
HasDependencies
Injector
will be
included in the returned set.getDependencies
in interface HasDependencies
public Key<F> getKey()
AssistedInjectBinding
Key
for the factory binding.getKey
in interface AssistedInjectBinding<F>
public java.util.Collection<AssistedMethod> getAssistedMethods()
AssistedInjectBinding
AssistedMethod
for each method in the factory.getAssistedMethods
in interface AssistedInjectBinding<F>
public <T,V> V acceptExtensionVisitor(BindingTargetVisitor<T,V> visitor, ProviderInstanceBinding<? extends T> binding)
ProviderWithExtensionVisitor
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'.
acceptExtensionVisitor
in interface ProviderWithExtensionVisitor<F>
private void validateFactoryReturnType(Errors errors, java.lang.Class<?> returnType, java.lang.Class<?> factoryType)
private boolean isTypeNotSpecified(TypeLiteral<?> typeLiteral, ConfigurationException ce)
private <T> InjectionPoint findMatchingConstructorInjectionPoint(java.lang.reflect.Method method, Key<?> returnType, TypeLiteral<T> implementation, java.util.List<Key<?>> paramList) throws ErrorsException
AssistedInject
, this requires all Assisted
parameters to exactly
match the parameters (in any order) listed in the method. Otherwise, if no
AssistedInject
constructors exist, this will default to looking for an
@Inject
constructor.ErrorsException
private boolean constructorHasMatchingParams(TypeLiteral<?> type, java.lang.reflect.Constructor<?> constructor, java.util.List<Key<?>> paramList, Errors errors) throws ErrorsException
ErrorsException
private java.util.Set<Dependency<?>> getDependencies(InjectionPoint ctorPoint, TypeLiteral<?> implementation)
private java.util.Set<Dependency<?>> removeAssistedDeps(java.util.Set<Dependency<?>> deps)
private boolean isValidForOptimizedAssistedInject(java.util.Set<Dependency<?>> dependencies, java.lang.Class<?> implementation, TypeLiteral<?> factoryType)
private boolean isInjectorOrAssistedProvider(Dependency<?> dependency)
private <T> Key<T> assistKey(java.lang.reflect.Method method, Key<T> key, Errors errors) throws ErrorsException
key
, 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.ErrorsException
@Inject @Toolable void initialize(Injector injector)
public Binding<?> getBindingFromNewInjector(java.lang.reflect.Method method, java.lang.Object[] args, FactoryProvider2.AssistData data)
public java.lang.Object invoke(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args) throws java.lang.Throwable
invoke
in interface java.lang.reflect.InvocationHandler
java.lang.Throwable
public java.lang.String toString()
toString
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
static boolean canRethrow(java.lang.reflect.Method invoked, java.lang.Throwable thrown)
thrown
can be thrown by invoked
without wrapping.