Skip to content

Commit 9420e67

Browse files
committed
ArC: fix destruction of @dependent producers for static fields
`@Dependent` beans were already not created for `static` producer methods and fields, and they were also not destroyed for `static` producer methods. However, a destruction attempt _was_ performed for `static` producer fields, leading to a weird log message. This commit fixes that.
1 parent d93001f commit 9420e67

File tree

3 files changed

+74
-7
lines changed

3 files changed

+74
-7
lines changed

independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/BeanGenerator.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,11 +1294,10 @@ private void generateCreateForProducerField(ClassCreator cc, BeanInfo bean,
12941294
});
12951295
}
12961296

1297-
// If the declaring bean is @Dependent we must destroy the instance afterwards
1298-
// TODO what is the field is `static`? this seems useless then; see also `implementCreateForProducerMethod()`
1299-
if (BuiltinScope.DEPENDENT.is(bean.getDeclaringBean().getScope())) {
1300-
b0.invokeInterface(MethodDescs.INJECTABLE_BEAN_DESTROY, declaringProvider,
1301-
declaringProviderInstance, creationalContext);
1297+
// If the declaring bean is `@Dependent` and the producer is not `static`, we must destroy the instance afterwards
1298+
if (BuiltinScope.DEPENDENT.is(bean.getDeclaringBean().getScope()) && !isStatic) {
1299+
b0.invokeInterface(MethodDescs.INJECTABLE_BEAN_DESTROY, declaringProvider, declaringProviderInstance,
1300+
creationalContext);
13021301
}
13031302

13041303
b0.return_(instance);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package io.quarkus.arc.test.producer.staticProducers;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import jakarta.annotation.PostConstruct;
7+
import jakarta.annotation.PreDestroy;
8+
import jakarta.enterprise.context.Dependent;
9+
import jakarta.enterprise.inject.Produces;
10+
11+
import org.junit.jupiter.api.Test;
12+
import org.junit.jupiter.api.extension.RegisterExtension;
13+
14+
import io.quarkus.arc.Arc;
15+
import io.quarkus.arc.InstanceHandle;
16+
import io.quarkus.arc.test.ArcTestContainer;
17+
18+
/**
19+
* Tests static method/field producers on a `@Dependent` bean
20+
*/
21+
public class DependentStaticProducerTest {
22+
23+
@RegisterExtension
24+
public ArcTestContainer container = new ArcTestContainer(MyProducer.class);
25+
26+
@Test
27+
public void testStaticProducers() {
28+
assertEquals(0, MyProducer.creationCount);
29+
assertEquals(0, MyProducer.destructionCount);
30+
31+
// method producers
32+
InstanceHandle<Long> longMethod = Arc.container().instance(Long.class);
33+
assertTrue(longMethod.isAvailable());
34+
assertEquals(42L, longMethod.get());
35+
36+
// field producers
37+
InstanceHandle<String> stringField = Arc.container().instance(String.class);
38+
assertTrue(stringField.isAvailable());
39+
assertEquals("foobar", stringField.get());
40+
41+
assertEquals(0, MyProducer.creationCount);
42+
assertEquals(0, MyProducer.destructionCount);
43+
}
44+
45+
@Dependent
46+
static class MyProducer {
47+
static int creationCount = 0;
48+
static int destructionCount = 0;
49+
50+
@Produces
51+
static Long produceLong() {
52+
return 42L;
53+
}
54+
55+
@Produces
56+
static String stringField = "foobar";
57+
58+
@PostConstruct
59+
void create() {
60+
creationCount++;
61+
}
62+
63+
@PreDestroy
64+
void destroy() {
65+
destructionCount++;
66+
}
67+
}
68+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
/**
2222
* Tests static method/field producers
2323
*/
24-
public class StaticMethodProducerTest {
24+
public class StaticProducerTest {
2525

2626
@RegisterExtension
27-
public ArcTestContainer container = new ArcTestContainer(StaticMethodProducerTest.class, SomeProducer.class,
27+
public ArcTestContainer container = new ArcTestContainer(StaticProducerTest.class, SomeProducer.class,
2828
MyQualifier.class);
2929

3030
@Test

0 commit comments

Comments
 (0)