Skip to content

Commit 5ecb6c5

Browse files
committed
fix factory and add test
1 parent ebf4b1e commit 5ecb6c5

File tree

9 files changed

+125
-9
lines changed

9 files changed

+125
-9
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ inject-generator/avaje-inject
1414
inject-generator/avaje-inject-generator
1515
inject-generator/avaje-processors.txt
1616
*.csv
17+
inject-generator/util/events/ListString$Publisher$DI.java
18+
inject-generator/util/events/ListString$Publisher.java
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.example.myapp.async;
2+
3+
import java.util.concurrent.atomic.AtomicBoolean;
4+
5+
import io.avaje.inject.AsyncBean;
6+
import io.avaje.inject.BeanScope;
7+
import io.avaje.inject.PostConstruct;
8+
import io.avaje.lang.Nullable;
9+
import jakarta.inject.Inject;
10+
import jakarta.inject.Named;
11+
import jakarta.inject.Singleton;
12+
13+
@AsyncBean
14+
@Singleton
15+
@Named("single")
16+
public class BackgroundBean {
17+
18+
final long initTime;
19+
20+
public BackgroundBean() throws InterruptedException {
21+
Thread.sleep(1000);
22+
this.initTime = System.currentTimeMillis();
23+
}
24+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.example.myapp.async;
2+
3+
import io.avaje.inject.AsyncBean;
4+
import io.avaje.inject.Bean;
5+
import io.avaje.inject.Factory;
6+
import jakarta.inject.Named;
7+
8+
@Factory
9+
@AsyncBean
10+
public class BackgroundBeanFactory {
11+
12+
@Bean
13+
@Named("factory")
14+
BackgroundBean lazyInt() throws InterruptedException {
15+
16+
return new BackgroundBean();
17+
}
18+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.example.myapp.async;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
import io.avaje.inject.BeanScope;
8+
9+
class AsyncTest {
10+
11+
@Test
12+
void test() {
13+
try (var scope = BeanScope.builder().build()) {
14+
var lazy = scope.get(BackgroundBean.class, "single");
15+
assertThat(lazy).isNotNull();
16+
17+
var lazyAgain = scope.get(BackgroundBean.class, "single");
18+
assertThat(lazyAgain).isSameAs(lazy);
19+
}
20+
}
21+
22+
@Test
23+
void testFactory() {
24+
try (var scope = BeanScope.builder().build()) {
25+
var prov = scope.get(BackgroundBean.class, "factory");
26+
assertThat(prov).isNotNull();
27+
}
28+
}
29+
}

inject-generator/src/main/java/io/avaje/inject/generator/MethodReader.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.Collections;
88
import java.util.List;
99
import java.util.Set;
10+
import java.util.concurrent.CompletableFuture;
1011

1112
import static io.avaje.inject.generator.Constants.CONDITIONAL_DEPENDENCY;
1213
import static io.avaje.inject.generator.ProcessingContext.asElement;
@@ -21,6 +22,7 @@ final class MethodReader {
2122
private final boolean prototype;
2223
private final boolean primary;
2324
private final boolean secondary;
25+
private final boolean async;
2426
private final boolean lazy;
2527
private final String returnTypeRaw;
2628
private final UType genericType;
@@ -49,9 +51,11 @@ final class MethodReader {
4951
prototype = PrototypePrism.isPresent(element);
5052
primary = PrimaryPrism.isPresent(element);
5153
secondary = SecondaryPrism.isPresent(element);
54+
async = AsyncBeanPrism.isPresent(element) || AsyncBeanPrism.isPresent(element.getEnclosingElement());
5255
lazy = LazyPrism.isPresent(element) || LazyPrism.isPresent(element.getEnclosingElement());
5356
conditions.readAll(element);
5457
} else {
58+
async = false;
5559
prototype = false;
5660
primary = false;
5761
secondary = false;
@@ -103,8 +107,8 @@ final class MethodReader {
103107
this.initMethod = lifecycleReader.initMethod();
104108
this.destroyMethod = lifecycleReader.destroyMethod();
105109
}
106-
if (lazy && prototype) {
107-
APContext.logError("Cannot use both @Lazy and @Prototype");
110+
if ((async||lazy) && prototype) {
111+
APContext.logError("Cannot use both @AsyncBean/@Lazy and @Prototype");
108112
}
109113
}
110114

@@ -230,7 +234,10 @@ void builderAddBeanProvider(Append writer) {
230234
writer.append(".asSecondary()");
231235
}
232236

233-
writer.indent(".registerProvider(() -> {").eol();
237+
writer
238+
.indent(".registerProvider(")
239+
.append("%s() -> {", async ? "CompletableFuture.supplyAsync(" : "")
240+
.eol();
234241

235242
startTry(writer, " ");
236243
writer.indent(indent).append(" return ");
@@ -243,7 +250,7 @@ void builderAddBeanProvider(Append writer) {
243250
}
244251
writer.append(");").eol();
245252
endTry(writer, " ");
246-
writer.indent(indent).append(" });").eol();
253+
writer.indent(indent).append(" }%s);", async ? ")::join" : "").eol();
247254
writer.indent(indent).append("}").eol();
248255
}
249256

@@ -340,6 +347,10 @@ void addImports(ImportTypeMap importTypes) {
340347
if (optionalType) {
341348
importTypes.add(Constants.OPTIONAL);
342349
}
350+
351+
if (async) {
352+
importTypes.add(CompletableFuture.class.getCanonicalName());
353+
}
343354
conditions.addImports(importTypes);
344355
}
345356

@@ -429,6 +440,10 @@ boolean isProtoType() {
429440
return prototype && !Util.isProvider(returnTypeRaw);
430441
}
431442

443+
boolean isAsync() {
444+
return async;
445+
}
446+
432447
boolean isLazy() {
433448
return lazy;
434449
}

inject-generator/src/main/java/io/avaje/inject/generator/SimpleBeanWriter.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ private void writeFactoryBeanMethod(MethodReader method) {
144144
method.buildConditional(writer);
145145
method.buildAddFor(writer);
146146
method.builderGetFactory(writer, beanReader.hasConditions());
147-
if (method.isLazy() || method.isProtoType() || method.isUseProviderForSecondary()) {
147+
if (method.isAsync() || method.isLazy() || method.isProtoType() || method.isUseProviderForSecondary()) {
148148
method.builderAddBeanProvider(writer);
149149
} else {
150150
method.startTry(writer);
@@ -198,11 +198,21 @@ private void writeAddFor(MethodReader constructor) {
198198
if (beanReader.registerProvider()) {
199199
beanReader.prototypePostConstruct(writer, indent);
200200
writer.indent(" return bean;").eol();
201-
writer.indent(" })").append("%s;", beanReader.async() ? "::join)" : "").eol();
201+
if (!constructor.methodThrows()) {
202+
writer.indent(" }").append(beanReader.async() ? ")::join);" : ");").eol();
203+
}
202204
}
203205
writeObserveMethods();
204206
constructor.endTry(writer);
205-
writer.append(" }").eol();
207+
208+
writer.append(" }");
209+
210+
if (beanReader.registerProvider() && constructor.methodThrows()) {
211+
writer.append("%s);", beanReader.async() ? ")::join" : "").eol();
212+
writer.append(" }");
213+
}
214+
215+
writer.eol();
206216
}
207217

208218
private void writeBuildMethodStart() {

inject-generator/src/test/java/io/avaje/inject/generator/models/valid/lazy/BackgroundBean.java renamed to inject-generator/src/test/java/io/avaje/inject/generator/models/valid/async/BackgroundBean.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.avaje.inject.generator.models.valid.lazy;
1+
package io.avaje.inject.generator.models.valid.async;
22

33
import io.avaje.inject.AsyncBean;
44
import jakarta.inject.Inject;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.avaje.inject.generator.models.valid.async;
2+
3+
import io.avaje.inject.AsyncBean;
4+
import io.avaje.inject.Bean;
5+
import io.avaje.inject.Factory;
6+
import jakarta.inject.Named;
7+
8+
@Factory
9+
@AsyncBean
10+
public class BackgroundBeanFactory {
11+
12+
@Bean
13+
@Named("factory")
14+
BackgroundBean lazyInt() throws InterruptedException {
15+
16+
return new BackgroundBean();
17+
}
18+
}

inject-generator/src/test/java/io/avaje/inject/generator/models/valid/lazy/LazyFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
public class LazyFactory {
1010

1111
@Bean
12-
Integer lazyInt() {
12+
Integer lazyInt() throws Exception {
1313
return 0;
1414
}
1515
}

0 commit comments

Comments
 (0)