|
28 | 28 | import java.lang.reflect.Modifier;
|
29 | 29 | import java.util.ArrayList;
|
30 | 30 | import java.util.Collection;
|
| 31 | +import java.util.Collections; |
31 | 32 | import java.util.HashMap;
|
32 | 33 | import java.util.HashSet;
|
33 | 34 | import java.util.List;
|
34 | 35 | import java.util.Map;
|
35 | 36 | import java.util.Optional;
|
36 | 37 | import java.util.Set;
|
37 | 38 | import java.util.function.Predicate;
|
| 39 | +import java.util.stream.Collectors; |
38 | 40 |
|
39 | 41 | import jakarta.enterprise.context.SessionScoped;
|
40 | 42 | import jakarta.enterprise.inject.Typed;
|
|
87 | 89 | import io.quarkus.deployment.builditem.ExtensionSslNativeSupportBuildItem;
|
88 | 90 | import io.quarkus.deployment.builditem.FeatureBuildItem;
|
89 | 91 | import io.quarkus.deployment.builditem.GeneratedClassBuildItem;
|
| 92 | +import io.quarkus.deployment.builditem.LaunchModeBuildItem; |
90 | 93 | import io.quarkus.deployment.builditem.RunTimeConfigBuilderBuildItem;
|
91 | 94 | import io.quarkus.deployment.builditem.ShutdownContextBuildItem;
|
92 | 95 | import io.quarkus.deployment.builditem.StaticInitConfigBuilderBuildItem;
|
@@ -119,7 +122,9 @@ class RestClientReactiveProcessor {
|
119 | 122 | private static final Logger log = Logger.getLogger(RestClientReactiveProcessor.class);
|
120 | 123 |
|
121 | 124 | private static final DotName REGISTER_REST_CLIENT = DotName.createSimple(RegisterRestClient.class.getName());
|
| 125 | + private static final DotName REST_CLIENT = DotName.createSimple(RestClient.class.getName()); |
122 | 126 | private static final DotName SESSION_SCOPED = DotName.createSimple(SessionScoped.class.getName());
|
| 127 | + private static final DotName INJECT_MOCK = DotName.createSimple("io.quarkus.test.InjectMock"); |
123 | 128 | private static final DotName KOTLIN_METADATA_ANNOTATION = DotName.createSimple("kotlin.Metadata");
|
124 | 129 |
|
125 | 130 | private static final String ENABLE_COMPRESSION = "quarkus.http.enable-compression";
|
@@ -473,11 +478,24 @@ void addRestClientBeans(Capabilities capabilities,
|
473 | 478 | BuildProducer<GeneratedBeanBuildItem> generatedBeans,
|
474 | 479 | RestClientReactiveConfig clientConfig,
|
475 | 480 | RestClientsBuildTimeConfig clientsBuildConfig,
|
| 481 | + LaunchModeBuildItem launchMode, |
476 | 482 | RestClientRecorder recorder,
|
477 | 483 | ShutdownContextBuildItem shutdown) {
|
478 | 484 |
|
479 | 485 | CompositeIndex index = CompositeIndex.create(combinedIndexBuildItem.getIndex());
|
480 | 486 |
|
| 487 | + Set<DotName> requestedRestClientMocks = Collections.emptySet(); |
| 488 | + if (launchMode.getLaunchMode() == LaunchMode.TEST) { |
| 489 | + // we need to determine which RestClient interfaces have been marked for mocking |
| 490 | + requestedRestClientMocks = combinedIndexBuildItem.getIndex().getAnnotations(INJECT_MOCK) |
| 491 | + .stream() |
| 492 | + .filter(ai -> ai.target().kind() == AnnotationTarget.Kind.FIELD) |
| 493 | + .map(ai -> ai.target().asField()) |
| 494 | + .filter(f -> f.hasAnnotation(REST_CLIENT)) |
| 495 | + .map(f -> f.type().name()) |
| 496 | + .collect(Collectors.toSet()); |
| 497 | + } |
| 498 | + |
481 | 499 | Map<String, String> configKeys = new HashMap<>();
|
482 | 500 | var annotationsStore = new AnnotationStore(index, restClientAnnotationsTransformerBuildItem.stream()
|
483 | 501 | .map(RestClientAnnotationsTransformerBuildItem::getAnnotationTransformation).toList());
|
@@ -552,14 +570,16 @@ void addRestClientBeans(Capabilities capabilities,
|
552 | 570 | Optional<String> baseUri = registerRestClient.getDefaultBaseUri();
|
553 | 571 |
|
554 | 572 | ResultHandle baseUriHandle = constructor.load(baseUri.isPresent() ? baseUri.get() : "");
|
| 573 | + boolean lazyDelegate = scope.getDotName().equals(REQUEST_SCOPED) |
| 574 | + || requestedRestClientMocks.contains(jaxrsInterface.name()); |
555 | 575 | constructor.invokeSpecialMethod(
|
556 | 576 | MethodDescriptor.ofConstructor(RestClientReactiveCDIWrapperBase.class, Class.class, String.class,
|
557 | 577 | String.class, boolean.class),
|
558 | 578 | constructor.getThis(),
|
559 | 579 | constructor.loadClassFromTCCL(jaxrsInterface.toString()),
|
560 | 580 | baseUriHandle,
|
561 | 581 | configKey.isPresent() ? constructor.load(configKey.get()) : constructor.loadNull(),
|
562 |
| - constructor.load(scope.getDotName().equals(REQUEST_SCOPED))); |
| 582 | + constructor.load(lazyDelegate)); |
563 | 583 | constructor.returnValue(null);
|
564 | 584 |
|
565 | 585 | // METHODS:
|
|
0 commit comments