Skip to content

Commit 7809702

Browse files
SONARJAVA-6075 Do not raise S2325 on instance main (#5421)
1 parent 6bb2693 commit 7809702

4 files changed

Lines changed: 36 additions & 1 deletion

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
void main() {
2+
System.out.println("Hello, World!");
3+
}

java-checks-test-sources/default/src/main/java/checks/StaticMethodCheckSample.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,3 +426,19 @@ private List<List<T>> requiresNestedTypeVar() { // Compliant
426426
}
427427
}
428428
}
429+
430+
// Note `final` on the class indicating that main methods will not be overridden.
431+
// Without it, the check would apply only to private methods.
432+
final public class StaticMethodCheckSample {
433+
void main() { // Compliant
434+
System.out.println("StaticMethodCheckSample no arg main");
435+
}
436+
437+
void main(String[] args) { // Compliant
438+
System.out.println("StaticMethodCheckSample main with args");
439+
}
440+
441+
void main(int i) { // Noncompliant
442+
System.out.println("i = " + i);
443+
}
444+
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ public class StaticMethodCheck extends BaseTreeVisitor implements JavaFileScanne
6464
.addWithoutParametersMatcher()
6565
.build());
6666

67+
private static final MethodMatchers MAIN_METHOD =
68+
MethodMatchers.create()
69+
.ofAnyType()
70+
.names("main")
71+
.addParametersMatcher(params ->
72+
params.isEmpty() || (params.size() == 1 && params.get(0).isSubtypeOf("java.lang.String[]")))
73+
.build();
74+
6775
private JavaFileScannerContext context;
6876
private Deque<MethodReference> methodReferences = new LinkedList<>();
6977

@@ -160,7 +168,7 @@ private static boolean shouldBePlacedAfterStatic(Modifier modifier) {
160168
}
161169

162170
private static boolean isExcluded(MethodTree tree) {
163-
return tree.is(Tree.Kind.CONSTRUCTOR) || EXCLUDED_SERIALIZABLE_METHODS.matches(tree) || hasEmptyBody(tree);
171+
return tree.is(Tree.Kind.CONSTRUCTOR) || EXCLUDED_SERIALIZABLE_METHODS.matches(tree) || hasEmptyBody(tree) || MAIN_METHOD.matches(tree);
164172
}
165173

166174
private static boolean hasEmptyBody(MethodTree tree) {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ void test() {
3232
.verifyIssues();
3333
}
3434

35+
@Test
36+
void test_compact_source() {
37+
CheckVerifier.newVerifier()
38+
.onFile(mainCodeSourcesPath("checks/StaticMethodCheckCompactSample.java"))
39+
.withCheck(new StaticMethodCheck())
40+
.verifyNoIssues();
41+
}
42+
3543
@Test
3644
void test_non_compiling() {
3745
CheckVerifier.newVerifier()

0 commit comments

Comments
 (0)