Skip to content

SonarCloud fix: java:S5411 Avoid using boxed "Boolean" types directly in boolean expressions #10242

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

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

Conversation

CDellaGiusta
Copy link
Contributor

@CDellaGiusta CDellaGiusta commented Apr 24, 2025

What does this PR change?

SonarCloud error reduction fix, rule java:S5411 Avoid using boxed "Boolean" types directly in boolean expressions

When boxed type java.lang.Boolean is used as an expression to determine the control flow (as described in Java Language Specification §4.2.5 The boolean Type and boolean Values) it will throw a NullPointerException if the value is null (as defined in Java Language Specification §5.1.8 Unboxing Conversion).

It is safer to avoid such conversion altogether and handle the null value explicitly.

Note, however, that no issues will be raised for Booleans that have already been null-checked or are marked @NonNull/@NotNull

GUI diff

No difference.

  • DONE

Documentation

  • No documentation needed: only internal and user invisible changes
  • DONE

Test coverage

  • No tests: already covered
  • DONE

Links

Issue(s): #9878, #9941
Port(s): not backported

  • DONE

Changelogs

  • No changelog needed

If you uncheck the checkbox after the PR is created, you will need to re-run changelog_test (see below)

Re-run a test

If you need to re-run a test, please mark the related checkbox, it will be unchecked automatically once it has re-run:

  • Re-run test "changelog_test"
  • Re-run test "backend_unittests_pgsql"
  • Re-run test "java_pgsql_tests"
  • Re-run test "schema_migration_test_pgsql"
  • Re-run test "susemanager_unittests"
  • Re-run test "javascript_lint"
  • Re-run test "spacecmd_unittests"

@CDellaGiusta CDellaGiusta requested review from a team as code owners April 24, 2025 05:54
@CDellaGiusta CDellaGiusta requested review from parlt91 and removed request for a team April 24, 2025 05:54
@CDellaGiusta CDellaGiusta self-assigned this Apr 24, 2025
@CDellaGiusta CDellaGiusta added the sonar-cloud-error-reduction all items related to reduce the number of errors in SonarCloud label Apr 24, 2025
@CDellaGiusta CDellaGiusta requested review from cbosdo and mackdk April 24, 2025 05:54
Copy link
Contributor

👋 Hello! Thanks for contributing to our project.
Acceptance tests will take some time (aprox. 1h), please be patient ☕

You can see the progress at the end of this page and at https://github.com/uyuni-project/uyuni/pull/10242/checks
Once tests finish, if they fail, you can check 👀 the cucumber report. See the link at the output of the action.
You can also check the artifacts section, which contains the logs at https://github.com/uyuni-project/uyuni/pull/10242/checks.

If you are unsure the failing tests are related to your code, you can check the "reference jobs". These are jobs that run on a scheduled time with code from master. If they fail for the same reason as your build, it means the tests or the infrastructure are broken. If they do not fail, but yours do, it means it is related to your code.

Reference tests:

KNOWN ISSUES

Sometimes the build can fail when pulling new jar files from download.opensuse.org . This is a known limitation. Given this happens rarely, when it does, all you need to do is rerun the test. Sorry for the inconvenience.

For more tips on troubleshooting, see the troubleshooting guide.

Happy hacking!
⚠️ You should not merge if acceptance tests fail to pass. ⚠️

@CDellaGiusta CDellaGiusta force-pushed the sonar-S5411-booleans branch from 68cf787 to a8e35d4 Compare April 24, 2025 06:59
}

if (localeCmd.getKickstartData().isUsingUtc() &&
if (BooleanUtils.isTrue(localeCmd.getKickstartData().isUsingUtc()) &&
Copy link
Contributor

Choose a reason for hiding this comment

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

isUsingUtc() can be converted to return a boolean:

    /**
     * Will the system hardware clock use UTC
     *
     * @return Boolean Are we using UTC?
     */
    public Boolean isUsingUtc() {
        KickstartCommand tzCommand = this.getCommand("timezone");

        if (tzCommand == null || tzCommand.getArguments() == null) {
            return Boolean.FALSE;
        }

        List<String> tokens = StringUtil.stringToList(tzCommand.getArguments());

        for (String token : tokens) {
            if (token.equals("--utc")) {
                return Boolean.TRUE;
            }
        }

        return Boolean.FALSE;
    }

@CDellaGiusta CDellaGiusta force-pushed the sonar-S5411-booleans branch 2 times, most recently from d748372 to 1d77287 Compare April 24, 2025 10:29
@CDellaGiusta CDellaGiusta requested a review from mackdk April 24, 2025 10:29
Comment on lines +81 to +88
private String getType(SystemJson s) {
if (BooleanUtils.isTrue(s.getPhysical())) {
return (BooleanUtils.isTrue(s.getVirtualHost()) ? "virtualHost" : "nonVirtual");
}
else {
return "virtualGuest";
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpicking here, but you can invert and unwrap the ifs to make the logic more evident:

Suggested change
private String getType(SystemJson s) {
if (BooleanUtils.isTrue(s.getPhysical())) {
return (BooleanUtils.isTrue(s.getVirtualHost()) ? "virtualHost" : "nonVirtual");
}
else {
return "virtualGuest";
}
}
private String getType(SystemJson s) {
if (BooleanUtils.isFalse(s.getPhysical())) {
return "virtualGuest";
}
if (BooleanUtils.isTrue(s.getVirtualHost())) {
return "virtualHost";
}
return "nonVirtual";
}

Copy link
Contributor

@cbosdo cbosdo left a comment

Choose a reason for hiding this comment

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

I can only approve @mackdk comments, the rest looks good to me.

@CDellaGiusta CDellaGiusta force-pushed the sonar-S5411-booleans branch 2 times, most recently from a54ecb4 to a7113e0 Compare May 22, 2025 07:11
@CDellaGiusta CDellaGiusta force-pushed the sonar-S5411-booleans branch from a7113e0 to f82e098 Compare May 30, 2025 08:52
@CDellaGiusta CDellaGiusta force-pushed the sonar-S5411-booleans branch from f82e098 to 5d3fdf1 Compare June 26, 2025 08:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants