Skip to content

Commit 1f7f05e

Browse files
committed
can inject directly into postconstruct methods
1 parent 5161bba commit 1f7f05e

File tree

6 files changed

+102
-21
lines changed

6 files changed

+102
-21
lines changed

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

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ final class BeanReader {
2929
private final List<MethodReader> injectMethods;
3030
private final List<MethodReader> factoryMethods;
3131
private final List<MethodReader> observerMethods;
32-
private final Element postConstructMethod;
32+
private final Optional<MethodReader> postConstructMethod;
3333
private final Element preDestroyMethod;
3434

3535
private final ImportTypeMap importTypes = new ImportTypeMap();
@@ -159,6 +159,8 @@ BeanReader read() {
159159
factoryMethod.addImports(importTypes);
160160
}
161161

162+
postConstructMethod.ifPresent(m -> m.addImports(importTypes));
163+
162164
conditions.addImports(importTypes);
163165
return this;
164166
}
@@ -236,6 +238,13 @@ Set<UType> allGenericTypes() {
236238
for (MethodReader factoryMethod : factoryMethods()) {
237239
factoryMethod.addDependsOnGeneric(allUTypes);
238240
}
241+
242+
postConstructMethod.ifPresent(
243+
m ->
244+
m.params().stream()
245+
.filter(MethodParam::isGenericParam)
246+
.map(MethodParam::getFullUType)
247+
.forEach(allUTypes::add));
239248
return allUTypes;
240249
}
241250

