Skip to content

Fix Lazy Checked Exceptions #737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class LazyBean {

@Inject @Nullable AtomicBoolean initialized;

public LazyBean() throws Exception {}

@PostConstruct
void init(BeanScope scope) {
// note that nested test scopes will not be lazy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class LazyFactory {

@Bean
@Named("factory")
LazyBean lazyInt(@Nullable AtomicBoolean initialized) {
LazyBean lazyInt(@Nullable AtomicBoolean initialized) throws Exception {

// note that nested test scopes will not be lazy
if (initialized != null) initialized.set(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ void addLifecycleCallbacks(Append writer, String indent) {
void prototypePostConstruct(Append writer, String indent) {
if (postConstructMethod != null) {
var postConstruct = (ExecutableElement) postConstructMethod;
writer.append("%s bean.%s(", indent, postConstructMethod.getSimpleName());
writer.indent(indent).append(" bean.%s(", postConstructMethod.getSimpleName());
if (postConstruct.getParameters().isEmpty()) {
writer.append(");").eol();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,15 @@ private void writeAddFor(MethodReader constructor) {
beanReader.buildAddFor(writer);
if (beanReader.registerProvider()) {
indent += " ";
writer.append(" builder.%s(() -> {", beanReader.lazy() ? "registerProvider" : "asPrototype().registerProvider").eol();

final String registerProvider;
if (beanReader.lazy()) {
registerProvider = "registerProvider";
} else {
registerProvider = "asPrototype().registerProvider";
}

writer.append(" builder.%s(() -> {", registerProvider).eol();
}
constructor.startTry(writer);
writeCreateBean(constructor);
Expand All @@ -187,11 +195,20 @@ private void writeAddFor(MethodReader constructor) {
if (beanReader.registerProvider()) {
beanReader.prototypePostConstruct(writer, indent);
writer.indent(" return bean;").eol();
writer.indent(" });").eol();
if (!constructor.methodThrows()) {
writer.indent(" });").eol();
}
}
writeObserveMethods();
constructor.endTry(writer);
writer.append(" }").eol();

if (beanReader.registerProvider() && constructor.methodThrows()) {
writer.append(" }");
writer.append(");").eol();
}

writer.append(" }");
writer.eol();
}

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

private void writeExtraInjection() {
if (!beanReader.registerProvider()) {
writer.indent(" ").append("builder.addInjector(b -> {").eol();
writer.indent(" ").append(" // field and method injection").eol();
writer.indent(indent).append(" builder.addInjector(b -> {").eol();
writer.indent(indent).append(" // field and method injection").eol();
}
injectFields();
injectMethods();
if (!beanReader.registerProvider()) {
writer.indent(" });").eol();
writer.indent(indent).append(" });").eol();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Stream;

import javax.tools.JavaCompiler;
Expand All @@ -23,16 +24,17 @@

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Disabled;

class InjectProcessorTest {

@AfterEach
void deleteGeneratedFiles() {
try {
Stream.concat(
Stream.of(
Files.walk(Paths.get("io").toAbsolutePath()),
Files.walk(Paths.get("lang").toAbsolutePath()))
Files.walk(Paths.get("lang").toAbsolutePath()),
Files.walk(Paths.get("util").toAbsolutePath()))
.flatMap(Function.identity())
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
Expand All @@ -41,7 +43,7 @@ void deleteGeneratedFiles() {
}
}

@Disabled
//@Disabled
@Test
void testGeneration() throws Exception {
final String source =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@
@Singleton
public class LazyBean {
@Inject Provider<Integer> intProvider;

public LazyBean() throws Exception {}
}