Skip to content

Commit ebf4b1e

Browse files
committed
add an async bean generation
1 parent 76826a7 commit ebf4b1e

File tree

7 files changed

+55
-10
lines changed

7 files changed

+55
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ build/
1313
inject-generator/avaje-inject
1414
inject-generator/avaje-inject-generator
1515
inject-generator/avaje-processors.txt
16+
*.csv

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

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

1213
import javax.lang.model.element.Element;
@@ -38,6 +39,7 @@ final class BeanReader {
3839
private final boolean prototype;
3940
private final boolean primary;
4041
private final boolean secondary;
42+
private final boolean async;
4143
private final boolean lazy;
4244
private final boolean proxy;
4345
private final BeanAspects aspects;
@@ -58,6 +60,7 @@ final class BeanReader {
5860
this.primary = PrimaryPrism.isPresent(beanType);
5961
this.secondary = !primary && SecondaryPrism.isPresent(beanType);
6062
this.lazy = !FactoryPrism.isPresent(beanType) && LazyPrism.isPresent(beanType);
63+
this.async = !FactoryPrism.isPresent(beanType) && AsyncBeanPrism.isPresent(beanType);
6164
final var beantypes = BeanTypesPrism.getOptionalOn(beanType);
6265
beantypes.ifPresent(p -> Util.validateBeanTypes(beanType, p.value()));
6366
this.typeReader =
@@ -128,13 +131,17 @@ BeanAspects aspects() {
128131
}
129132

130133
boolean registerProvider() {
131-
return prototype || lazy;
134+
return prototype || async || lazy;
132135
}
133136

134137
boolean lazy() {
135138
return lazy;
136139
}
137140

141+
boolean async() {
142+
return async;
143+
}
144+
138145
boolean importedComponent() {
139146
return importedComponent;
140147
}
@@ -304,7 +311,7 @@ void buildAddFor(Append writer) {
304311
}
305312

306313
void buildRegister(Append writer) {
307-
if (prototype || lazy) {
314+
if (prototype || lazy || async) {
308315
return;
309316
}
310317
writer.indent(" ");
@@ -349,15 +356,20 @@ void prototypePostConstruct(Append writer, String indent) {
349356

350357
private void lifeCycleNotSupported(String lifecycle) {
351358
if (registerProvider()) {
352-
logError(
353-
beanType,
354-
"%s scoped bean does not support the %s lifecycle method",
355-
prototype ? "@Prototype" : "@Lazy",
356-
lifecycle);
359+
String scope;
360+
if (lazy) scope = "@Lazy";
361+
if (async) scope = "@AsyncBean";
362+
else scope = "@Prototype";
363+
364+
logError(beanType, "%s scoped bean does not support the %s lifecycle method", scope, lifecycle);
357365
}
358366
}
359367

360368
private Set<String> importTypes() {
369+
importTypes.add(type);
370+
if (async) {
371+
importTypes.add(CompletableFuture.class.getCanonicalName());
372+
}
361373
importTypes.add(type);
362374
typeReader.extraImports(importTypes);
363375
requestParams.addImports(importTypes);

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.Map;
1515
import java.util.Objects;
1616
import java.util.Set;
17+
import java.util.concurrent.CompletableFuture;
1718

1819
import javax.lang.model.type.TypeKind;
1920
import javax.tools.JavaFileObject;
@@ -175,7 +176,17 @@ private void writeAddFor(MethodReader constructor) {
175176
beanReader.buildAddFor(writer);
176177
if (beanReader.registerProvider()) {
177178
indent += " ";
178-
writer.append(" builder.%s(() -> {", beanReader.lazy() ? "registerProvider" : "asPrototype().registerProvider").eol();
179+
180+
final String registerProvider;
181+
if (beanReader.async()) {
182+
registerProvider = "registerProvider(CompletableFuture.supplyAsync";
183+
} else if (beanReader.lazy()) {
184+
registerProvider = "registerProvider";
185+
} else {
186+
registerProvider = "asPrototype().registerProvider";
187+
}
188+
189+
writer.append(" builder.%s(() -> {", registerProvider).eol();
179190
}
180191
constructor.startTry(writer);
181192
writeCreateBean(constructor);
@@ -187,7 +198,7 @@ private void writeAddFor(MethodReader constructor) {
187198
if (beanReader.registerProvider()) {
188199
beanReader.prototypePostConstruct(writer, indent);
189200
writer.indent(" return bean;").eol();
190-
writer.indent(" });").eol();
201+
writer.indent(" })").append("%s;", beanReader.async() ? "::join)" : "").eol();
191202
}
192203
writeObserveMethods();
193204
constructor.endTry(writer);

inject-generator/src/main/java/io/avaje/inject/generator/package-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
@GeneratePrism(value = Aspect.Import.class, name = "AspectImportPrism")
44
@GeneratePrism(Assisted.class)
55
@GeneratePrism(AssistFactory.class)
6+
@GeneratePrism(AsyncBean.class)
67
@GeneratePrism(Bean.class)
78
@GeneratePrism(Component.class)
89
@GeneratePrism(Component.Import.class)

inject-generator/src/test/java/io/avaje/inject/generator/InjectProcessorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void deleteGeneratedFiles() {
4141
}
4242
}
4343

44-
@Disabled
44+
4545
@Test
4646
void testGeneration() throws Exception {
4747
final String source =
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.avaje.inject.generator.models.valid.lazy;
2+
3+
import io.avaje.inject.AsyncBean;
4+
import jakarta.inject.Inject;
5+
import jakarta.inject.Provider;
6+
import jakarta.inject.Singleton;
7+
8+
@Singleton
9+
@AsyncBean
10+
public class BackgroundBean {
11+
@Inject Provider<Integer> intProvider;
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package io.avaje.inject;
2+
3+
import java.lang.annotation.*;
4+
5+
/** Limits the types exposed by this bean to the given types. */
6+
@Retention(RetentionPolicy.SOURCE)
7+
@Target({ElementType.METHOD, ElementType.TYPE})
8+
public @interface AsyncBean {}

0 commit comments

Comments
 (0)