Class DefaultParameterContext
- java.lang.Object
-
- org.junit.jupiter.engine.execution.DefaultParameterContext
-
- All Implemented Interfaces:
ParameterContext
class DefaultParameterContext extends java.lang.Object implements ParameterContext
- Since:
- 5.0
-
-
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 ofannotationTypethat is either present or meta-present on theParameterfor this context.<A extends java.lang.annotation.Annotation>
java.util.List<A>findRepeatableAnnotations(java.lang.Class<A> annotationType)Find all repeatable annotations ofannotationTypethat are either present or meta-present on theParameterfor this context.private java.lang.reflect.AnnotatedElementgetEffectiveAnnotatedParameter()Due to a bug injavacon JDK versions prior to JDK 9, looking up annotations directly on aParameterwill fail for inner class constructors.intgetIndex()Get the index of theParameterfor this context within the parameter list of theExecutablethat declares the parameter.java.lang.reflect.ParametergetParameter()Get theParameterfor this context.java.util.Optional<java.lang.Object>getTarget()Get the target on which theExecutablethat declares theParameterfor this context will be invoked, if available.booleanisAnnotated(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)Determine if an annotation ofannotationTypeis either present or meta-present on theParameterfor this context.java.lang.StringtoString()-
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
-
-
-
-
Method Detail
-
getParameter
public java.lang.reflect.Parameter getParameter()
Description copied from interface:ParameterContextGet theParameterfor 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 theParameterAPI due to a bug injavacon JDK versions prior to JDK 9.- Specified by:
getParameterin interfaceParameterContext- Returns:
- the parameter; never
null - See Also:
ParameterContext.getIndex()
-
getIndex
public int getIndex()
Description copied from interface:ParameterContextGet the index of theParameterfor this context within the parameter list of theExecutablethat declares the parameter.- Specified by:
getIndexin interfaceParameterContext- Returns:
- the index of the parameter
- See Also:
ParameterContext.getParameter(),Executable.getParameters()
-
getTarget
public java.util.Optional<java.lang.Object> getTarget()
Description copied from interface:ParameterContextGet the target on which theExecutablethat declares theParameterfor this context will be invoked, if available.- Specified by:
getTargetin interfaceParameterContext- Returns:
- an
Optionalcontaining the target on which theExecutablewill be invoked; nevernullbut will be empty if theExecutableis a constructor or astaticmethod.
-
isAnnotated
public boolean isAnnotated(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
Description copied from interface:ParameterContextDetermine if an annotation ofannotationTypeis either present or meta-present on theParameterfor this context.WARNING
Favor the use of this method over directly invoking
AnnotatedElement.isAnnotationPresent(Class)due to a bug injavacon JDK versions prior to JDK 9.- Specified by:
isAnnotatedin interfaceParameterContext- Parameters:
annotationType- the annotation type to search for; nevernull- Returns:
trueif 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:ParameterContextFind the first annotation ofannotationTypethat is either present or meta-present on theParameterfor this context.WARNING
Favor the use of this method over directly invoking annotation lookup methods in the
ParameterAPI due to a bug injavacon JDK versions prior to JDK 9.- Specified by:
findAnnotationin interfaceParameterContext- Type Parameters:
A- the annotation type- Parameters:
annotationType- the annotation type to search for; nevernull- Returns:
- an
Optionalcontaining the annotation; nevernullbut 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:ParameterContextFind all repeatable annotations ofannotationTypethat are either present or meta-present on theParameterfor this context.WARNING
Favor the use of this method over directly invoking annotation lookup methods in the
ParameterAPI due to a bug injavacon JDK versions prior to JDK 9.- Specified by:
findRepeatableAnnotationsin 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
nullnor mutable, but potentially empty - See Also:
ParameterContext.isAnnotated(Class),ParameterContext.findAnnotation(Class),Repeatable
-
getEffectiveAnnotatedParameter
private java.lang.reflect.AnnotatedElement getEffectiveAnnotatedParameter()
Due to a bug injavacon JDK versions prior to JDK 9, looking up annotations directly on aParameterwill fail for inner class constructors.Bug in
javacon 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
Parameterobject (i.e.,index - 1). The current index must never be zero in such situations since JUnit Jupiter should never ask aParameterResolverto resolve a parameter for the implicit enclosing instance parameter.WARNING
The
AnnotatedElementreturned by this method should never be cast and treated as aParametersince 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
Parameterfor this context, or the effectiveParameterif the aforementioned bug is detected
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-