Class DefaultParameterContext

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private int index  
      private java.lang.reflect.Parameter parameter  
      private java.util.Optional<java.lang.Object> target  
    • Constructor Summary

      Constructors 
      Constructor Description
      DefaultParameterContext​(java.lang.reflect.Parameter parameter, int index, java.util.Optional<java.lang.Object> target)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      <A extends java.lang.annotation.Annotation>
      java.util.Optional<A>
      findAnnotation​(java.lang.Class<A> annotationType)
      Find the first annotation of annotationType that is either present or meta-present on the Parameter for this context.
      <A extends java.lang.annotation.Annotation>
      java.util.List<A>
      findRepeatableAnnotations​(java.lang.Class<A> annotationType)
      Find all repeatable annotations of annotationType that are either present or meta-present on the Parameter for this context.
      private java.lang.reflect.AnnotatedElement getEffectiveAnnotatedParameter()
      Due to a bug in javac on JDK versions prior to JDK 9, looking up annotations directly on a Parameter will fail for inner class constructors.
      int getIndex()
      Get the index of the Parameter for this context within the parameter list of the Executable that declares the parameter.
      java.lang.reflect.Parameter getParameter()
      Get the Parameter for this context.
      java.util.Optional<java.lang.Object> getTarget()
      Get the target on which the Executable that declares the Parameter for this context will be invoked, if available.
      boolean isAnnotated​(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
      Determine if an annotation of annotationType is either present or meta-present on the Parameter for this context.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • parameter

        private final java.lang.reflect.Parameter parameter
      • index

        private final int index
      • target

        private final java.util.Optional<java.lang.Object> target
    • Constructor Detail

      • DefaultParameterContext

        DefaultParameterContext​(java.lang.reflect.Parameter parameter,
                                int index,
                                java.util.Optional<java.lang.Object> target)
    • Method Detail

      • getTarget

        public java.util.Optional<java.lang.Object> getTarget()
        Description copied from interface: ParameterContext
        Get the target on which the Executable that declares the Parameter for this context will be invoked, if available.
        Specified by:
        getTarget in interface ParameterContext
        Returns:
        an Optional containing the target on which the Executable will be invoked; never null but will be empty if the Executable is a constructor or a static method.
      • isAnnotated

        public boolean isAnnotated​(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
        Description copied from interface: ParameterContext
        Determine if an annotation of annotationType is either present or meta-present on the Parameter for this context.

        WARNING

        Favor the use of this method over directly invoking AnnotatedElement.isAnnotationPresent(Class) due to a bug in javac on JDK versions prior to JDK 9.

        Specified by:
        isAnnotated in interface ParameterContext
        Parameters:
        annotationType - the annotation type to search for; never null
        Returns:
        true if the annotation is present or meta-present
        See Also:
        ParameterContext.findAnnotation(Class), ParameterContext.findRepeatableAnnotations(Class)
      • findAnnotation

        public <A extends java.lang.annotation.Annotation> java.util.Optional<A> findAnnotation​(java.lang.Class<A> annotationType)
        Description copied from interface: ParameterContext
        Find the first annotation of annotationType that is either present or meta-present on the Parameter for this context.

        WARNING

        Favor the use of this method over directly invoking annotation lookup methods in the Parameter API due to a bug in javac on JDK versions prior to JDK 9.

        Specified by:
        findAnnotation in interface ParameterContext
        Type Parameters:
        A - the annotation type
        Parameters:
        annotationType - the annotation type to search for; never null
        Returns:
        an Optional containing the annotation; never null but potentially empty
        See Also:
        ParameterContext.isAnnotated(Class), ParameterContext.findRepeatableAnnotations(Class)
      • findRepeatableAnnotations

        public <A extends java.lang.annotation.Annotation> java.util.List<A> findRepeatableAnnotations​(java.lang.Class<A> annotationType)
        Description copied from interface: ParameterContext
        Find all repeatable annotations of annotationType that are either present or meta-present on the Parameter for this context.

        WARNING

        Favor the use of this method over directly invoking annotation lookup methods in the Parameter API due to a bug in javac on JDK versions prior to JDK 9.

        Specified by:
        findRepeatableAnnotations in interface ParameterContext
        Type Parameters:
        A - the annotation type
        Parameters:
        annotationType - the repeatable annotation type to search for; never null
        Returns:
        the list of all such annotations found; neither null nor mutable, but potentially empty
        See Also:
        ParameterContext.isAnnotated(Class), ParameterContext.findAnnotation(Class), Repeatable
      • getEffectiveAnnotatedParameter

        private java.lang.reflect.AnnotatedElement getEffectiveAnnotatedParameter()
        Due to a bug in javac on JDK versions prior to JDK 9, looking up annotations directly on a Parameter will fail for inner class constructors.

        Bug in javac on JDK versions prior to JDK 9

        The parameter annotations array in the compiled byte code for the user's test class excludes an entry for the implicit enclosing instance parameter for an inner class constructor.

        Workaround

        JUnit provides a workaround for this off-by-one error by helping extension authors to access annotations on the preceding Parameter object (i.e., index - 1). The current index must never be zero in such situations since JUnit Jupiter should never ask a ParameterResolver to resolve a parameter for the implicit enclosing instance parameter.

        WARNING

        The AnnotatedElement returned by this method should never be cast and treated as a Parameter since the metadata (e.g., Parameter.getName(), Parameter.getType(), etc.) will not match those for the declared parameter at the given index in an inner class constructor.

        Returns:
        the actual Parameter for this context, or the effective Parameter if the aforementioned bug is detected
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object