Skip to content

Commit

Permalink
ArC: detect incorrect PostConstruct and PreDestroy
Browse files Browse the repository at this point in the history
- resolves quarkusio#43751
  • Loading branch information
mkouba committed Oct 8, 2024
1 parent 63a1fea commit 8276ccc
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<AnnotationInstance, String> wrongUsages = new HashMap<>();

for (UnsupportedAnnotation annotation : unsupported) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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() {
}

}

}

0 comments on commit 8276ccc

Please sign in to comment.