Skip to content

Commit cfc8399

Browse files
authored
Merge pull request #737 from SentryMan/lazy-checked
Fix Lazy Checked Exceptions
2 parents c42eb5c + 8599370 commit cfc8399

File tree

6 files changed

+35
-12
lines changed

6 files changed

+35
-12
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public class LazyBean {
1717

1818
@Inject @Nullable AtomicBoolean initialized;
1919

20+
public LazyBean() throws Exception {}
21+
2022
@PostConstruct
2123
void init(BeanScope scope) {
2224
// note that nested test scopes will not be lazy

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

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

1515
@Bean
1616
@Named("factory")
17-
LazyBean lazyInt(@Nullable AtomicBoolean initialized) {
17+
LazyBean lazyInt(@Nullable AtomicBoolean initialized) throws Exception {
1818

1919
// note that nested test scopes will not be lazy
2020
if (initialized != null) initialized.set(true);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ void addLifecycleCallbacks(Append writer, String indent) {
337337
void prototypePostConstruct(Append writer, String indent) {
338338
if (postConstructMethod != null) {
339339
var postConstruct = (ExecutableElement) postConstructMethod;
340-
writer.append("%s bean.%s(", indent, postConstructMethod.getSimpleName());
340+
writer.indent(indent).append(" bean.%s(", postConstructMethod.getSimpleName());
341341
if (postConstruct.getParameters().isEmpty()) {
342342
writer.append(");").eol();
343343
} else {

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

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,15 @@ private void writeAddFor(MethodReader constructor) {
175175
beanReader.buildAddFor(writer);
176176
if (beanReader.registerProvider()) {
177177
indent += " ";
178-
writer.append(" builder.%s(() -> {", beanReader.lazy() ? "registerProvider" : "asPrototype().registerProvider").eol();
178+
179+
final String registerProvider;
180+
if (beanReader.lazy()) {
181+
registerProvider = "registerProvider";
182+
} else {
183+
registerProvider = "asPrototype().registerProvider";
184+
}
185+
186+
writer.append(" builder.%s(() -> {", registerProvider).eol();
179187
}
180188
constructor.startTry(writer);
181189
writeCreateBean(constructor);
@@ -187,11 +195,20 @@ private void writeAddFor(MethodReader constructor) {
187195
if (beanReader.registerProvider()) {
188196
beanReader.prototypePostConstruct(writer, indent);
189197
writer.indent(" return bean;").eol();
190-
writer.indent(" });").eol();
198+
if (!constructor.methodThrows()) {
199+
writer.indent(" });").eol();
200+
}
191201
}
192202
writeObserveMethods();
193203
constructor.endTry(writer);
194-
writer.append(" }").eol();
204+
205+
if (beanReader.registerProvider() && constructor.methodThrows()) {
206+
writer.append(" }");
207+
writer.append(");").eol();
208+
}
209+
210+
writer.append(" }");
211+
writer.eol();
195212
}
196213

197214
private void writeBuildMethodStart() {
@@ -208,13 +225,13 @@ private void writeCreateBean(MethodReader constructor) {
208225

209226
private void writeExtraInjection() {
210227
if (!beanReader.registerProvider()) {
211-
writer.indent(" ").append("builder.addInjector(b -> {").eol();
212-
writer.indent(" ").append(" // field and method injection").eol();
228+
writer.indent(indent).append(" builder.addInjector(b -> {").eol();
229+
writer.indent(indent).append(" // field and method injection").eol();
213230
}
214231
injectFields();
215232
injectMethods();
216233
if (!beanReader.registerProvider()) {
217-
writer.indent(" });").eol();
234+
writer.indent(indent).append(" });").eol();
218235
}
219236
}
220237

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.Comparator;
1212
import java.util.List;
1313
import java.util.Set;
14+
import java.util.function.Function;
1415
import java.util.stream.Stream;
1516

1617
import javax.tools.JavaCompiler;
@@ -23,16 +24,17 @@
2324

2425
import org.junit.jupiter.api.AfterEach;
2526
import org.junit.jupiter.api.Test;
26-
import org.junit.jupiter.api.Disabled;
2727

2828
class InjectProcessorTest {
2929

3030
@AfterEach
3131
void deleteGeneratedFiles() {
3232
try {
33-
Stream.concat(
33+
Stream.of(
3434
Files.walk(Paths.get("io").toAbsolutePath()),
35-
Files.walk(Paths.get("lang").toAbsolutePath()))
35+
Files.walk(Paths.get("lang").toAbsolutePath()),
36+
Files.walk(Paths.get("util").toAbsolutePath()))
37+
.flatMap(Function.identity())
3638
.sorted(Comparator.reverseOrder())
3739
.map(Path::toFile)
3840
.forEach(File::delete);
@@ -41,7 +43,7 @@ void deleteGeneratedFiles() {
4143
}
4244
}
4345

44-
@Disabled
46+
//@Disabled
4547
@Test
4648
void testGeneration() throws Exception {
4749
final String source =

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@
99
@Singleton
1010
public class LazyBean {
1111
@Inject Provider<Integer> intProvider;
12+
13+
public LazyBean() throws Exception {}
1214
}

0 commit comments

Comments
 (0)