junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/ParameterContext.java
raw symbols SCIP| 1 | /* |
| 2 | * Copyright 2015-2024 the original author or authors. |
| 3 | * |
| 4 | * All rights reserved. This program and the accompanying materials are |
| 5 | * made available under the terms of the Eclipse Public License v2.0 which |
| 6 | * accompanies this distribution and is available at |
| 7 | * |
| 8 | * https://www.eclipse.org/legal/epl-v20.html |
| 9 | */ |
| 10 | |
| 11 | package org.junit.jupiter.api.extension; |
| 12 | |
| 13 | import static org.apiguardian.api.API.Status.EXPERIMENTAL; |
| 14 | import static org.apiguardian.api.API.Status.STABLE; |
| 15 | |
| 16 | import java.lang.annotation.Annotation; |
| 17 | import java.lang.reflect.AnnotatedElement; |
| 18 | import java.lang.reflect.Executable; |
| 19 | import java.lang.reflect.Parameter; |
| 20 | import java.util.List; |
| 21 | import java.util.Optional; |
| 22 | |
| 23 | import org.apiguardian.api.API; |
| 24 | |
| 25 | /** |
| 26 | * {@code ParameterContext} encapsulates the <em>context</em> in which an |
| 27 | * {@link #getDeclaringExecutable Executable} will be invoked for a given |
| 28 | * {@link #getParameter Parameter}. |
| 29 | * |
| 30 | * <p>A {@code ParameterContext} is used to support parameter resolution via |
| 31 | * a {@link ParameterResolver}. |
| 32 | * |
| 33 | * @since 5.0 |
| 34 | * @see ParameterResolver |
| 35 | * @see java.lang.reflect.Parameter |
| 36 | * @see java.lang.reflect.Executable |
| 37 | * @see java.lang.reflect.Method |
| 38 | * @see java.lang.reflect.Constructor |
| 39 | */ |
| 40 | @API(status = STABLE, since = "5.0") |
| 41 | public interface ParameterContext extends AnnotatedElementContext { |
| 42 | |
| 43 | /** |
| 44 | * Get the {@link Parameter} for this context. |
| 45 | * |
| 46 | * <h4>WARNING</h4> |
| 47 | * <p>When searching for annotations on the parameter in this context, |
| 48 | * favor {@link #isAnnotated(Class)}, {@link #findAnnotation(Class)}, and |
| 49 | * {@link #findRepeatableAnnotations(Class)} over methods in the |
| 50 | * {@link Parameter} API due to a bug in {@code javac} on JDK versions prior |
| 51 | * to JDK 9. |
| 52 | * |
| 53 | * @return the parameter; never {@code null} |
| 54 | * @see #getIndex() |
| 55 | */ |
| 56 | Parameter getParameter(); |
| 57 | |
| 58 | /** |
| 59 | * Get the index of the {@link Parameter} for this context within the |
| 60 | * parameter list of the {@link #getDeclaringExecutable Executable} that |
| 61 | * declares the parameter. |
| 62 | * |
| 63 | * @return the index of the parameter |
| 64 | * @see #getParameter() |
| 65 | * @see Executable#getParameters() |
| 66 | */ |
| 67 | int getIndex(); |
| 68 | |
| 69 | /** |
| 70 | * Get the {@link Executable} (i.e., the {@link java.lang.reflect.Method} or |
| 71 | * {@link java.lang.reflect.Constructor}) that declares the {@code Parameter} |
| 72 | * for this context. |
| 73 | * |
| 74 | * @return the declaring {@code Executable}; never {@code null} |
| 75 | * @see Parameter#getDeclaringExecutable() |
| 76 | */ |
| 77 | default Executable getDeclaringExecutable() { |
| 78 | return getParameter().getDeclaringExecutable(); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Get the target on which the {@link #getDeclaringExecutable Executable} |
| 83 | * that declares the {@link #getParameter Parameter} for this context will |
| 84 | * be invoked, if available. |
| 85 | * |
| 86 | * @return an {@link Optional} containing the target on which the |
| 87 | * {@code Executable} will be invoked; never {@code null} but will be |
| 88 | * <em>empty</em> if the {@code Executable} is a constructor or a |
| 89 | * {@code static} method. |
| 90 | */ |
| 91 | Optional<Object> getTarget(); |
| 92 | |
| 93 | /** |
| 94 | * {@inheritDoc} |
| 95 | * @since 5.10 |
| 96 | */ |
| 97 | @API(status = EXPERIMENTAL, since = "5.10") |
| 98 | @Override |
| 99 | default AnnotatedElement getAnnotatedElement() { |
| 100 | return getParameter(); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * {@inheritDoc} |
| 105 | * @since 5.1.1 |
| 106 | */ |
| 107 | @API(status = STABLE, since = "5.10") |
| 108 | @Override |
| 109 | default boolean isAnnotated(Class<? extends Annotation> annotationType) { |
| 110 | return AnnotatedElementContext.super.isAnnotated(annotationType); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * {@inheritDoc} |
| 115 | * @since 5.1.1 |
| 116 | */ |
| 117 | @API(status = STABLE, since = "5.10") |
| 118 | @Override |
| 119 | default <A extends Annotation> Optional<A> findAnnotation(Class<A> annotationType) { |
| 120 | return AnnotatedElementContext.super.findAnnotation(annotationType); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * {@inheritDoc} |
| 125 | * @since 5.1.1 |
| 126 | */ |
| 127 | @API(status = STABLE, since = "5.10") |
| 128 | @Override |
| 129 | default <A extends Annotation> List<A> findRepeatableAnnotations(Class<A> annotationType) { |
| 130 | return AnnotatedElementContext.super.findRepeatableAnnotations(annotationType); |
| 131 | } |
| 132 | |
| 133 | } |