junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/ParameterResolver.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.STABLE; |
| 14 | |
| 15 | import java.lang.reflect.Parameter; |
| 16 | |
| 17 | import org.apiguardian.api.API; |
| 18 | |
| 19 | /** |
| 20 | * {@code ParameterResolver} defines the API for {@link Extension Extensions} |
| 21 | * that wish to dynamically resolve arguments for {@linkplain Parameter parameters} |
| 22 | * at runtime. |
| 23 | * |
| 24 | * <p>If a constructor for a test class or a |
| 25 | * {@link org.junit.jupiter.api.Test @Test}, |
| 26 | * {@link org.junit.jupiter.api.BeforeEach @BeforeEach}, |
| 27 | * {@link org.junit.jupiter.api.AfterEach @AfterEach}, |
| 28 | * {@link org.junit.jupiter.api.BeforeAll @BeforeAll}, or |
| 29 | * {@link org.junit.jupiter.api.AfterAll @AfterAll} method declares a parameter, |
| 30 | * an argument for the parameter must be resolved at runtime by a |
| 31 | * {@code ParameterResolver}. |
| 32 | * |
| 33 | * <h2>Constructor Requirements</h2> |
| 34 | * |
| 35 | * <p>Consult the documentation in {@link Extension} for details on |
| 36 | * constructor requirements. |
| 37 | * |
| 38 | * @since 5.0 |
| 39 | * @see #supportsParameter(ParameterContext, ExtensionContext) |
| 40 | * @see #resolveParameter(ParameterContext, ExtensionContext) |
| 41 | * @see ParameterContext |
| 42 | * @see TestInstanceFactory |
| 43 | * @see TestInstancePostProcessor |
| 44 | * @see TestInstancePreDestroyCallback |
| 45 | */ |
| 46 | @API(status = STABLE, since = "5.0") |
| 47 | public interface ParameterResolver extends Extension { |
| 48 | |
| 49 | /** |
| 50 | * Determine if this resolver supports resolution of an argument for the |
| 51 | * {@link Parameter} in the supplied {@link ParameterContext} for the supplied |
| 52 | * {@link ExtensionContext}. |
| 53 | * |
| 54 | * <p>The {@link java.lang.reflect.Method} or {@link java.lang.reflect.Constructor} |
| 55 | * in which the parameter is declared can be retrieved via |
| 56 | * {@link ParameterContext#getDeclaringExecutable()}. |
| 57 | * |
| 58 | * @param parameterContext the context for the parameter for which an argument should |
| 59 | * be resolved; never {@code null} |
| 60 | * @param extensionContext the extension context for the {@code Executable} |
| 61 | * about to be invoked; never {@code null} |
| 62 | * @return {@code true} if this resolver can resolve an argument for the parameter |
| 63 | * @see #resolveParameter |
| 64 | * @see ParameterContext |
| 65 | */ |
| 66 | boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) |
| 67 | throws ParameterResolutionException; |
| 68 | |
| 69 | /** |
| 70 | * Resolve an argument for the {@link Parameter} in the supplied {@link ParameterContext} |
| 71 | * for the supplied {@link ExtensionContext}. |
| 72 | * |
| 73 | * <p>This method is only called by the framework if {@link #supportsParameter} |
| 74 | * previously returned {@code true} for the same {@link ParameterContext} |
| 75 | * and {@link ExtensionContext}. |
| 76 | * |
| 77 | * <p>The {@link java.lang.reflect.Method} or {@link java.lang.reflect.Constructor} |
| 78 | * in which the parameter is declared can be retrieved via |
| 79 | * {@link ParameterContext#getDeclaringExecutable()}. |
| 80 | * |
| 81 | * @param parameterContext the context for the parameter for which an argument should |
| 82 | * be resolved; never {@code null} |
| 83 | * @param extensionContext the extension context for the {@code Executable} |
| 84 | * about to be invoked; never {@code null} |
| 85 | * @return the resolved argument for the parameter; may only be {@code null} if the |
| 86 | * parameter type is not a primitive |
| 87 | * @see #supportsParameter |
| 88 | * @see ParameterContext |
| 89 | */ |
| 90 | Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) |
| 91 | throws ParameterResolutionException; |
| 92 | |
| 93 | } |