Skip to content

Commit 2b7eaa8

Browse files
authored
Merge pull request #533 from mohamed-abdelnour/fix-fallible-providers
fix: compilation error with providers and `throws`
2 parents 7670e95 + ac16933 commit 2b7eaa8

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

blackbox-test-inject/src/main/java/org/example/myapp/config/AppConfig.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ public Builder newBuilder() {
4848
return new Builder();
4949
}
5050

51+
@Prototype
52+
@Bean
53+
public BuilderThrows newBuilderThrows() throws Exception {
54+
return new BuilderThrows();
55+
}
56+
5157
@Bean
5258
Generated newGenerated() {
5359
return new Generated();
@@ -59,6 +65,12 @@ public MySecType generalSecondary() {
5965
return new MySecType();
6066
}
6167

68+
@Secondary
69+
@Bean
70+
public MySecTypeThrows generalSecondaryThrows() throws Exception {
71+
return new MySecTypeThrows();
72+
}
73+
6274
@Secondary
6375
@Bean
6476
public Optional<MySecOptType> optionalSecondary() {
@@ -100,12 +112,18 @@ MyAbstract myAbstract() {
100112
public static class Builder {
101113
}
102114

115+
public static class BuilderThrows {
116+
}
117+
103118
public static class Generated {
104119
}
105120

106121
public static class MySecType {
107122
}
108123

124+
public static class MySecTypeThrows {
125+
}
126+
109127
public static class MySecOptType {
110128
}
111129

blackbox-test-inject/src/main/java/org/example/myapp/lazy/LazyFactory.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,10 @@ LazyBean lazyInt(@Nullable AtomicBoolean initialized) {
2020
if (initialized != null) initialized.set(true);
2121
return new LazyBean();
2222
}
23+
24+
@Bean
25+
@Named("factoryThrows")
26+
LazyBean lazyIntThrows(@Nullable AtomicBoolean initialized) throws Exception {
27+
return lazyInt(initialized);
28+
}
2329
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ void builderAddBeanProvider(Append writer) {
205205

206206
writer.indent(String.format(".%s(() -> {", lazy ? "registerLazy" : "registerProvider")).eol();
207207

208+
startTry(writer);
208209
writer.indent(indent).append(" return ");
209210
writer.append("factory.%s(", methodName);
210211
for (int i = 0; i < params.size(); i++) {
@@ -214,6 +215,7 @@ void builderAddBeanProvider(Append writer) {
214215
params.get(i).builderGetDependency(writer, "builder");
215216
}
216217
writer.append(");").eol();
218+
endTry(writer);
217219
writer.indent(indent).append(" });").eol();
218220
writer.indent(indent).append("}").eol();
219221
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,10 @@ private void writeFactoryBeanMethod(MethodReader method) {
144144
method.buildConditional(writer);
145145
method.buildAddFor(writer);
146146
method.builderGetFactory(writer, beanReader.hasConditions());
147-
method.startTry(writer);
148147
if (method.isLazy() || method.isProtoType() || method.isUseProviderForSecondary()) {
149148
method.builderAddBeanProvider(writer);
150-
method.endTry(writer);
151149
} else {
150+
method.startTry(writer);
152151
method.builderBuildBean(writer);
153152
method.builderBuildAddBean(writer);
154153
method.endTry(writer);

0 commit comments

Comments
 (0)