Class DefaultParameterContext
- All Implemented Interfaces:
ParameterContext
- Since:
- 5.0
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionDefaultParameterContext
(Parameter parameter, int index, Optional<Object> target) -
Method Summary
Modifier and TypeMethodDescription<A extends Annotation>
Optional<A> findAnnotation
(Class<A> annotationType) Find the first annotation ofannotationType
that is either present or meta-present on theParameter
for this context.<A extends Annotation>
List<A> findRepeatableAnnotations
(Class<A> annotationType) Find all repeatable annotations ofannotationType
that are either present or meta-present on theParameter
for this context.private AnnotatedElement
Due to a bug injavac
on JDK versions prior to JDK 9, looking up annotations directly on aParameter
will fail for inner class constructors.int
getIndex()
Get the index of theParameter
for this context within the parameter list of theExecutable
that declares the parameter.Get theParameter
for this context.Get the target on which theExecutable
that declares theParameter
for this context will be invoked, if available.boolean
isAnnotated
(Class<? extends Annotation> annotationType) Determine if an annotation ofannotationType
is either present or meta-present on theParameter
for this context.toString()
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.junit.jupiter.api.extension.ParameterContext
getDeclaringExecutable
-
Field Details
-
parameter
-
index
private final int index -
target
-
-
Constructor Details
-
DefaultParameterContext
-
-
Method Details
-
getParameter
Description copied from interface:ParameterContext
Get theParameter
for this context.WARNING
When searching for annotations on the parameter in this context, favor
ParameterContext.isAnnotated(Class)
,ParameterContext.findAnnotation(Class)
, andParameterContext.findRepeatableAnnotations(Class)
over methods in theParameter
API due to a bug injavac
on JDK versions prior to JDK 9.- Specified by:
getParameter
in interfaceParameterContext
- Returns:
- the parameter; never
null
- See Also:
-
getIndex
public int getIndex()Description copied from interface:ParameterContext
Get the index of theParameter
for this context within the parameter list of theExecutable
that declares the parameter.- Specified by:
getIndex
in interfaceParameterContext
- Returns:
- the index of the parameter
- See Also:
-
getTarget
Description copied from interface:ParameterContext
Get the target on which theExecutable
that declares theParameter
for this context will be invoked, if available.- Specified by:
getTarget
in interfaceParameterContext
- Returns:
- an
Optional
containing the target on which theExecutable
will be invoked; nevernull
but will be empty if theExecutable
is a constructor or astatic
method.
-
isAnnotated
Description copied from interface:ParameterContext
Determine if an annotation ofannotationType
is either present or meta-present on theParameter
for this context.WARNING
Favor the use of this method over directly invoking
AnnotatedElement.isAnnotationPresent(Class)
due to a bug injavac
on JDK versions prior to JDK 9.- Specified by:
isAnnotated
in interfaceParameterContext
- Parameters:
annotationType
- the annotation type to search for; nevernull
- Returns:
true
if the annotation is present or meta-present- See Also:
-
findAnnotation
Description copied from interface:ParameterContext
Find the first annotation ofannotationType
that is either present or meta-present on theParameter
for this context.WARNING
Favor the use of this method over directly invoking annotation lookup methods in the
Parameter
API due to a bug injavac
on JDK versions prior to JDK 9.- Specified by:
findAnnotation
in interfaceParameterContext
- Type Parameters:
A
- the annotation type- Parameters:
annotationType
- the annotation type to search for; nevernull
- Returns:
- an
Optional
containing the annotation; nevernull
but potentially empty - See Also:
-
findRepeatableAnnotations
Description copied from interface:ParameterContext
Find all repeatable annotations ofannotationType
that are either present or meta-present on theParameter
for this context.WARNING
Favor the use of this method over directly invoking annotation lookup methods in the
Parameter
API due to a bug injavac
on JDK versions prior to JDK 9.- Specified by:
findRepeatableAnnotations
in interfaceParameterContext
- Type Parameters:
A
- the annotation type- Parameters:
annotationType
- the repeatable annotation type to search for; nevernull
- Returns:
- the list of all such annotations found; neither
null
nor mutable, but potentially empty - See Also:
-
getEffectiveAnnotatedParameter
Due to a bug injavac
on JDK versions prior to JDK 9, looking up annotations directly on aParameter
will fail for inner class constructors.Bug in
javac
on JDK versions prior to JDK 9The 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 aParameterResolver
to resolve a parameter for the implicit enclosing instance parameter.WARNING
The
AnnotatedElement
returned by this method should never be cast and treated as aParameter
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 effectiveParameter
if the aforementioned bug is detected
-
toString
-