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

Introduce Slf4jLogDeclaration to canonicalize slf4j's variable's declaration #783

Open
wants to merge 19 commits into
base: master
Choose a base branch
from

Conversation

mohamedsamehsalah
Copy link
Contributor

@mohamedsamehsalah mohamedsamehsalah commented Sep 8, 2023

#632

Suggested commit message:

Introduce `Slf4jLogDeclaration` check (#783)

@mohamedsamehsalah mohamedsamehsalah added this to the 0.14.0 milestone Sep 8, 2023
@mohamedsamehsalah mohamedsamehsalah linked an issue Sep 8, 2023 that may be closed by this pull request
3 tasks
@github-actions
Copy link

github-actions bot commented Sep 8, 2023

  • Surviving mutants in this change: 5
  • Killed mutants in this change: 16
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.Slf4jLogDeclaration 5 16

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@mohamedsamehsalah mohamedsamehsalah force-pushed the mohamedsamehsalah/632-canonical-slf4j-logger-usage branch from 536811a to fc4d717 Compare September 8, 2023 20:15
@github-actions
Copy link

github-actions bot commented Sep 8, 2023

  • Surviving mutants in this change: 5
  • Killed mutants in this change: 16
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.Slf4jLogDeclaration 5 16

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@mohamedsamehsalah mohamedsamehsalah force-pushed the mohamedsamehsalah/632-canonical-slf4j-logger-usage branch from fc4d717 to 3c70f11 Compare September 8, 2023 20:24
@github-actions
Copy link

github-actions bot commented Sep 8, 2023

  • Surviving mutants in this change: 5
  • Killed mutants in this change: 16
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.Slf4jLogDeclaration 5 16

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@mohamedsamehsalah mohamedsamehsalah force-pushed the mohamedsamehsalah/632-canonical-slf4j-logger-usage branch from 3c70f11 to 5947396 Compare September 8, 2023 20:43
@sonarcloud
Copy link

sonarcloud bot commented Sep 8, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 2 Code Smells

91.4% 91.4% Coverage
0.0% 0.0% Duplication

@github-actions
Copy link

github-actions bot commented Sep 8, 2023

  • Surviving mutants in this change: 5
  • Killed mutants in this change: 16
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.Slf4jLogDeclaration 5 16

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

Copy link
Member

@Stephan202 Stephan202 left a comment

Choose a reason for hiding this comment

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

Quick skim; hope this helps :)


