🌐 AI搜索 & 代理 主页
Skip to content

Conversation

@rmacqueen
Copy link
Contributor

@rmacqueen rmacqueen commented Aug 9, 2025

Summary

This PR extends DefaultLibraryModels to recognize additional null-check helper methods from common libraries, ensuring NullAway treats them as guarding against null values.

Added support for:

  • org.apache.commons.collections.CollectionUtils.isEmpty(Collection)
  • org.apache.commons.collections4.CollectionUtils.isEmpty(Collection<?>)
  • software.amazon.awssdk.utils.CollectionUtils.isNullOrEmpty(Collection<?>)

Each method is now mapped so that NullAway understands they perform null checks on their first argument.

Added corresponding unit tests in FrameworkTests to validate behavior for Apache Commons Collections, Apache Commons Collections4, and AWS CollectionUtils.

Impact
With these changes, NullAway will correctly recognize these helper methods as null guards, reducing false positives when developers use them to validate collections or objects before dereferencing.

Summary by CodeRabbit

  • New Features

    • Expanded recognition of common null/empty guards to include additional collection and string utility methods (Apache Commons, Commons Lang/3, AWS SDK utils, Spark utils, Android TextUtils), improving null-safety analysis.
  • Tests

    • Added tests verifying null/empty guard handling for Apache Commons Collections, Commons Collections4, AWS SDK utils, and AWS StringUtils isEmpty/isBlank paths.
  • Chores

    • Added AWS SDK utils as a test dependency.

@CLAassistant
Copy link

CLAassistant commented Aug 9, 2025

CLA assistant check
All committers have signed the CLA.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Aug 9, 2025

Walkthrough

Added 19 library-model method entries into two internal maps to mark various utility methods as null/empty guards; added four positive tests exercising those guards; added the AWS SDK utils test dependency and wired it into the nullaway test configuration.

Changes

Cohort / File(s) Change Summary
Library model updates
nullaway/src/main/java/com/uber/nullaway/handlers/LibraryModelsHandler.java
Added 19 method references across two private maps: five entries to FAIL_IF_NULL_PARAMETERS (param index 0) and fourteen entries to NULL_IMPLIES_TRUE_PARAMETERS (param index 0). Methods added include various CollectionUtils.isEmpty/isNullOrEmpty, StringUtils.isEmpty/isBlank, and ObjectUtils.isEmpty variants from Apache Commons, Apache Commons Collections4, AWS SDK utils, Spark utils, Android TextUtils, and Commons Lang. No other logic or data-structure changes.
Test additions
nullaway/src/test/java/com/uber/nullaway/FrameworkTests.java
Added four new positive tests: apacheCollectionsCollectionUtilsIsEmpty, apacheCollections4CollectionUtilsIsEmpty, amazonAwsUtilCollectionUtilsIsNullOrEmpty, and amazonAwsStringUtilsIsEmptyOrIsBlank. Each verifies NullAway treats the respective utility guards as preventing null dereferences.
Build configuration
gradle/dependencies.gradle, nullaway/build.gradle
Added test dependency entry amazonUtils = "software.amazon.awssdk:utils:2.32.19" and added deps.test.amazonUtils to the testImplementation configuration for the nullaway module.

Sequence Diagram(s)

sequenceDiagram
    participant Test as Test code
    participant Analyzer as NullAway Analyzer
    participant Models as LibraryModelsHandler

    Test->>Analyzer: Analyze guarded dereference (call to utility e.g., isEmpty/isNullOrEmpty/isBlank)
    Analyzer->>Models: Query if invoked method implies non-null for parameter 0
    Models-->>Analyzer: Yes (method present in FAIL_IF_NULL_PARAMETERS / NULL_IMPLIES_TRUE_PARAMETERS)
    Analyzer-->>Test: Mark subsequent dereference as safe
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cb66393 and e1d319a.

