public final class InjectionPoint
extends java.lang.Object
Inject
annotation. For non-private, no argument constructors, the member may
omit the annotation.Modifier and Type | Class and 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.
|
Modifier and Type | Field and 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 and 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) |
Modifier and Type | Method and 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 of
type . |
static InjectionPoint |
forConstructorOf(java.lang.Class<?> type)
Returns a new injection point for the injectable constructor of
type . |
static InjectionPoint |
forConstructorOf(TypeLiteral<?> type)
Returns a new injection point for the injectable constructor of
type . |
static java.util.Set<InjectionPoint> |
forInstanceMethodsAndFields(java.lang.Class<?> type)
Returns all instance method and field injection points on
type . |
static java.util.Set<InjectionPoint> |
forInstanceMethodsAndFields(TypeLiteral<?> type)
Returns all instance method and field injection points on
type . |
private com.google.common.collect.ImmutableList<Dependency<?>> |
forMember(java.lang.reflect.Member member,
TypeLiteral<?> type,
java.lang.annotation.Annotation[][] paramterAnnotations) |
static <T> InjectionPoint |
forMethod(java.lang.reflect.Method method,
TypeLiteral<T> type)
Returns a new injection point for the specified method of
type . |
static java.util.Set<InjectionPoint> |
forStaticMethodsAndFields(java.lang.Class<?> type)
Returns all static method and field injection points on
type . |
static java.util.Set<InjectionPoint> |
forStaticMethodsAndFields(TypeLiteral<?> type)
Returns all static method and field injection points on
type . |
(package private) static java.lang.annotation.Annotation |
getAtInject(java.lang.reflect.AnnotatedElement member) |
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() |
private static final java.util.logging.Logger logger
private final boolean optional
private final java.lang.reflect.Member member
private final TypeLiteral<?> declaringType
private final com.google.common.collect.ImmutableList<Dependency<?>> dependencies
InjectionPoint(TypeLiteral<?> declaringType, java.lang.reflect.Method method, boolean optional)
InjectionPoint(TypeLiteral<?> declaringType, java.lang.reflect.Constructor<?> constructor)
InjectionPoint(TypeLiteral<?> declaringType, java.lang.reflect.Field field, boolean optional)
private com.google.common.collect.ImmutableList<Dependency<?>> forMember(java.lang.reflect.Member member, TypeLiteral<?> type, java.lang.annotation.Annotation[][] paramterAnnotations)
private <T> Dependency<T> newDependency(Key<T> key, boolean allowsNull, int parameterIndex)
public java.lang.reflect.Member getMember()
public java.util.List<Dependency<?>> getDependencies()
public boolean isOptional()
ImplementedBy
, default
constructors etc.) may be used to satisfy optional injection points.public boolean isToolable()
Toolable
.public TypeLiteral<?> getDeclaringType()
raw declaring class
.public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object
public static <T> InjectionPoint forConstructor(java.lang.reflect.Constructor<T> constructor)
constructor
is parameterized (such as List<T>
), prefer the overload that includes a
type literal.constructor
- any single constructor present on type
.public static <T> InjectionPoint forConstructor(java.lang.reflect.Constructor<T> constructor, TypeLiteral<? extends T> type)
type
.constructor
- any single constructor present on type
.type
- the concrete type that defines constructor
.public static InjectionPoint forConstructorOf(TypeLiteral<?> type)
type
.type
- a concrete type with exactly one constructor annotated @Inject
,
or a no-arguments constructor that is not private.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.public static InjectionPoint forConstructorOf(java.lang.Class<?> type)
type
.type
- a concrete type with exactly one constructor annotated @Inject
,
or a no-arguments constructor that is not private.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.public static <T> InjectionPoint forMethod(java.lang.reflect.Method method, TypeLiteral<T> type)
type
.
This is useful for extensions that need to build dependency graphs from
arbitrary methods.method
- any single method present on type
.type
- the concrete type that defines method
.public static java.util.Set<InjectionPoint> forStaticMethodsAndFields(TypeLiteral<?> type)
type
.ConfigurationException
- if there is a malformed injection point on type
, such as
a field with multiple binding annotations. The exception's partial value
is a Set<InjectionPoint>
of the valid injection points.public static java.util.Set<InjectionPoint> forStaticMethodsAndFields(java.lang.Class<?> type)
type
.ConfigurationException
- if there is a malformed injection point on type
, such as
a field with multiple binding annotations. The exception's partial value
is a Set<InjectionPoint>
of the valid injection points.public static java.util.Set<InjectionPoint> forInstanceMethodsAndFields(TypeLiteral<?> type)
type
.ConfigurationException
- if there is a malformed injection point on type
, such as
a field with multiple binding annotations. The exception's partial value
is a Set<InjectionPoint>
of the valid injection points.public static java.util.Set<InjectionPoint> forInstanceMethodsAndFields(java.lang.Class<?> type)
type
.ConfigurationException
- if there is a malformed injection point on type
, such as
a field with multiple binding annotations. The exception's partial value
is a Set<InjectionPoint>
of the valid injection points.private static boolean checkForMisplacedBindingAnnotations(java.lang.reflect.Member member, Errors errors)
static java.lang.annotation.Annotation getAtInject(java.lang.reflect.AnnotatedElement member)
private static java.util.Set<InjectionPoint> getInjectionPoints(TypeLiteral<?> type, boolean statics, Errors errors)
statics
- true is this method should return static members, false for instance memberserrors
- used to record errorsprivate static boolean isEligibleForInjection(java.lang.reflect.Method method, boolean statics)
isValidMethod(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.
private static boolean isValidMethod(InjectionPoint.InjectableMethod injectableMethod, Errors errors)
private static java.util.List<TypeLiteral<?>> hierarchyFor(TypeLiteral<?> type)
private static boolean overrides(java.lang.reflect.Method a, java.lang.reflect.Method b)