@Override
public Description matchClass(ClassTree tree, VisitorState state) {
if (tree.getKind() == Kind.INTERFACE || !TEST_CLASS_WITH_LOGGER.matches(tree, state)) {
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we need to skip interfaces: for those we can still suggest (but not auto-fix) the canonical field name.

(Separately from that we should drop redundant modifiers, but that should be a separate check, modeled after this Checkstyle rule.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think we need to skip interfaces

The intention was to not apply modifiers to inteface variables but still suggest/fix field name.

: for those we can still suggest (but not auto-fix) the canonical field name.

Why only suggest and not auto-fix ?

(Separately from that we should drop redundant modifiers, but that should be a separate check, modeled after this Checkstyle rule.)

Should this check be part of this PR ?

Comment on lines 100 to 102
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
if (!GET_LOGGER_METHOD.matches(tree, state)) {
Copy link
Member

Choose a reason for hiding this comment

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

We're now creating separate fixes for the field declaration (modifiers + name) and for the referenced class. I wonder whether this means that we should have two separate checks. I can imagine that some users will appreciate the second, while finding the first too conservative.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pushed a change using TreeScanner (Conceptually still two checks usingvisitClass & visitMethodInvocationTree).

What do you think about making the separation into an EP flag ?

if (LOGGER.matches(member, state)) {
VariableTree variable = (VariableTree) member;
SuggestedFixes.addModifiers(
member, state, Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL)
Copy link
Member

Choose a reason for hiding this comment

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

Making the field final or private could make the code fail to compile. Likely this is still the right trade-off, but perhaps something worth calling out.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does it make sense to make the suggested modifiers EP flags instead ?

@mohamedsamehsalah mohamedsamehsalah force-pushed the mohamedsamehsalah/632-canonical-slf4j-logger-usage branch from 5947396 to 61626b7 Compare November 25, 2023 20:36
Copy link

  • Surviving mutants in this change: 6
  • Killed mutants in this change: 19
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.Slf4jLogDeclaration$1 3 4
🧟tech.picnic.errorprone.bugpatterns.Slf4jLogDeclaration 3 15

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

Copy link

sonarcloud bot commented Nov 25, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 2 Code Smells

89.5% 89.5% Coverage
0.0% 0.0% Duplication

Copy link

  • Surviving mutants in this change: 6
  • Killed mutants in this change: 19
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.Slf4jLogDeclaration$1 3 4
🧟tech.picnic.errorprone.bugpatterns.Slf4jLogDeclaration 3 15

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@rickie rickie modified the milestones: 0.15.0, 0.16.0 Feb 10, 2024
Copy link
Member

@rickie rickie left a comment

Choose a reason for hiding this comment

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

Some comments we discussed offline :).

@mohamedsamehsalah mohamedsamehsalah force-pushed the mohamedsamehsalah/632-canonical-slf4j-logger-usage branch from 4142606 to a54db09 Compare March 3, 2024 16:27
Copy link
Contributor Author

@mohamedsamehsalah mohamedsamehsalah left a comment

Choose a reason for hiding this comment

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

Sorry for the slow progress 🐢

Copy link

github-actions bot commented Mar 3, 2024

  • Surviving mutants in this change: 10
  • Killed mutants in this change: 15
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.Slf4jLogDeclaration$1 7 2
🧟tech.picnic.errorprone.bugpatterns.Slf4jLogDeclaration 3 13

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

1 similar comment
Copy link

github-actions bot commented Mar 3, 2024

  • Surviving mutants in this change: 10
  • Killed mutants in this change: 15
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.Slf4jLogDeclaration$1 7 2
🧟tech.picnic.errorprone.bugpatterns.Slf4jLogDeclaration 3 13

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@mohamedsamehsalah mohamedsamehsalah force-pushed the mohamedsamehsalah/632-canonical-slf4j-logger-usage branch from 7109c5d to b29f243 Compare April 6, 2024 17:11
Copy link

github-actions bot commented Apr 6, 2024

  • Surviving mutants in this change: 6
  • Killed mutants in this change: 22
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.Slf4jLogDeclaration 4 16
🧟tech.picnic.errorprone.bugpatterns.Slf4jLogDeclaration$1 2 6

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

Copy link

github-actions bot commented Apr 6, 2024

  • Surviving mutants in this change: 6
  • Killed mutants in this change: 26
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.Slf4jLogDeclaration$1 4 8
🧟tech.picnic.errorprone.bugpatterns.Slf4jLogDeclaration 2 18

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

Copy link

github-actions bot commented Apr 6, 2024

  • Surviving mutants in this change: 5
  • Killed mutants in this change: 26
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.Slf4jLogDeclaration$1 3 8
🧟tech.picnic.errorprone.bugpatterns.Slf4jLogDeclaration 2 18

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

Copy link
Contributor Author

@mohamedsamehsalah mohamedsamehsalah left a comment

Choose a reason for hiding this comment

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

Thanks @rickie , I resolved the comments from the pair programming sessions and tried to clean up some of the mutants 🐞. PTAL 🙏 .

}

Symbol enclosingElement = ASTHelpers.getSymbol(tree).getEnclosingElement();
if (enclosingElement == null) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Either we do this check or we suppress NullAway warning

String argumentClassName;
if (arg.getKind() == Kind.STRING_LITERAL) {
java.util.regex.Matcher matcher = STRING_LITERAL_ARGUMENT.matcher(argumentName);
checkArgument(matcher.matches(), "Invalid argument name.");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here and below, this check is used to invoke the matching.

Copy link
Member

@rickie rickie left a comment

Choose a reason for hiding this comment

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

Did another pass, some small improvements here and there :).

ExpressionTree arg = Iterables.getOnlyElement(tree.getArguments());
String argumentName = SourceCode.treeToString(arg, state);

String argumentClassName;
Copy link
Member

Choose a reason for hiding this comment

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

We can simplify the code a little:

       java.util.regex.Matcher matcher;
          if (arg.getKind() == Kind.STRING_LITERAL) {
            matcher = STRING_LITERAL_ARGUMENT.matcher(argumentName);
          } else {
            matcher = CLASS_ARGUMENT.matcher(argumentName);
          }

          checkArgument(matcher.matches(), "Invalid argument name.");
          String argumentClassName = matcher.group(1);

}
}

private static void updateGetLoggerArgument(
Copy link
Member

Choose a reason for hiding this comment

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

I'm not seeing why we need a TreeScanner to walk over a VariableTree, I think we can improve this :).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We do this because we need to match on the variable declaration and not in the class level (Which was flagged here). In addition, in case the variable is in a nested class, without this, the match will be on the outer class level .

Copy link

  • Surviving mutants in this change: 4
  • Killed mutants in this change: 27
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.Slf4jLogDeclaration$1 2 8
🧟tech.picnic.errorprone.bugpatterns.Slf4jLogDeclaration 2 19

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

Copy link

  • Surviving mutants in this change: 3
  • Killed mutants in this change: 26
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.Slf4jLogDeclaration 3 26

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@rickie
Copy link
Member

rickie commented Apr 20, 2024

/integration-test

Copy link
Member

@rickie rickie left a comment

Choose a reason for hiding this comment

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

I think there is only one open question for me that we should answer before approving. Apart from that, I have almost nothing to add here!

Really cool stuff @mohamedsamehsalah 🚀 !

matcher = CLASS_ARGUMENT_PATTERN.matcher(argumentName);
}

checkArgument(matcher.matches(), "Invalid argument name.");
Copy link
Member

Choose a reason for hiding this comment

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

Hmm interesting. Maybe a verify or checkState, would be better?

"",
" static class WrongArgumentOverloadedGetLogger {",
" // BUG: Diagnostic contains:",
" private static final Logger LOG = LoggerFactory.getLogger(\"OverloadedGetLogger\");",
Copy link
Member

Choose a reason for hiding this comment

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

On a second look, I'm not really sure how we should handle these string overloads... There are quite some different ways of using them, if we look outside of Picnic to usages. Within Picnic we have a few like Something.NameOfclass, so not sure what we want to do there...

CC: @Stephan202 What do you think?

" }",
"",
" static class WrongArgumentOverloadedGetLogger {",
" // BUG: Diagnostic contains:",
Copy link
Member

Choose a reason for hiding this comment

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

We can drop this one in a replacement test 😉.

"class A {",
" private static final long serialVersionUID = 1L;",
"",
" private static final Logger LOG = LoggerFactory.getLogger(A.class);",
Copy link
Member

Choose a reason for hiding this comment

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

I put the "positive" examples at the top of the file :).

Copy link

sonarcloud bot commented May 2, 2024

Copy link

github-actions bot commented May 2, 2024

  • Surviving mutants in this change: 3
  • Killed mutants in this change: 26
class surviving killed
🧟tech.picnic.errorprone.bugpatterns.Slf4jLogDeclaration 3 26

Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

Canonical SLF4J Logger usage
3 participants