📒 Files selected for processing (2)
  • nullaway/src/main/java/com/uber/nullaway/handlers/LibraryModelsHandler.java (1 hunks)
  • nullaway/src/test/java/com/uber/nullaway/FrameworkTests.java (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • nullaway/src/test/java/com/uber/nullaway/FrameworkTests.java
  • nullaway/src/main/java/com/uber/nullaway/handlers/LibraryModelsHandler.java
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
nullaway/src/main/java/com/uber/nullaway/handlers/LibraryModelsHandler.java (1)

716-730: Optional: broaden coverage to common AWS/string and map variants.

If in scope, consider adding these to reduce false positives across more codebases:

  • com.amazonaws.util.StringUtils.isNullOrEmpty(String)
  • com.amazonaws.util.CollectionUtils.isNullOrEmpty(java.util.Map)

Proposed additions near this block:

             .put(
                 methodRef(
                     "com.amazonaws.util.CollectionUtils",
                     "isNullOrEmpty(java.util.Collection<?>)"),
                 0)
+            .put(
+                methodRef(
+                    "com.amazonaws.util.CollectionUtils",
+                    "isNullOrEmpty(java.util.Map<?,?>)"),
+                0)
+            .put(
+                methodRef(
+                    "com.amazonaws.util.StringUtils",
+                    "isNullOrEmpty(java.lang.String)"),
+                0)

Please verify these signatures against your dependency versions before adding.

nullaway/src/test/java/com/uber/nullaway/FrameworkTests.java (1)

1071-1129: Add a negative test to cover the then-branch behavior.

For completeness/regression protection, add a case that asserts an error when dereferencing within the then-branch:

  • if (CollectionUtils.isEmpty(s)) { s.get(0); } // expect diagnostic

This ensures the cast applies only to the else-branch for null-implies-true models.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7b64688 and 4273dc6.

📒 Files selected for processing (2)
  • nullaway/src/main/java/com/uber/nullaway/handlers/LibraryModelsHandler.java (1 hunks)
  • nullaway/src/test/java/com/uber/nullaway/FrameworkTests.java (1 hunks)
🔇 Additional comments (5)
nullaway/src/main/java/com/uber/nullaway/handlers/LibraryModelsHandler.java (1)

716-730: Models correctly extend null-implies-true coverage for common collection helpers.

Signatures and package names look correct and consistent with existing entries. This will enable the expected else-branch non-null cast for guarded collections.

nullaway/src/test/java/com/uber/nullaway/FrameworkTests.java (4)

1071-1089: LGTM: positive test for Apache Commons Collections (v3) isEmpty guard.

Pattern matches null-guard via early return; exercises else-branch non-null inference.


1091-1109: LGTM: positive test for Apache Commons Collections4 isEmpty guard.

Covers the generics variant; consistent with library model entry.


1111-1129: LGTM: positive test for AWS CollectionUtils.isNullOrEmpty(Collection<?>) guard.

Complements the new model entry and typical usage patterns.


1071-1129: Verify external CollectionUtils stubs are available

I couldn’t locate any stub implementations for the imported CollectionUtils classes in the repository. Please confirm that the following stub files exist in your test stub directory (and are on the compilation classpath):

  • org/apache/commons/collections/CollectionUtils.java with
    public static boolean isEmpty(Collection<?>)
  • org/apache/commons/collections4/CollectionUtils.java with
    public static <T> boolean isEmpty(Collection<T>)
  • com/amazonaws/util/CollectionUtils.java with
    public static <T> boolean isNullOrEmpty(Collection<T>)

Without these, the tests in FrameworkTests.java (lines 1071–1129) will fail to compile.

@rmacqueen rmacqueen force-pushed the master branch 2 times, most recently from f153f1b to 32b5d63 Compare August 9, 2025 01:01
@msridhar
Copy link
Collaborator

msridhar commented Aug 9, 2025

Thanks for the contribution @rmacqueen! Does the suggestion below from the AI bot make any sense? If it does maybe we could handle it in this PR? Otherwise the change looks good.

nullaway/src/main/java/com/uber/nullaway/handlers/LibraryModelsHandler.java (1)> 716-730: Optional: broaden coverage to common AWS/string and map variants.

If in scope, consider adding these to reduce false positives across more codebases:

  • com.amazonaws.util.StringUtils.isNullOrEmpty(String)
  • com.amazonaws.util.CollectionUtils.isNullOrEmpty(java.util.Map)

Proposed additions near this block:

             .put(
                 methodRef(
                     "com.amazonaws.util.CollectionUtils",
                     "isNullOrEmpty(java.util.Collection<?>)"),
                 0)
+            .put(
+                methodRef(
+                    "com.amazonaws.util.CollectionUtils",
+                    "isNullOrEmpty(java.util.Map<?,?>)"),
+                0)
+            .put(
+                methodRef(
+                    "com.amazonaws.util.StringUtils",
+                    "isNullOrEmpty(java.lang.String)"),
+                0)

@rmacqueen
Copy link
Contributor Author

@msridhar Sure that does make sense! Will add update my PR to add those

Copy link
Collaborator

@msridhar msridhar left a comment

Choose a reason for hiding this comment

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

LGTM! If you run ./gradlew spotlessApply that should fix the formatting errors that CI is complaining about. Also, if you push all commits under the rmacqueen GitHub account that should fix the CLA check.

@rmacqueen rmacqueen force-pushed the master branch 3 times, most recently from a370abd to dacfb73 Compare August 10, 2025 04:14
@msridhar msridhar enabled auto-merge (squash) August 10, 2025 13:58
@msridhar
Copy link
Collaborator

@rmacqueen looks like a test is failing. Could you take a look?

@msridhar msridhar disabled auto-merge August 10, 2025 14:53
@msridhar
Copy link
Collaborator

@rmacqueen I think the issue with the failing test is that the fully-qualified class name for the Amazon class is software.amazon.awssdk.utils.CollectionUtils. I pushed some fixes related to this but the library models class still needs to be fixed, could you do it?

Also, if you could not force push that would be great, as it loses the commit history. We will squash commits before merging.

@msridhar
Copy link
Collaborator

Looks like there are 1.x and 2.x versions of the Amazon utilities, and the package naming changed from com.amazon.xyz to software.amazon.xyz. Do we want to support both?

@rmacqueen
Copy link
Contributor Author

@msridhar Thanks for pointing that out. I’ll update the library models class to use software.amazon.awssdk.utils.CollectionUtils so the test passes.

Also noted on avoiding force pushes; I’ll keep the commit history intact going forward.

@rmacqueen
Copy link
Contributor Author

Let’s just support 2.x going forward. I’ll update the library models to only include software.amazon.*

@codecov
Copy link

codecov bot commented Aug 12, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.50%. Comparing base (7b64688) to head (e1d319a).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #1242      +/-   ##
============================================
+ Coverage     88.48%   88.50%   +0.02%     
  Complexity     2373     2373              
============================================
  Files            90       90              
  Lines          7787     7802      +15     
  Branches       1555     1555              
============================================
+ Hits           6890     6905      +15     
  Misses          452      452              
  Partials        445      445              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@msridhar msridhar changed the title Add library model for Apache Commons CollectionUtils.isNotEmpty and Amazon CollectionUtils.IsNullOrEmpty Add library model for Apache Commons CollectionUtils.isNotEmpty, Amazon CollectionUtils.IsNullOrEmpty, and a couple Amazon StringUtils methods Aug 12, 2025
@msridhar msridhar enabled auto-merge (squash) August 12, 2025 23:14
@msridhar
Copy link
Collaborator

Thanks again for the contribution @rmacqueen!! I tweaked the StringUtils models slightly and added tests for them.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
nullaway/src/main/java/com/uber/nullaway/handlers/LibraryModelsHandler.java (1)

726-731: Add the Map<?> variant for AWS CollectionUtils.isNullOrEmpty for completeness

AWS SDK v2 also provides isNullOrEmpty(Map). Modeling it here will extend the same guard semantics to map checks.

Apply this diff within the same NULL_IMPLIES_TRUE_PARAMETERS builder:

             .put(
                 methodRef(
                     "software.amazon.awssdk.utils.CollectionUtils",
                     "isNullOrEmpty(java.util.Collection<?>)"),
                 0)
+            .put(
+                methodRef(
+                    "software.amazon.awssdk.utils.CollectionUtils",
+                    "isNullOrEmpty(java.util.Map<?,?>)"),
+                0)
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between dd729c1 and cb66393.

📒 Files selected for processing (2)
  • nullaway/src/main/java/com/uber/nullaway/handlers/LibraryModelsHandler.java (1 hunks)
  • nullaway/src/test/java/com/uber/nullaway/FrameworkTests.java (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • nullaway/src/test/java/com/uber/nullaway/FrameworkTests.java
🔇 Additional comments (2)
nullaway/src/main/java/com/uber/nullaway/handlers/LibraryModelsHandler.java (2)

716-738: Correct: model these helpers under NULL_IMPLIES_TRUE

These entries correctly capture “null implies true” semantics for the Apache Commons and AWS SDK v2 helpers. This will tighten flow analysis (else-branch implies NONNULL) without over-constraining arguments.


732-738: FAIL_IF_NULL_PARAMETERS block is clean of AWS/Apache helpers

Verified the FAIL_IF_NULL_PARAMETERS builder in LibraryModelsHandler.java and found no references to
software.amazon.awssdk.utils.{StringUtils,CollectionUtils} or
org.apache.commons.collections.CollectionUtils. No further changes needed.

@msridhar msridhar disabled auto-merge August 12, 2025 23:17
@rmacqueen
Copy link
Contributor Author

@msridhar Many thanks! Ah and I see you updated the author again too. Sorry about that! Really appreciate you helping get this PR in!

@msridhar msridhar merged commit ba4ff83 into uber:master Aug 12, 2025
13 checks passed
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.

3 participants