Skip to content

Commit 5fd42bf

Browse files
authored
Merge pull request #810 from SentryMan/inheritedLifecycle
Support Inherited Lifecycle on Factory Methods
2 parents 157fd15 + 5817d88 commit 5fd42bf

File tree

5 files changed

+93
-3
lines changed

5 files changed

+93
-3
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.example.myapp.lifecycle;
2+
3+
import jakarta.inject.Singleton;
4+
5+
@Singleton
6+
public class GreetingService extends ServiceCreator {
7+
private String greeting;
8+
9+
@Override
10+
public void setup() {
11+
greeting = "Hello from GreetingService!";
12+
}
13+
14+
public String getGreeting() {
15+
return greeting;
16+
}
17+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.example.myapp.lifecycle;
2+
3+
import io.avaje.inject.PostConstruct;
4+
5+
public abstract class ServiceCreator {
6+
@PostConstruct
7+
public void setup() {}
8+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.example.myapp.lifecycle;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
import io.avaje.inject.test.InjectTest;
8+
import jakarta.inject.Inject;
9+
10+
@InjectTest
11+
class HelloServiceTest {
12+
13+
@Inject GreetingService service;
14+
15+
@Test
16+
void testGreetingMessage() {
17+
assertEquals("Hello from GreetingService!", service.getGreeting());
18+
}
19+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.example.myapp.lifecycle;
2+
3+
import io.avaje.inject.Bean;
4+
import io.avaje.inject.Factory;
5+
import io.avaje.inject.test.TestScope;
6+
7+
@TestScope
8+
@Factory
9+
public class TestBeanFactory {
10+
@Bean
11+
public GreetingService greetingService() {
12+
return new GreetingService();
13+
}
14+
}
Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package io.avaje.inject.generator;
22

3+
import java.util.Objects;
4+
35
import javax.lang.model.element.AnnotationMirror;
46
import javax.lang.model.element.Element;
7+
import javax.lang.model.element.ExecutableElement;
8+
import javax.lang.model.element.TypeElement;
9+
import javax.lang.model.util.ElementFilter;
10+
import javax.lang.model.util.Types;
511

612
final class AnnotationUtil {
713

@@ -11,13 +17,39 @@ static boolean hasAnnotationWithName(Element element, String matchShortName) {
1117
return true;
1218
}
1319
}
20+
21+
if (element instanceof ExecutableElement) {
22+
return annotatedSuperMethod((ExecutableElement) element, matchShortName);
23+
}
24+
1425
return false;
1526
}
1627

17-
/**
18-
* Return the short name of the element.
19-
*/
28+
/** Return the short name of the element. */
2029
private static String shortName(Element element) {
2130
return element.getSimpleName().toString();
2231
}
32+
33+
private static boolean annotatedSuperMethod(ExecutableElement element, Object matchShortName) {
34+
var methodName = element.getSimpleName();
35+
final Types types = APContext.types();
36+
return types.directSupertypes(element.getEnclosingElement().asType()).stream()
37+
.filter(type -> !type.toString().contains("java.lang.Object"))
38+
.map(superType -> {
39+
final var superClass = (TypeElement) types.asElement(superType);
40+
for (final var method : ElementFilter.methodsIn(superClass.getEnclosedElements())) {
41+
if (matchNameAndParams(element, method, methodName)) {
42+
return method;
43+
}
44+
}
45+
return null;
46+
})
47+
.filter(Objects::nonNull)
48+
.flatMap(m -> APContext.elements().getAllAnnotationMirrors(m).stream())
49+
.anyMatch(m -> matchShortName.equals(shortName(m.getAnnotationType().asElement())));
50+
}
51+
52+
private static boolean matchNameAndParams(ExecutableElement element, ExecutableElement method, javax.lang.model.element.Name methodName) {
53+
return method.getSimpleName().contentEquals(methodName) && method.getParameters().size() == element.getParameters().size();
54+
}
2355
}

0 commit comments

Comments
 (0)