Class InjectionTarget


  • public final class InjectionTarget
    extends Object
    A representation of an injection target. This can be a field or a parameter.

    When method isAnnotated(Class, boolean), findAnnotation(Class, boolean) or findRepeatableAnnotations(Class, boolean) is called with true for the includeDeclaringElements argument, the declaring elements are checked if the target itself is not annotated. The declaring elements are the constructor or method (for parameters only) and the class the field, constructor or method is declared in. If the class is a nested class, its declaring class is also checked; this continues until a top-level class is found.

    Author:
    Rob Spoor
    Since:
    2.0
    • Method Detail

      • isAnnotated

        public boolean isAnnotated​(Class<? extends Annotation> annotationType)
        Checks whether or not an annotation of a specific type is either present or meta-present on the injection target.
        Parameters:
        annotationType - The type to check.
        Returns:
        true if the an annotation of the given type is present or meta-present, or false otherwise.
      • isAnnotated

        public boolean isAnnotated​(Class<? extends Annotation> annotationType,
                                   boolean includeDeclaringElements)
        Checks whether or not an annotation of a specific type is either present or meta-present on the injection target.
        Parameters:
        annotationType - The type to check.
        includeDeclaringElements - If true, the injection targets declaring elements are checked if the target itself is not annotated.
        Returns:
        true if the an annotation of the given type is present or meta-present, or false otherwise.
      • findAnnotation

        public <A extends AnnotationOptional<A> findAnnotation​(Class<A> annotationType)
        Finds the first annotation of a specific type that is either present or meta-present on the injection target.
        Type Parameters:
        A - The type of annotation.
        Parameters:
        annotationType - The type to find an annotation for.
        Returns:
        An Optional describing the first annotation of the given type, or Optional.empty() if the annotation is not present.
      • findAnnotation

        public <A extends AnnotationOptional<A> findAnnotation​(Class<A> annotationType,
                                                                 boolean includeDeclaringElements)
        Finds the first annotation of a specific type that is either present or meta-present on the injection target.
        Type Parameters:
        A - The type of annotation.
        Parameters:
        annotationType - The type to find an annotation for.
        includeDeclaringElements - If true, the injection targets declaring elements are checked if the target itself is not annotated.
        Returns:
        An Optional describing the first annotation of the given type, or Optional.empty() if the annotation is not present.
      • findRepeatableAnnotations

        public <A extends AnnotationList<A> findRepeatableAnnotations​(Class<A> annotationType)
        Finds all repeatable annotations of a specific type that are either present or meta-present on the injection target.
        Type Parameters:
        A - The type of annotation.
        Parameters:
        annotationType - The type to find annotations for.
        Returns:
        A list with all annotations of the given type; possibly empty but never null.
      • findRepeatableAnnotations

        public <A extends AnnotationList<A> findRepeatableAnnotations​(Class<A> annotationType,
                                                                        boolean includeDeclaringElements)
        Finds all repeatable annotations of a specific type that are either present or meta-present on the injection target.
        Type Parameters:
        A - The type of annotation.
        Parameters:
        annotationType - The type to find annotations for.
        includeDeclaringElements - If true, the injection targets declaring elements are checked if the target itself is not annotated.
        Returns:
        A list with all annotations of the given type; possibly empty but never null.
      • createException

        public JUnitException createException​(String message)
        Creates a JUnitException with a message.
        Parameters:
        message - The message for the exception.
        Returns:
        The created exception.
      • createException

        public JUnitException createException​(String message,
                                              Throwable cause)
        Creates a JUnitException with a message and a cause.
        Parameters:
        message - The message for the exception.
        cause - The cause of the exception.
        Returns:
        The created exception.
      • forParameter

        public static InjectionTarget forParameter​(ParameterContext parameterContext)
        Creates an injection target for a constructor or method parameter.
        Parameters:
        parameterContext - The parameter context describing the parameter.
        Returns:
        The created injection target.
        Throws:
        NullPointerException - If the given parameter context is null.
      • forField

        public static InjectionTarget forField​(Field field)
        Creates an injection target for a field.
        Parameters:
        field - The field.
        Returns:
        The created injection target.
        Throws:
        NullPointerException - If the given parameter context is null.