Skip to content

Commit

Permalink
Merge pull request #43761 from mkouba/issue-43751
Browse files Browse the repository at this point in the history
ArC: detect incorrect PostConstruct and PreDestroy
  • Loading branch information
mkouba authored Oct 8, 2024
2 parents a6b314a + 8276ccc commit 7f5dc76
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 7f5dc76

Please sign in to comment.