Skip to content

Commit

Permalink
ArC: fix creation of synthetic beans
Browse files Browse the repository at this point in the history
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 b0b898a)
  • Loading branch information
Ladicek authored and gsmet committed Mar 15, 2024
1 parent 2ea8be9 commit 0835729
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ Set<AnnotationInstance> getQualifiers() {
return qualifiers;
}

String getIdentifier() {
return identifier;
}

Supplier<?> getSupplier() {
return supplier;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<MethodCreator> creator(String name, SyntheticBeanBuildItem bean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -98,10 +98,20 @@ public SynthBean test(String val) {
public void testBeans() {
List<InstanceHandle<SynthBean>> beans = Arc.container().listAll(SynthBean.class);
assertEquals(2, beans.size());
int countOk = 0;
int countNok = 0;
for (InstanceHandle<SynthBean> 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
Expand Down

0 comments on commit 0835729

Please sign in to comment.