From 083572929085befd0af68bce30b1177483cc942b Mon Sep 17 00:00:00 2001 From: Ladislav Thon Date: Wed, 13 Mar 2024 12:40:37 +0100 Subject: [PATCH] ArC: fix creation of synthetic beans If a synthetic bean creation function is backed by a `Supplier`, `Function`, `RuntimeValue` or a runtime proxy, that creation function is stored into a `Map` during application startup and later retrieved when needed. The key under which it is stored into the `Map` is wrong, as it only contains the name of the implementation class and a hash of bean types and qualifiers. When there are multiple synthetic beans with the same implementation class, types and qualifiers, only one creation function is used for all of them. This commit fixes that by including the synthetic bean identifier in the hash, making creation function key unique for each synthetic bean. (cherry picked from commit b0b898aac5a918d5cc2eade0e7a460e4c5ba7a04) --- .../arc/deployment/SyntheticBeanBuildItem.java | 4 ++++ .../arc/deployment/SyntheticBeansProcessor.java | 3 ++- .../synthetic/SyntheticBeanBuildItemProxyTest.java | 14 ++++++++++++-- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/SyntheticBeanBuildItem.java b/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/SyntheticBeanBuildItem.java index 442dfd08b0728..3a8dd22f6fb18 100644 --- a/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/SyntheticBeanBuildItem.java +++ b/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/SyntheticBeanBuildItem.java @@ -193,6 +193,10 @@ Set getQualifiers() { return qualifiers; } + String getIdentifier() { + return identifier; + } + Supplier getSupplier() { return supplier; } diff --git a/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/SyntheticBeansProcessor.java b/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/SyntheticBeansProcessor.java index 747580b6728ef..0c105be1d1af3 100644 --- a/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/SyntheticBeansProcessor.java +++ b/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/SyntheticBeansProcessor.java @@ -94,7 +94,8 @@ private void configureSyntheticBean(ArcRecorder recorder, private String createName(ExtendedBeanConfigurator configurator) { return configurator.getImplClazz().toString().replace(".", "_") + "_" - + HashUtil.sha1(configurator.getTypes().toString() + configurator.getQualifiers().toString()); + + HashUtil.sha1(configurator.getTypes().toString() + configurator.getQualifiers().toString() + + (configurator.getIdentifier() != null ? configurator.getIdentifier() : "")); } private Consumer creator(String name, SyntheticBeanBuildItem bean) { diff --git a/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/synthetic/SyntheticBeanBuildItemProxyTest.java b/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/synthetic/SyntheticBeanBuildItemProxyTest.java index 6ce4b6c9d5477..2186b1e1faabf 100644 --- a/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/synthetic/SyntheticBeanBuildItemProxyTest.java +++ b/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/synthetic/SyntheticBeanBuildItemProxyTest.java @@ -2,7 +2,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; import java.lang.reflect.Method; import java.util.List; @@ -98,10 +98,20 @@ public SynthBean test(String val) { public void testBeans() { List> beans = Arc.container().listAll(SynthBean.class); assertEquals(2, beans.size()); + int countOk = 0; + int countNok = 0; for (InstanceHandle handle : beans) { String val = handle.get().getValue(); - assertTrue("ok".equals(val) || "nok".equals(val)); + if ("ok".equals(val)) { + countOk++; + } else if ("nok".equals(val)) { + countNok++; + } else { + fail("Expected 'ok' or 'nok'"); + } } + assertEquals(1, countOk); + assertEquals(1, countNok); } @Vetoed