Class InjectionPoint

java.lang.Object
com.google.inject.spi.InjectionPoint

public final class InjectionPoint extends 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
  • Field Details

    • logger

      private static final Logger logger
    • optional

      private final boolean optional
    • member

      private final Member member
    • declaringType

      private final TypeLiteral<?> declaringType
    • dependencies

      private final com.google.common.collect.ImmutableList<Dependency<?>> dependencies
  • Constructor Details

    • InjectionPoint

      InjectionPoint(TypeLiteral<?> declaringType, Method method, boolean optional)
    • InjectionPoint

      InjectionPoint(TypeLiteral<?> declaringType, Constructor<?> constructor)
    • InjectionPoint

      InjectionPoint(TypeLiteral<?> declaringType, Field field, boolean optional)
  • Method Details

    • forMember

      private com.google.common.collect.ImmutableList<Dependency<?>> forMember(Member member, TypeLiteral<?> type, Annotation[][] paramterAnnotations)
    • newDependency

      private <T> Dependency<T> newDependency(Key<T> key, boolean allowsNull, int parameterIndex)
    • getMember

      public Member getMember()
      Returns the injected constructor, field, or method.
    • getDependencies

      public 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's raw declaring class.
      Since:
      3.0
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • forConstructor

      public static <T> InjectionPoint forConstructor(Constructor<T> constructor)
      Returns a new injection point for the specified constructor. If the declaring type of constructor is parameterized (such as List<T>), prefer the overload that includes a type literal.
      Parameters:
      constructor - any single constructor present on type.
      Since:
      3.0
    • forConstructor

      public static <T> InjectionPoint forConstructor(Constructor<T> constructor, TypeLiteral<? extends T> type)
      Returns a new injection point for the specified constructor of type.
      Parameters:
      constructor - any single constructor present on type.
      type - the concrete type that defines constructor.
      Since:
      3.0
    • forConstructorOf

      public static InjectionPoint forConstructorOf(TypeLiteral<?> type)
      Returns a new injection point for the injectable constructor of type.
      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(Class<?> type)
      Returns a new injection point for the injectable constructor of type.
      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(Method method, TypeLiteral<T> type)
      Returns a new injection point for the specified method of type. This is useful for extensions that need to build dependency graphs from arbitrary methods.
      Parameters:
      method - any single method present on type.
      type - the concrete type that defines method.
      Since:
      4.0
    • forStaticMethodsAndFields

      public static Set<InjectionPoint> forStaticMethodsAndFields(TypeLiteral<?> type)
      Returns all static method and field injection points on type.
      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 on type, such as a field with multiple binding annotations. The exception's partial value is a Set<InjectionPoint> of the valid injection points.
    • forStaticMethodsAndFields

      public static Set<InjectionPoint> forStaticMethodsAndFields(Class<?> type)
      Returns all static method and field injection points on type.
      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 on type, such as a field with multiple binding annotations. The exception's partial value is a Set<InjectionPoint> of the valid injection points.
    • forInstanceMethodsAndFields

      public static Set<InjectionPoint> forInstanceMethodsAndFields(TypeLiteral<?> type)
      Returns all instance method and field injection points on type.
      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 on type, such as a field with multiple binding annotations. The exception's partial value is a Set<InjectionPoint> of the valid injection points.
    • forInstanceMethodsAndFields

      public static Set<InjectionPoint> forInstanceMethodsAndFields(Class<?> type)
      Returns all instance method and field injection points on type.
      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 on type, such as a field with multiple binding annotations. The exception's partial value is a Set<InjectionPoint> of the valid injection points.
    • checkForMisplacedBindingAnnotations

      private static boolean checkForMisplacedBindingAnnotations(Member member, Errors errors)
      Returns true if the binding annotation is in the wrong place.
    • getAtInject

      static Annotation getAtInject(AnnotatedElement member)
    • getInjectionPoints

      private static 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 members
      errors - used to record errors
    • getDeclaredFields

      private static Field[] getDeclaredFields(TypeLiteral<?> type)
    • getDeclaredMethods

      private static Method[] getDeclaredMethods(TypeLiteral<?> type)
    • isEligibleForInjection

      private static boolean isEligibleForInjection(Method method, boolean statics)
      Returns true if the method is eligible to be injected. This is different than 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.

    • isValidMethod

      private static boolean isValidMethod(InjectionPoint.InjectableMethod injectableMethod, Errors errors)
    • hierarchyFor

      private static List<TypeLiteral<?>> hierarchyFor(TypeLiteral<?> type)
    • overrides

      private static boolean overrides(Method a, 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.