Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SONARJAVA-5041 S5786 should raise an issue on JUnit5 annotated lifecycle methods with a public modifier #5004

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

alban-auzeill
Copy link
Member

@alban-auzeill alban-auzeill commented Feb 6, 2025

Copy link
Contributor

Choose a reason for hiding this comment

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

The change looks good overall but it looks like we don't have examples of issues raised on classes by S5786 with the new logic. It probably would help if we could get some documentation on the modifier scope enums to understand their usage

@@ -517,6 +535,9 @@
522
],
"org.eclipse.jetty:jetty-project:jetty-io/src/test/java/org/eclipse/jetty/io/SslEngineBehaviorTest.java": [
39,

Choose a reason for hiding this comment

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

Line 39 matches the class declaration. Are changes to the rules supposed to change our behavior on classes in addition to methods?

@@ -676,6 +712,9 @@
634
],
"org.eclipse.jetty:jetty-project:jetty-server/src/test/java/org/eclipse/jetty/server/ErrorHandlerTest.java": [
54,

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

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

That was fun to review 😅

.addTextEdit(JavaTextEdit.removeTree(modifier))
.build())
JavaQuickFix.newQuickFix("Remove \"%s\" modifier", modifier.keyword().text())
.addTextEdit(JavaTextEdit.removeTextSpan(AnalyzerMessage.textSpanBetween(modifier, true, QuickFixHelper.nextToken(modifier), false)))

Choose a reason for hiding this comment

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

Why do we have to change the way that we generate the quickfix?


raiseIssueOnMethods(junit5ClassMethods, ModifierScope.CLASS_METHOD);
raiseIssueOnMethods(junit5InstanceMethods, ModifierScope.INSTANCE_METHOD);
if (!junit5InstanceMethods.isEmpty()) {

Choose a reason for hiding this comment

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

Can we extract this for readability?

Suggested change
if (!junit5InstanceMethods.isEmpty()) {
boolean classHasJunit5InstanceMethods = !junit5InstanceMethods.isEmpty();
if (classHasJunit5InstanceMethods) {

if (!testMethods.isEmpty()) {
raiseIssueOnNotCompliantModifiers(classTree.modifiers(), false);
// Can we change the visibility of the class?
if (!hasPublicStaticMethods && !hasPublicStaticFields) {

Choose a reason for hiding this comment

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

I am not sure I like seeing doubling down on negatives here

Comment on lines +39 to +43
public enum ModifierScope {
CLASS,
CLASS_METHOD,
INSTANCE_METHOD
}

Choose a reason for hiding this comment

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

It would be very helpful to document how these enums represent and how they can be used

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