Skip to content

Commit 1d30568

Browse files
committed
#70 - Change inject-generator to detect any @PostConstruct @PreDestroy in any package - so javax.annotation, jakarta.annotation etc
1 parent e0c7753 commit 1d30568

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import io.avaje.inject.Primary;
55
import io.avaje.inject.Secondary;
66

7-
import javax.annotation.PostConstruct;
8-
import javax.annotation.PreDestroy;
97
import javax.inject.Inject;
108
import javax.inject.Named;
119
import javax.inject.Qualifier;
@@ -71,7 +69,7 @@ class BeanReader {
7169
BeanReader(TypeElement beanType, ProcessingContext context) {
7270
this.beanType = beanType;
7371
this.type = beanType.getQualifiedName().toString();
74-
this.shortName = beanType.getSimpleName().toString();
72+
this.shortName = shortName(beanType);
7573
this.context = context;
7674
this.requestParams = new BeanRequestParams(type);
7775
init();
@@ -267,18 +265,35 @@ private void readMethod(Element element, boolean factory) {
267265
}
268266

269267
if (context.isPostConstructAvailable()) {
270-
PostConstruct pcMarker = element.getAnnotation(PostConstruct.class);
271-
if (pcMarker != null) {
268+
if (hasAnnotationWithName(element, "PostConstruct")) {
272269
postConstructMethod = element;
273270
}
274271

275-
PreDestroy pdMarker = element.getAnnotation(PreDestroy.class);
276-
if (pdMarker != null) {
272+
if (hasAnnotationWithName(element, "PreDestroy")) {
277273
preDestroyMethod = element;
278274
}
279275
}
280276
}
281277

278+
/**
279+
* Return true if the element has an annotation with the matching short name.
280+
*/
281+
private boolean hasAnnotationWithName(Element element, String matchShortName) {
282+
for (AnnotationMirror mirror : element.getAnnotationMirrors()) {
283+
if (matchShortName.equals(shortName(mirror.getAnnotationType().asElement()))) {
284+
return true;
285+
}
286+
}
287+
return false;
288+
}
289+
290+
/**
291+
* Return the short name of the element.
292+
*/
293+
private String shortName(Element element) {
294+
return element.getSimpleName().toString();
295+
}
296+
282297
private void addFactoryMethod(ExecutableElement methodElement, Bean bean) {
283298
// Not yet reading Qualifier annotations, Named only at this stage
284299
Named named = methodElement.getAnnotation(Named.class);

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<modules>
2323
<module>inject</module>
2424
<module>inject-generator</module>
25-
<!-- <module>inject-test</module>-->
25+
<module>inject-test</module>
2626
</modules>
2727
</project>
2828

0 commit comments

Comments
 (0)