Skip to content

Commit ec3fde7

Browse files
authored
Merge pull request #754 from avaje/feature/destoryMethodChaining
#749 Add support for chained methods in @bean(destroyMethod)
2 parents 5161bba + 2f31887 commit ec3fde7

File tree

7 files changed

+77
-7
lines changed

7 files changed

+77
-7
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.example.myapp;
2+
3+
import java.util.concurrent.atomic.AtomicInteger;
4+
5+
public class MyNestedDestroy {
6+
7+
public static AtomicInteger started = new AtomicInteger();
8+
public static AtomicInteger stopped = new AtomicInteger();
9+
10+
public static void reset() {
11+
started.set(0);
12+
stopped.set(0);
13+
}
14+
15+
public void start() {
16+
started.incrementAndGet();
17+
}
18+
19+
public Reaper reaper() {
20+
return new Reaper();
21+
}
22+
23+
public static class Reaper {
24+
25+
public void stop() {
26+
stopped.incrementAndGet();
27+
}
28+
}
29+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22

33
import io.avaje.inject.Bean;
44
import io.avaje.inject.Factory;
5+
import org.example.myapp.MyNestedDestroy;
56

67
import java.io.IOException;
78

89
@Factory
910
class AFactory {
1011

12+
@Bean(initMethod = "start", destroyMethod = "reaper().stop()")
13+
MyNestedDestroy lifecycle2() {
14+
return new MyNestedDestroy();
15+
}
16+
1117
@Bean
1218
A0.Builder build0() {
1319
return new I0();

blackbox-test-inject/src/test/java/org/example/myapp/HelloServiceTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@
1414

1515
class HelloServiceTest {
1616

17+
@Test
18+
void lifecycles() {
19+
MyNestedDestroy.reset();
20+
try (BeanScope beanScope = BeanScope.builder().build()) {
21+
assertThat(beanScope.get(MyNestedDestroy.class)).isNotNull();
22+
assertThat(MyNestedDestroy.started.get()).isEqualTo(1);
23+
assertThat(MyNestedDestroy.stopped.get()).isEqualTo(0);
24+
}
25+
assertThat(MyNestedDestroy.stopped.get()).isEqualTo(1);
26+
}
27+
1728
/**
1829
* No mocking, no use of <code>@TestScope</code> so just like main.
1930
*/

blackbox-test-inject/src/test/java/org/example/myapp/testconfig/CountTestScopeStart.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,5 @@ public void onStart() {
1313

1414
public void onStop() {
1515
stopped.incrementAndGet();
16-
System.out.println("STOPPED !! ");
1716
}
1817
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,6 @@ void buildRegister(Append writer) {
321321
}
322322

323323
void addLifecycleCallbacks(Append writer, String indent) {
324-
325324
if (postConstructMethod != null && !registerProvider()) {
326325
writer.indent(indent).append(" builder.addPostConstruct($bean::%s);", postConstructMethod.getSimpleName()).eol();
327326
}

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ void builderBuildAddBean(Append writer) {
287287
if (hasInitMethod) {
288288
var addPostConstruct =
289289
multiRegister
290-
? " .peek(b -> builder.addPostConstruct(b::%s))"
290+
? " .peek($bean -> builder.addPostConstruct($bean::%s))"
291291
: "builder.addPostConstruct($bean::%s);";
292292
writer.indent(indent).append(addPostConstruct, initMethod).eol();
293293
}
@@ -296,14 +296,14 @@ void builderBuildAddBean(Append writer) {
296296
if (notEmpty(destroyMethod)) {
297297
var addPreDestroy =
298298
multiRegister
299-
? " .forEach(b -> builder.addPreDestroy(b::%s%s));"
300-
: "builder.addPreDestroy($bean::%s%s);";
301-
writer.indent(indent).append(addPreDestroy, destroyMethod, priority).eol();
299+
? " .forEach($bean -> builder.addPreDestroy(%s%s));"
300+
: "builder.addPreDestroy(%s%s);";
301+
writer.indent(indent).append(addPreDestroy, addPreDestroy(destroyMethod), priority).eol();
302302

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

@@ -324,6 +324,13 @@ void builderBuildAddBean(Append writer) {
324324
}
325325
}
326326

327+
static String addPreDestroy(String destroyMethod) {
328+
if (!destroyMethod.contains(".")) {
329+
return "$bean::" + destroyMethod;
330+
}
331+
return "() -> $bean." + destroyMethod;
332+
}
333+
327334
private boolean hasLifecycleMethods() {
328335
return notEmpty(initMethod) || notEmpty(destroyMethod) || (typeReader != null && typeReader.isClosable() || beanCloseable);
329336
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.avaje.inject.generator;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
7+
class MethodReaderTest {
8+
9+
@Test
10+
void addPreDestroy() {
11+
assertThat(MethodReader.addPreDestroy("close")).isEqualTo("$bean::close");
12+
assertThat(MethodReader.addPreDestroy("foo")).isEqualTo("$bean::foo");
13+
}
14+
15+
@Test
16+
void addPreDestroyNested() {
17+
assertThat(MethodReader.addPreDestroy("foo().bar()")).isEqualTo("() -> $bean.foo().bar()");
18+
}
19+
}

0 commit comments

Comments
 (0)