Skip to content

SONARJAVA-6112 Only one "main" method should be present#5455

Merged
rombirli merged 19 commits intomasterfrom
rombirli/SONARJAVA-6112-only-one-main-method
Feb 25, 2026
Merged

SONARJAVA-6112 Only one "main" method should be present#5455
rombirli merged 19 commits intomasterfrom
rombirli/SONARJAVA-6112-only-one-main-method

Conversation

@rombirli
Copy link
Copy Markdown
Contributor

No description provided.

@hashicorp-vault-sonar-prod
Copy link
Copy Markdown
Contributor

hashicorp-vault-sonar-prod Bot commented Feb 12, 2026

SONARJAVA-6112

@rombirli rombirli marked this pull request as ready for review February 12, 2026 10:45
Comment thread java-checks/src/main/java/org/sonar/java/checks/MultipleMainInstancesCheck.java Outdated
Comment thread java-checks/src/main/java/org/sonar/java/checks/MultipleMainInstancesCheck.java Outdated
Comment thread java-checks/src/main/java/org/sonar/java/checks/MultipleMainInstancesCheck.java Outdated
@rombirli rombirli force-pushed the rombirli/SONARJAVA-6112-only-one-main-method branch from 062ebc3 to dffd2f5 Compare February 13, 2026 14:18
Comment thread java-checks/src/main/java/org/sonar/java/checks/MultipleMainInstancesCheck.java Outdated
@rombirli rombirli force-pushed the rombirli/SONARJAVA-6112-only-one-main-method branch from 25781e1 to 6915763 Compare February 23, 2026 10:43
@rombirli rombirli force-pushed the rombirli/SONARJAVA-6112-only-one-main-method branch from 6915763 to 498e9c4 Compare February 23, 2026 13:33
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comment on the sample first. Either I miss something, or we may need to make some changes.

Comment thread java-checks/src/main/java/org/sonar/java/checks/MultipleMainInstancesCheck.java Outdated
Comment thread java-checks/src/main/java/org/sonar/java/checks/MultipleMainInstancesCheck.java Outdated
}

class Child extends Parent {
void main(String[] args) { // Noncompliant {{Override main from Parent to avoid introducing multiple main methods.}}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overriding parent main may not be doable if args are used. Am I missing something?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right! however in case parent contains main(), child contains main(String[] args) and uses args, it still makes sense to raise an issue on child's main, even if this issue is not easily fixable. A more accurate issue message for that case could be Do not extends a class that contains main() when you define main(String[] args). What do you think ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with a minor comment on naming.

@sonarqube-next
Copy link
Copy Markdown

@rombirli rombirli merged commit 872967d into master Feb 25, 2026
15 checks passed
@rombirli rombirli deleted the rombirli/SONARJAVA-6112-only-one-main-method branch February 25, 2026 16:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants