From 8276ccc5ec33262bf452b20c6845009e79efa711 Mon Sep 17 00:00:00 2001 From: Martin Kouba Date: Tue, 8 Oct 2024 10:08:37 +0200 Subject: [PATCH] ArC: detect incorrect PostConstruct and PreDestroy - resolves #43751 --- .../WrongAnnotationUsageProcessor.java | 3 ++ .../WrongPostConstructPreDestroyTest.java | 48 +++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 extensions/arc/deployment/src/test/java/io/quarkus/arc/test/wrongannotations/WrongPostConstructPreDestroyTest.java diff --git a/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/WrongAnnotationUsageProcessor.java b/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/WrongAnnotationUsageProcessor.java index 3477a61bf3870..9819e72f124d7 100644 --- a/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/WrongAnnotationUsageProcessor.java +++ b/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/WrongAnnotationUsageProcessor.java @@ -53,6 +53,9 @@ void detect(ArcConfig config, ApplicationIndexBuildItem applicationIndex, Custom unsupported.add(new UnsupportedAnnotation("org.gradle.internal.impldep.javax.inject.Inject", correctInject)); + unsupported.add(new UnsupportedAnnotation("javax.annotation.PostConstruct", "@jakarta.annotation.PostConstruct")); + unsupported.add(new UnsupportedAnnotation("javax.annotation.PreDestroy", "@jakarta.annotation.PreDestroy")); + Map wrongUsages = new HashMap<>(); for (UnsupportedAnnotation annotation : unsupported) { diff --git a/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/wrongannotations/WrongPostConstructPreDestroyTest.java b/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/wrongannotations/WrongPostConstructPreDestroyTest.java new file mode 100644 index 0000000000000..f7a3a3c1ea2aa --- /dev/null +++ b/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/wrongannotations/WrongPostConstructPreDestroyTest.java @@ -0,0 +1,48 @@ +package io.quarkus.arc.test.wrongannotations; + +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + +import javax.annotation.PostConstruct; +import javax.annotation.PreDestroy; + +import jakarta.inject.Singleton; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.runtime.util.ExceptionUtil; +import io.quarkus.test.QuarkusUnitTest; + +public class WrongPostConstructPreDestroyTest { + + @RegisterExtension + static final QuarkusUnitTest config = new QuarkusUnitTest() + .withApplicationRoot(root -> root + .addClasses(MySingleton.class)) + .assertException(t -> { + Throwable rootCause = ExceptionUtil.getRootCause(t); + assertTrue(rootCause.getMessage().contains("javax.annotation.PostConstruct"), t.toString()); + assertTrue(rootCause.getMessage().contains("javax.annotation.PreDestroy"), t.toString()); + }); + + @Test + public void testValidationFailed() { + // This method should not be invoked + fail(); + } + + @Singleton + static class MySingleton { + + @PostConstruct + void init() { + } + + @PreDestroy + void destroy() { + } + + } + +}