-
Notifications
You must be signed in to change notification settings - Fork 41.2k
Add rule to prevent calls to Objects.requireNonNull() #41611
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
Conversation
@ivamly Please sign the Contributor License Agreement! Click here to manually synchronize the status of this Pull Request. See the FAQ for frequently asked questions. |
@ivamly Thank you for signing the Contributor License Agreement! |
I've reviewed the contribution guidelines and considered adding Javadoc comments to ArchitectureCheck.java and ArchitectureCheckTests.java. However, I wasn't sure if it was necessary to include Javadoc for these classes, so I have refrained from adding them for now. If it's required, please let me know, and I will update the code accordingly. |
@wilkinsona I noticed from the build report that the build is failing due to missing newline characters at the end of my files. What should I do to resolve this issue? Do I need to make another commit, update the pull request, or take any other action? Apologies if this seems like a basic question—I'm still new to this process. Thank you for your help! |
Either we can take care of it as part of merging the changes, or you can push another commit to your |
I'll make a commit tonight. |
It looks like there's a bug in ArchUnit that's causing it to report many false positives. It trips up on code that uses a method reference such as this: public void someCodeThatUsesAMethodReference() {
List.of("1", "2", "3").forEach(System.out::println);
} We'll have to see if there's a workaround or if there's a fix available. |
Thank you for the update. That's interesting. Is there anything I can do? I’m happy to assist if needed. |
It isn't a bug in ArchUnit, but an unfortunate side effort of the bytecode that's produced by a method reference. The example above looks like this:
Note 13 where it's calling
Thanks for the offer. Could you please update the PR so that it doesn't prohibit |
Thanks for the clarification! This is really interesting. I’ll update the PR within an hour. |
Thanks very much, @ivamly. |
Add Check for Objects.requireNonNull Usage and Corresponding Tests
This pull request for issue ticket #41603 introduces a new architecture check for ensuring that Objects.requireNonNull is not used directly. Instead, the Assert.notNull method from Spring Framework should be used. Additionally, this update includes new test cases and necessary files to validate this architecture rule.
Changes:
New Architecture Check:
Added a method to the ArchitectureCheck class to verify that Objects.requireNonNull is not used within the codebase.
Test Cases:
Implemented tests to validate the new architecture check:
RequireNonNullUsage: Tests that the direct use of Objects.requireNonNull in the code results in a failure.
AssertNotNullUsage: Tests that using Assert.notNull is allowed and does not cause a failure.
File Updates:
Created the necessary files and classes for testing:
RequireNonNullUsage: Contains a method using Objects.requireNonNull to trigger the architecture check.
AssertNotNullUsage: Contains a method using Assert.notNull to verify that it does not violate the rule.
These changes help enforce best practices for null-checking within the project and ensure that the code adheres to the preferred use of Spring utilities.