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

Fix the issue 196 #199

Merged
merged 5 commits into from
Mar 5, 2020
Merged

Fix the issue 196 #199

merged 5 commits into from
Mar 5, 2020

Conversation

KengoTODA
Copy link
Member

close #196

@KengoTODA KengoTODA self-assigned this Mar 4, 2020
@KengoTODA KengoTODA changed the title WIP: Fix 196 Fix the issue 196 Mar 4, 2020
@KengoTODA KengoTODA mentioned this pull request Mar 4, 2020

public class SpotBugsPlugin implements Plugin<Project> {
public static final String CONFIG_NAME = "spotbugs";
public static final String PLUGINS_CONFIG_NAME = "spotbugsPlugins";
public static final String SLF4J_CONFIG_NAME = "spotbugsSlf4j";
public static final String EXTENSION_NAME = "spotbugs";

private final Logger log = LoggerFactory.getLogger(SpotBugsPlugin.class);
Copy link
Contributor

Choose a reason for hiding this comment

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

This could instead be LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); so that it can be copy-pasted to other classes. Also, why not static?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not so positive to make copy-paste friendly in this case. The code with MethodHandles isn't so intentional.

To avoid the pointed mistake, since we use JDK11 to build in CI, I wanna use Errorprone with SLF4J plugin that can detect it. I'll make separated PR.

Also, why not static?

It simply has no merit nor demerit. Plugin instance is created just few times per build.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ouch, we code in Groovy so Errorprone doesn't work. Now it's time to use SpotBugs! :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Note that PMD will flag non-static loggers as a mistake unless passed into the constructor: https://pmd.github.io/latest/pmd_rules_java_errorprone.html#properlogger

Why not make logging statements copy-paste friendly? They're gonna be copy-pasted at some point, may as well avoid the risk of forgetting to update the classname.

Copy link
Member Author

Choose a reason for hiding this comment

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

Confirmed that SpotBugs SLF4J plugin cannot detect the issue, because Groovy compiles this code into the following format:

        Object var5 = var4[0].call(LoggerFactory.class, Object.class);
        this.log = (Logger)ScriptBytecodeAdapter.castToType(var5, Logger.class);

so I'll follow your suggestion at this moment.

Copy link
Member Author

Choose a reason for hiding this comment

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

Why not make logging statements copy-paste friendly?

I believe it's not the way to go. And if I do, I will choose LoggerFactory.getLogger(getClass()) which is more intuitive code to read.

BTW static-or-not is not so problem, FAQ says both are OK. Then I personally prefer the less-magic way.

Copy link
Contributor

Choose a reason for hiding this comment

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

getClass() works for an instance logger, yes.

@sonarcloud
Copy link

sonarcloud bot commented Mar 5, 2020

Kudos, SonarCloud Quality Gate passed!

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities (and Security Hotspot 0 Security Hotspots to review)
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@KengoTODA KengoTODA merged commit f720493 into master Mar 5, 2020
@KengoTODA KengoTODA deleted the fix--196 branch March 5, 2020 07:03
@github-actions
Copy link

github-actions bot commented Mar 5, 2020

🎉 This PR is included in version 4.0.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

4.0.0 no longer included in "check"
2 participants