Skip to content
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

#749 Add support for chained methods in @Bean(destroyMethod) #754

Merged
merged 1 commit into from
Jan 6, 2025
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
@@ -0,0 +1,29 @@
package org.example.myapp;

import java.util.concurrent.atomic.AtomicInteger;

public class MyNestedDestroy {

public static AtomicInteger started = new AtomicInteger();
public static AtomicInteger stopped = new AtomicInteger();

public static void reset() {
started.set(0);
stopped.set(0);
}

public void start() {
started.incrementAndGet();
}

public Reaper reaper() {
return new Reaper();
}

public static class Reaper {

public void stop() {
stopped.incrementAndGet();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

import io.avaje.inject.Bean;
import io.avaje.inject.Factory;
import org.example.myapp.MyNestedDestroy;

import java.io.IOException;

@Factory
class AFactory {

@Bean(initMethod = "start", destroyMethod = "reaper().stop()")
MyNestedDestroy lifecycle2() {
return new MyNestedDestroy();
}

@Bean
A0.Builder build0() {
return new I0();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@

class HelloServiceTest {

@Test
void lifecycles() {
MyNestedDestroy.reset();
try (BeanScope beanScope = BeanScope.builder().build()) {
assertThat(beanScope.get(MyNestedDestroy.class)).isNotNull();
assertThat(MyNestedDestroy.started.get()).isEqualTo(1);
assertThat(MyNestedDestroy.stopped.get()).isEqualTo(0);
}
assertThat(MyNestedDestroy.stopped.get()).isEqualTo(1);
}

/**
* No mocking, no use of <code>@TestScope</code> so just like main.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@ public void onStart() {

public void onStop() {
stopped.incrementAndGet();
System.out.println("STOPPED !! ");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,6 @@ void buildRegister(Append writer) {
}

void addLifecycleCallbacks(Append writer, String indent) {

if (postConstructMethod != null && !registerProvider()) {
writer.indent(indent).append(" builder.addPostConstruct($bean::%s);", postConstructMethod.getSimpleName()).eol();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ void builderBuildAddBean(Append writer) {
if (hasInitMethod) {
var addPostConstruct =
multiRegister
? " .peek(b -> builder.addPostConstruct(b::%s))"
? " .peek($bean -> builder.addPostConstruct($bean::%s))"
: "builder.addPostConstruct($bean::%s);";
writer.indent(indent).append(addPostConstruct, initMethod).eol();
}
Expand All @@ -296,14 +296,14 @@ void builderBuildAddBean(Append writer) {
if (notEmpty(destroyMethod)) {
var addPreDestroy =
multiRegister
? " .forEach(b -> builder.addPreDestroy(b::%s%s));"
: "builder.addPreDestroy($bean::%s%s);";
writer.indent(indent).append(addPreDestroy, destroyMethod, priority).eol();
? " .forEach($bean -> builder.addPreDestroy(%s%s));"
: "builder.addPreDestroy(%s%s);";
writer.indent(indent).append(addPreDestroy, addPreDestroy(destroyMethod), priority).eol();

} else if (typeReader != null && typeReader.isClosable()) {
var addPreDestroy =
multiRegister
? " .forEach(b -> builder.addPreDestroy(b::close%s));"
? " .forEach($bean -> builder.addPreDestroy($bean::close%s));"
: "builder.addPreDestroy($bean::close%s);";
writer.indent(indent).append(addPreDestroy, priority).eol();

Expand All @@ -324,6 +324,13 @@ void builderBuildAddBean(Append writer) {
}
}

static String addPreDestroy(String destroyMethod) {
if (!destroyMethod.contains(".")) {
return "$bean::" + destroyMethod;
}
return "() -> $bean." + destroyMethod;
}

private boolean hasLifecycleMethods() {
return notEmpty(initMethod) || notEmpty(destroyMethod) || (typeReader != null && typeReader.isClosable() || beanCloseable);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.avaje.inject.generator;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class MethodReaderTest {

@Test
void addPreDestroy() {
assertThat(MethodReader.addPreDestroy("close")).isEqualTo("$bean::close");
assertThat(MethodReader.addPreDestroy("foo")).isEqualTo("$bean::foo");
}

@Test
void addPreDestroyNested() {
assertThat(MethodReader.addPreDestroy("foo().bar()")).isEqualTo("() -> $bean.foo().bar()");
}
}
Loading