From 3aecefb09afff6a9e8243070cd8e038419479ef8 Mon Sep 17 00:00:00 2001 From: Martin Kouba Date: Tue, 5 Mar 2024 10:04:17 +0100 Subject: [PATCH] ArC: fix BeanConfiguratorBase#read() - fixes #39169 --- .../SyntheticBeanBuildItemProxyTest.java | 42 +++++++++++++------ .../arc/processor/BeanConfiguratorBase.java | 1 + 2 files changed, 30 insertions(+), 13 deletions(-) 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 50275150a2162..6ce4b6c9d5477 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 @@ -1,10 +1,11 @@ package io.quarkus.arc.test.synthetic; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.lang.reflect.Method; +import java.util.List; import java.util.function.Consumer; import jakarta.enterprise.context.ApplicationScoped; @@ -14,6 +15,7 @@ import org.junit.jupiter.api.extension.RegisterExtension; import io.quarkus.arc.Arc; +import io.quarkus.arc.InstanceHandle; import io.quarkus.arc.deployment.SyntheticBeanBuildItem; import io.quarkus.arc.deployment.SyntheticBeanBuildItem.ExtendedBeanConfigurator; import io.quarkus.builder.BuildChainBuilder; @@ -46,20 +48,31 @@ public void execute(BuildContext context) { // We need to use reflection due to some class loading problems Object recorderProxy = bytecodeRecorder.getRecordingProxy(TestRecorder.class); try { - Method test = recorderProxy.getClass().getDeclaredMethod("test"); - Object proxy = test.invoke(recorderProxy); - ExtendedBeanConfigurator configurator = SyntheticBeanBuildItem.configure(SynthBean.class) + Method test = recorderProxy.getClass().getDeclaredMethod("test", String.class); + + Object proxy1 = test.invoke(recorderProxy, "ok"); + ExtendedBeanConfigurator configurator1 = SyntheticBeanBuildItem.configure(SynthBean.class) .scope(ApplicationScoped.class) + .identifier("ok") .unremovable(); // No creator assertThrows(IllegalStateException.class, - () -> configurator.done()); + () -> configurator1.done()); // Not a returned proxy assertThrows(IllegalArgumentException.class, - () -> configurator.runtimeProxy(new SynthBean())); - context.produce(configurator - .runtimeProxy(proxy) + () -> configurator1.runtimeProxy(new SynthBean())); + context.produce(configurator1 + .runtimeProxy(proxy1) + .done()); + + // Register a synthetic bean with same types and qualifiers but different identifier + context.produce(SyntheticBeanBuildItem.configure(SynthBean.class) + .scope(ApplicationScoped.class) + .identifier("nok") + .unremovable() + .runtimeProxy(test.invoke(recorderProxy, "nok")) .done()); + } catch (Exception e) { throw new RuntimeException(e); } @@ -73,9 +86,9 @@ public void execute(BuildContext context) { @Recorder public static class TestRecorder { - public SynthBean test() { + public SynthBean test(String val) { SynthBean bean = new SynthBean(); - bean.setValue("ok"); + bean.setValue(val); return bean; } @@ -83,9 +96,12 @@ public SynthBean test() { @Test public void testBeans() { - SynthBean bean = Arc.container().instance(SynthBean.class).get(); - assertNotNull(bean); - assertEquals("ok", bean.getValue()); + List> beans = Arc.container().listAll(SynthBean.class); + assertEquals(2, beans.size()); + for (InstanceHandle handle : beans) { + String val = handle.get().getValue(); + assertTrue("ok".equals(val) || "nok".equals(val)); + } } @Vetoed diff --git a/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/BeanConfiguratorBase.java b/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/BeanConfiguratorBase.java index 7f56930b8be3a..87cfaaca00bfd 100644 --- a/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/BeanConfiguratorBase.java +++ b/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/BeanConfiguratorBase.java @@ -72,6 +72,7 @@ protected BeanConfiguratorBase(DotName implClazz) { */ public THIS read(BeanConfiguratorBase base) { super.read(base); + identifier = base.identifier; types.clear(); types.addAll(base.types); qualifiers.clear();