Class InjectionPoint
- java.lang.Object
-
- com.google.inject.spi.InjectionPoint
-
public final class InjectionPoint extends java.lang.Object
A constructor, field or method that can receive injections. Typically this is a member with the @Inject
annotation. For non-private, no argument constructors, the member may omit the annotation.- Since:
- 2.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
InjectionPoint.InjectableField
(package private) static class
InjectionPoint.InjectableMember
Node in the doubly-linked list of injectable members (fields and methods).(package private) static class
InjectionPoint.InjectableMembers
Linked list of injectable members.(package private) static class
InjectionPoint.InjectableMethod
(package private) static class
InjectionPoint.OverrideIndex
Keeps track of injectable methods so we can remove methods that get overridden in O(1) time.(package private) static class
InjectionPoint.Position
Position in type hierarchy.(package private) static class
InjectionPoint.Signature
A method signature.
-
Field Summary
Fields Modifier and Type Field Description private TypeLiteral<?>
declaringType
private com.google.common.collect.ImmutableList<Dependency<?>>
dependencies
private static java.util.logging.Logger
logger
private java.lang.reflect.Member
member
private boolean
optional
-
Constructor Summary
Constructors Constructor Description InjectionPoint(TypeLiteral<?> declaringType, java.lang.reflect.Constructor<?> constructor)
InjectionPoint(TypeLiteral<?> declaringType, java.lang.reflect.Field field, boolean optional)
InjectionPoint(TypeLiteral<?> declaringType, java.lang.reflect.Method method, boolean optional)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static boolean
checkForMisplacedBindingAnnotations(java.lang.reflect.Member member, Errors errors)
Returns true if the binding annotation is in the wrong place.boolean
equals(java.lang.Object o)
static <T> InjectionPoint
forConstructor(java.lang.reflect.Constructor<T> constructor)
Returns a new injection point for the specified constructor.static <T> InjectionPoint
forConstructor(java.lang.reflect.Constructor<T> constructor, TypeLiteral<? extends T> type)
Returns a new injection point for the specified constructor oftype
.static InjectionPoint
forConstructorOf(TypeLiteral<?> type)
Returns a new injection point for the injectable constructor oftype
.static InjectionPoint
forConstructorOf(TypeLiteral<?> type, boolean atInjectRequired)
Returns a new injection point for the injectable constructor oftype
.static InjectionPoint
forConstructorOf(java.lang.Class<?> type)
Returns a new injection point for the injectable constructor oftype
.static java.util.Set<InjectionPoint>
forInstanceMethodsAndFields(TypeLiteral<?> type)
Returns all instance method and field injection points ontype
.static java.util.Set<InjectionPoint>
forInstanceMethodsAndFields(java.lang.Class<?> type)
Returns all instance method and field injection points ontype
.private com.google.common.collect.ImmutableList<Dependency<?>>
forMember(Errors errors, java.lang.reflect.Member member, TypeLiteral<?> type, java.lang.reflect.AnnotatedType[] annotatedTypes, java.lang.annotation.Annotation[][] parameterAnnotationsPerParameter, java.util.function.Predicate<java.lang.Integer> isParameterKotlinNullable)
static <T> InjectionPoint
forMethod(java.lang.reflect.Method method, TypeLiteral<T> type)
Returns a new injection point for the specified method oftype
.static java.util.Set<InjectionPoint>
forStaticMethodsAndFields(TypeLiteral<?> type)
Returns all static method and field injection points ontype
.static java.util.Set<InjectionPoint>
forStaticMethodsAndFields(java.lang.Class<?> type)
Returns all static method and field injection points ontype
.static java.lang.annotation.Annotation[]
getAnnotations(java.lang.reflect.Field field)
Returns all the annotations on a field.(package private) static java.lang.annotation.Annotation
getAtInject(java.lang.reflect.AnnotatedElement member)
private static java.lang.reflect.Field[]
getDeclaredFields(TypeLiteral<?> type)
private static java.lang.reflect.Method[]
getDeclaredMethods(TypeLiteral<?> type)
TypeLiteral<?>
getDeclaringType()
Returns the generic type that defines this injection point.java.util.List<Dependency<?>>
getDependencies()
Returns the dependencies for this injection point.private static java.util.Set<InjectionPoint>
getInjectionPoints(TypeLiteral<?> type, boolean statics, Errors errors)
Returns an ordered, immutable set of injection points for the given type.java.lang.reflect.Member
getMember()
Returns the injected constructor, field, or method.int
hashCode()
private static java.util.List<TypeLiteral<?>>
hierarchyFor(TypeLiteral<?> type)
private static boolean
isEligibleForInjection(java.lang.reflect.Method method, boolean statics)
Returns true if the method is eligible to be injected.boolean
isOptional()
Returns true if this injection point shall be skipped if the injector cannot resolve bindings for all required dependencies.boolean
isToolable()
Returns true if the element is annotated with @Toolable
.private static boolean
isValidMethod(InjectionPoint.InjectableMethod injectableMethod, Errors errors)
private <T> Dependency<T>
newDependency(Key<T> key, boolean allowsNull, int parameterIndex)
private static boolean
overrides(java.lang.reflect.Method a, java.lang.reflect.Method b)
Returns true if a overrides b.java.lang.String
toString()
-
-
-
Field Detail
-
logger
private static final java.util.logging.Logger logger
-
optional
private final boolean optional
-
member
private final java.lang.reflect.Member member
-
declaringType
private final TypeLiteral<?> declaringType
-
dependencies
private final com.google.common.collect.ImmutableList<Dependency<?>> dependencies
-
-
Constructor Detail
-
InjectionPoint
InjectionPoint(TypeLiteral<?> declaringType, java.lang.reflect.Method method, boolean optional)
-
InjectionPoint
InjectionPoint(TypeLiteral<?> declaringType, java.lang.reflect.Constructor<?> constructor)
-
InjectionPoint
InjectionPoint(TypeLiteral<?> declaringType, java.lang.reflect.Field field, boolean optional)
-
-
Method Detail
-
forMember
private com.google.common.collect.ImmutableList<Dependency<?>> forMember(Errors errors, java.lang.reflect.Member member, TypeLiteral<?> type, java.lang.reflect.AnnotatedType[] annotatedTypes, java.lang.annotation.Annotation[][] parameterAnnotationsPerParameter, java.util.function.Predicate<java.lang.Integer> isParameterKotlinNullable)
-
newDependency
private <T> Dependency<T> newDependency(Key<T> key, boolean allowsNull, int parameterIndex)
-
getMember
public java.lang.reflect.Member getMember()
Returns the injected constructor, field, or method.
-
getDependencies
public java.util.List<Dependency<?>> getDependencies()
Returns the dependencies for this injection point. If the injection point is for a method or constructor, the dependencies will correspond to that member's parameters. Field injection points always have a single dependency for the field itself.- Returns:
- a possibly-empty list
-
isOptional
public boolean isOptional()
Returns true if this injection point shall be skipped if the injector cannot resolve bindings for all required dependencies. Both explicit bindings (as specified in a module), and implicit bindings (@ImplementedBy
, default constructors etc.) may be used to satisfy optional injection points.
-
isToolable
public boolean isToolable()
Returns true if the element is annotated with @Toolable
.- Since:
- 3.0
-
getDeclaringType
public TypeLiteral<?> getDeclaringType()
Returns the generic type that defines this injection point. If the member exists on a parameterized type, the result will include more type information than the member'sraw declaring class
.- Since:
- 3.0
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
forConstructor
public static <T> InjectionPoint forConstructor(java.lang.reflect.Constructor<T> constructor)
Returns a new injection point for the specified constructor. If the declaring type ofconstructor
is parameterized (such asList<T>
), prefer the overload that includes a type literal.- Parameters:
constructor
- any single constructor present ontype
.- Since:
- 3.0
-
forConstructor
public static <T> InjectionPoint forConstructor(java.lang.reflect.Constructor<T> constructor, TypeLiteral<? extends T> type)
Returns a new injection point for the specified constructor oftype
.- Parameters:
constructor
- any single constructor present ontype
.type
- the concrete type that definesconstructor
.- Since:
- 3.0
-
forConstructorOf
public static InjectionPoint forConstructorOf(TypeLiteral<?> type)
Returns a new injection point for the injectable constructor oftype
.Either a
@Inject
annotated constructor or a non-private no arg constructor is required to be defined by the class corresponding totype
.- Parameters:
type
- a concrete type with exactly one constructor annotated @Inject
, or a no-arguments constructor that is not private.- Throws:
ConfigurationException
- if there is no injectable constructor, more than one injectable constructor, or if parameters of the injectable constructor are malformed, such as a parameter with multiple binding annotations.
-
forConstructorOf
public static InjectionPoint forConstructorOf(TypeLiteral<?> type, boolean atInjectRequired)
Returns a new injection point for the injectable constructor oftype
.If
atInjectRequired
is true, the constructor must be annotated with@Inject
. IfatInjectRequired
is false, either a@Inject
annotated constructor or a non-private no arg constructor is required to be defined by the class corresponding totype
.- Parameters:
type
- a concrete type with exactly one constructor annotated@Inject
, or a no-arguments constructor that is not private.atInjectRequired
- whether the constructor must be annotated withInject
.- Throws:
ConfigurationException
- if there is no injectable constructor, more than one injectable constructor, or if parameters of the injectable constructor are malformed, such as a parameter with multiple binding annotations.- Since:
- 5.0
-
forConstructorOf
public static InjectionPoint forConstructorOf(java.lang.Class<?> type)
Returns a new injection point for the injectable constructor oftype
.- Parameters:
type
- a concrete type with exactly one constructor annotated @Inject
, or a no-arguments constructor that is not private.- Throws:
ConfigurationException
- if there is no injectable constructor, more than one injectable constructor, or if parameters of the injectable constructor are malformed, such as a parameter with multiple binding annotations.
-
forMethod
public static <T> InjectionPoint forMethod(java.lang.reflect.Method method, TypeLiteral<T> type)
Returns a new injection point for the specified method oftype
. This is useful for extensions that need to build dependency graphs from arbitrary methods.- Parameters:
method
- any single method present ontype
.type
- the concrete type that definesmethod
.- Since:
- 4.0
-
forStaticMethodsAndFields
public static java.util.Set<InjectionPoint> forStaticMethodsAndFields(TypeLiteral<?> type)
Returns all static method and field injection points ontype
.- Returns:
- a possibly empty set of injection points. The set has a specified iteration order. All fields are returned and then all methods. Within the fields, supertype fields are returned before subtype fields. Similarly, supertype methods are returned before subtype methods.
- Throws:
ConfigurationException
- if there is a malformed injection point ontype
, such as a field with multiple binding annotations. The exception'spartial value
is aSet<InjectionPoint>
of the valid injection points.
-
forStaticMethodsAndFields
public static java.util.Set<InjectionPoint> forStaticMethodsAndFields(java.lang.Class<?> type)
Returns all static method and field injection points ontype
.- Returns:
- a possibly empty set of injection points. The set has a specified iteration order. All fields are returned and then all methods. Within the fields, supertype fields are returned before subtype fields. Similarly, supertype methods are returned before subtype methods.
- Throws:
ConfigurationException
- if there is a malformed injection point ontype
, such as a field with multiple binding annotations. The exception'spartial value
is aSet<InjectionPoint>
of the valid injection points.
-
forInstanceMethodsAndFields
public static java.util.Set<InjectionPoint> forInstanceMethodsAndFields(TypeLiteral<?> type)
Returns all instance method and field injection points ontype
.- Returns:
- a possibly empty set of injection points. The set has a specified iteration order. All fields are returned and then all methods. Within the fields, supertype fields are returned before subtype fields. Similarly, supertype methods are returned before subtype methods.
- Throws:
ConfigurationException
- if there is a malformed injection point ontype
, such as a field with multiple binding annotations. The exception'spartial value
is aSet<InjectionPoint>
of the valid injection points.
-
forInstanceMethodsAndFields
public static java.util.Set<InjectionPoint> forInstanceMethodsAndFields(java.lang.Class<?> type)
Returns all instance method and field injection points ontype
.- Returns:
- a possibly empty set of injection points. The set has a specified iteration order. All fields are returned and then all methods. Within the fields, supertype fields are returned before subtype fields. Similarly, supertype methods are returned before subtype methods.
- Throws:
ConfigurationException
- if there is a malformed injection point ontype
, such as a field with multiple binding annotations. The exception'spartial value
is aSet<InjectionPoint>
of the valid injection points.
-
checkForMisplacedBindingAnnotations
private static boolean checkForMisplacedBindingAnnotations(java.lang.reflect.Member member, Errors errors)
Returns true if the binding annotation is in the wrong place.
-
getAtInject
static java.lang.annotation.Annotation getAtInject(java.lang.reflect.AnnotatedElement member)
-
getInjectionPoints
private static java.util.Set<InjectionPoint> getInjectionPoints(TypeLiteral<?> type, boolean statics, Errors errors)
Returns an ordered, immutable set of injection points for the given type. Members in superclasses come before members in subclasses. Within a class, fields come before methods. Overridden methods are filtered out. The order of fields/methods within a class is consistent but undefined.- Parameters:
statics
- true is this method should return static members, false for instance memberserrors
- used to record errors
-
getDeclaredFields
private static java.lang.reflect.Field[] getDeclaredFields(TypeLiteral<?> type)
-
getDeclaredMethods
private static java.lang.reflect.Method[] getDeclaredMethods(TypeLiteral<?> type)
-
isEligibleForInjection
private static boolean isEligibleForInjection(java.lang.reflect.Method method, boolean statics)
Returns true if the method is eligible to be injected. This is different thanisValidMethod(com.google.inject.spi.InjectionPoint.InjectableMethod, com.google.inject.internal.Errors)
, because ineligibility will not drop a method from being injected if a superclass was eligible & valid. Bridge & synthetic methods are excluded from eligibility for two reasons:Prior to Java8, javac would generate these methods in subclasses without annotations, which means this would accidentally stop injecting a method annotated with
Inject
, since the spec says to stop injecting if a subclass isn't annotated with it.Starting at Java8, javac copies the annotations to the generated subclass method, except it leaves out the generic types. If this considered it a valid injectable method, this would eject the parent's overridden method that had the proper generic types, and would use invalid injectable parameters as a result.
The fix for both is simply to ignore these synthetic bridge methods.
-
isValidMethod
private static boolean isValidMethod(InjectionPoint.InjectableMethod injectableMethod, Errors errors)
-
hierarchyFor
private static java.util.List<TypeLiteral<?>> hierarchyFor(TypeLiteral<?> type)
-
overrides
private static boolean overrides(java.lang.reflect.Method a, java.lang.reflect.Method b)
Returns true if a overrides b. Assumes signatures of a and b are the same and a's declaring class is a subclass of b's declaring class.
-
getAnnotations
public static java.lang.annotation.Annotation[] getAnnotations(java.lang.reflect.Field field)
Returns all the annotations on a field. If Kotlin-support is enabled, the annotations will include annotations on the related Kotlin-property.- Since:
- 5.0
-
-