@@ -260,7 +269,7 @@ String metaKey() {
260269
* Return true if lifecycle via annotated methods is required.
261270
*/
262271
boolean hasLifecycleMethods() {
263-
return (postConstructMethod != null || preDestroyMethod != null || typeReader.isClosable());
272+
return (postConstructMethod.isPresent() || preDestroyMethod != null || typeReader.isClosable());
264273
}
265274

266275
List<MetaData> createFactoryMethodMeta() {
@@ -322,9 +331,10 @@ void buildRegister(Append writer) {
322331

323332
void addLifecycleCallbacks(Append writer, String indent) {
324333

325-
if (postConstructMethod != null && !registerProvider()) {
326-
writer.indent(indent).append(" builder.addPostConstruct($bean::%s);", postConstructMethod.getSimpleName()).eol();
334+
if (postConstructMethod.isPresent() && !registerProvider()) {
335+
writePostConstruct(writer, indent, postConstructMethod.get());
327336
}
337+
328338
if (preDestroyMethod != null) {
329339
lifeCycleNotSupported("@PreDestroy");
330340
var priority = preDestroyPriority == null || preDestroyPriority == 1000 ? "" : ", " + preDestroyPriority;
@@ -334,17 +344,54 @@ void addLifecycleCallbacks(Append writer, String indent) {
334344
}
335345
}
336346

347+
private void writePostConstruct(Append writer, String indent, MethodReader postConstruct) {
348+
349+
writer.indent(indent).append(" builder.addPostConstruct(");
350+
final var simplename = postConstruct.name();
351+
352+
final var params = postConstruct.params();
353+
if (params.isEmpty() || Constants.BEANSCOPE.equals(params.get(0).getFullUType().shortType())) {
354+
writer.append("$bean::%s);", simplename).eol();
355+
} else {
356+
writer.append("b -> $bean.%s(", simplename);
357+
358+
writeLifeCycleGet(writer, params, "b", "b");
359+
writer.append(");").eol();
360+
}
361+
}
362+
337363
void prototypePostConstruct(Append writer, String indent) {
338-
if (postConstructMethod != null) {
339-
var postConstruct = (ExecutableElement) postConstructMethod;
340-
writer.indent(indent).append(" bean.%s(", postConstructMethod.getSimpleName());
341-
if (postConstruct.getParameters().isEmpty()) {
342-
writer.append(");").eol();
364+
postConstructMethod.ifPresent(
365+
m -> {
366+
writer.indent(indent).append(" bean.%s(", m.name());
367+
if (m.params().isEmpty()) {
368+
writer.append(");").eol();
369+
} else {
370+
writeLifeCycleGet(
371+
writer, m.params(), "builder", "builder.get(io.avaje.inject.BeanScope.class)");
372+
writer.append(";").eol();
373+
}
374+
writer.eol();
375+
});
376+
}
377+
378+
private void writeLifeCycleGet(
379+
Append writer, final List<MethodParam> params, String builderName, String beanScopeString) {
380+
final var size = params.size();
381+
for (int i = 0; i < size; i++) {
382+
final var param = params.get(i);
383+
384+
if (Constants.BEANSCOPE.equals(param.getFullUType().fullWithoutAnnotations())) {
385+
writer.append(beanScopeString);
343386
} else {
344-
writer.append("builder.get(io.avaje.inject.BeanScope.class));").eol();
387+
param.builderGetDependency(writer, builderName);
388+
}
389+
390+
if (i + 1 != size) {
391+
writer.append(", ");
345392
}
346-
writer.eol();
347393
}
394+
writer.append(")");
348395
}
349396

350397
private void lifeCycleNotSupported(String lifecycle) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ final class TypeExtendsInjection {
3030
private final TypeElement baseType;
3131
private final boolean factory;
3232
private final List<AspectPair> typeAspects;
33-
private Element postConstructMethod;
33+
private Optional<MethodReader> postConstructMethod = Optional.empty();
3434
private Element preDestroyMethod;
3535
private Integer preDestroyPriority;
3636

@@ -125,7 +125,7 @@ private void readMethod(Element element, TypeElement type) {
125125
notInjectMethods.add(methodKey);
126126
}
127127
if (AnnotationUtil.hasAnnotationWithName(element, "PostConstruct")) {
128-
postConstructMethod = element;
128+
postConstructMethod = Optional.of(new MethodReader(methodElement, type, importTypes).read());
129129
checkAspect = false;
130130
}
131131
if (AnnotationUtil.hasAnnotationWithName(element, "PreDestroy")) {
@@ -206,7 +206,7 @@ List<MethodReader> observerMethods() {
206206
return observerMethods;
207207
}
208208

209-
Element postConstructMethod() {
209+
Optional<MethodReader> postConstructMethod() {
210210
return postConstructMethod;
211211
}
212212

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.ArrayList;
88
import java.util.List;
99
import java.util.Objects;
10+
import java.util.Optional;
1011
import java.util.Set;
1112
import java.util.stream.Collectors;
1213
import java.util.stream.Stream;
@@ -101,7 +102,7 @@ List<MethodReader> observerMethods() {
101102
return extendsInjection.observerMethods();
102103
}
103104

104-
Element postConstructMethod() {
105+
Optional<MethodReader> postConstructMethod() {
105106
return extendsInjection.postConstructMethod();
106107
}
107108

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package io.avaje.inject.generator;
22

3-
import javax.lang.model.element.Element;
4-
import javax.lang.model.element.TypeElement;
5-
import javax.lang.model.type.TypeKind;
6-
import javax.lang.model.type.TypeMirror;
3+
import static java.util.stream.Collectors.toList;
74

85
import java.util.List;
96
import java.util.Optional;
107
import java.util.Set;
11-
import static java.util.stream.Collectors.toList;
8+
9+
import javax.lang.model.element.Element;
10+
import javax.lang.model.element.TypeElement;
11+
import javax.lang.model.type.TypeKind;
1212

1313
final class TypeReader {
1414

@@ -107,7 +107,7 @@ List<MethodReader> observerMethods() {
107107
return extendsReader.observerMethods();
108108
}
109109

110-
Element postConstructMethod() {
110+
Optional<MethodReader> postConstructMethod() {
111111
return extendsReader.postConstructMethod();
112112
}
113113

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.lifecycle;
2+
3+
import java.util.function.Consumer;
4+
5+
import io.avaje.inject.BeanScope;
6+
import io.avaje.inject.PostConstruct;
7+
import jakarta.inject.Singleton;
8+
9+
@Singleton
10+
public class Minos {
11+
12+
@PostConstruct
13+
void prepareThyself(Serpent serpent, Consumer<String> c, BeanScope b) {}
14+
15+
// @PreDestroy
16+
// void thyEndIsNow() {
17+
// }
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.avaje.inject.generator.models.valid.lifecycle;
2+
3+
import java.util.function.Consumer;
4+
5+
import io.avaje.inject.BeanScope;
6+
import io.avaje.inject.PostConstruct;
7+
import io.avaje.inject.Prototype;
8+
9+
@Prototype
10+
public class Serpent {
11+
12+
@PostConstruct
13+
void hiss(Consumer<String> c, BeanScope b) {}
14+
15+
}

0 commit comments

Comments
 (0)