Skip to content

Commit 34d90c4

Browse files
romainbrenguierclauderombirli
authored
SONARJAVA-6521 Exclude lifecycle annotations from S2325 (#5704)
Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com> Co-authored-by: rombirli <56340680+rombirli@users.noreply.github.com>
1 parent 5664af6 commit 34d90c4

3 files changed

Lines changed: 58 additions & 2 deletions

File tree

java-checks-test-sources/default/src/main/files/non-compiling/checks/StaticMethodCheckSample.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,36 @@ private void method() { // Compliant
3535
class SerializableExclusions implements Serializable {
3636
private Object writeReplace() throws ObjectStreamException { }
3737
}
38+
39+
class LifecycleAnnotations {
40+
private static int counter = 0;
41+
42+
@javax.annotation.PostConstruct
43+
private void initPostConstruct() { // Compliant - lifecycle method
44+
System.out.println("PostConstruct");
45+
}
46+
47+
@jakarta.annotation.PreDestroy
48+
private void cleanupPreDestroy() { // Compliant - lifecycle method
49+
System.out.println("PreDestroy");
50+
}
51+
52+
@javax.ejb.PostActivate
53+
private void activatePostActivate() { // Compliant - lifecycle method
54+
System.out.println("PostActivate");
55+
}
56+
57+
@jakarta.ejb.PrePassivate
58+
private void passivatePrePassivate() { // Compliant - lifecycle method
59+
System.out.println("PrePassivate");
60+
}
61+
62+
@io.quarkus.runtime.Startup
63+
private void startupMethod() { // Compliant - Quarkus startup method
64+
System.out.println("Startup");
65+
}
66+
67+
private void regularMethod() { // Noncompliant {{Make "regularMethod" a "static" method.}}
68+
counter++;
69+
}
70+
}

java-checks/src/main/java/org/sonar/java/checks/StaticMethodCheck.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
*/
1717
package org.sonar.java.checks;
1818

19+
1920
import java.util.ArrayList;
21+
import java.util.Arrays;
2022
import java.util.Deque;
2123
import java.util.LinkedList;
2224
import java.util.List;
@@ -72,6 +74,18 @@ public class StaticMethodCheck extends BaseTreeVisitor implements JavaFileScanne
7274
params.isEmpty() || (params.size() == 1 && params.get(0).isSubtypeOf("java.lang.String[]")))
7375
.build();
7476

77+
private static final String[] LIFECYCLE_ANNOTATIONS = {
78+
"javax.annotation.PostConstruct",
79+
"javax.annotation.PreDestroy",
80+
"jakarta.annotation.PostConstruct",
81+
"jakarta.annotation.PreDestroy",
82+
"javax.ejb.PostActivate",
83+
"javax.ejb.PrePassivate",
84+
"jakarta.ejb.PostActivate",
85+
"jakarta.ejb.PrePassivate",
86+
"io.quarkus.runtime.Startup"
87+
};
88+
7589
private JavaFileScannerContext context;
7690
private Deque<MethodReference> methodReferences = new LinkedList<>();
7791

@@ -168,7 +182,16 @@ private static boolean shouldBePlacedAfterStatic(Modifier modifier) {
168182
}
169183

170184
private static boolean isExcluded(MethodTree tree) {
171-
return tree.is(Tree.Kind.CONSTRUCTOR) || EXCLUDED_SERIALIZABLE_METHODS.matches(tree) || hasEmptyBody(tree) || MAIN_METHOD.matches(tree);
185+
return tree.is(Tree.Kind.CONSTRUCTOR)
186+
|| EXCLUDED_SERIALIZABLE_METHODS.matches(tree)
187+
|| hasEmptyBody(tree)
188+
|| MAIN_METHOD.matches(tree)
189+
|| hasLifecycleAnnotation(tree);
190+
}
191+
192+
private static boolean hasLifecycleAnnotation(MethodTree tree) {
193+
var metadata = tree.symbol().metadata();
194+
return Arrays.stream(LIFECYCLE_ANNOTATIONS).anyMatch(metadata::isAnnotatedWith);
172195
}
173196

174197
private static boolean hasEmptyBody(MethodTree tree) {

java-checks/src/test/java/org/sonar/java/checks/StaticMethodCheckTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void test_non_compiling() {
4545
CheckVerifier.newVerifier()
4646
.onFile(nonCompilingTestSourcesPath("checks/StaticMethodCheckSample.java"))
4747
.withCheck(new StaticMethodCheck())
48-
.verifyNoIssues();
48+
.verifyIssues();
4949
}
5050

5151
}

0 commit comments

Comments
 (0)