Skip to content

Conversation

@stefanodallapalma
Copy link
Contributor

@stefanodallapalma stefanodallapalma commented Sep 11, 2025

Problem

The AnnotateNullableParameters recipe currently uses a hardcoded list of known null-checking methods (e.g., Objects.isNull(), StringUtils.isEmpty()). While this covers common utility libraries, many teams have custom utility methods for null checking that the recipe doesn't recognize.

Example scenario:

public PersonBuilder setInfo(String name, String lastName) {
    if (MyUtils.hasText(name)) {        // ❌ Custom method not recognized
        this.name = name;
    }
    if (Objects.isNull(lastName)) {     // ✅ Built-in method recognized  
        this.lastName = lastName;
    }
    return this;
}

Currently, only lastName would get the @Nullable annotation despite both parameters being null-checked.

Solution

Added an optional additionalNullCheckingMethods parameter that accepts a list of custom null-checking method patterns in OpenRewrite's MethodMatcher format.

Key features:

  • Backward compatible - All existing functionality preserved
  • 🔧 Configurable - Teams can specify their custom null-checking utilities
  • 📝 Flexible patterns - Supports OpenRewrite's method matching syntax

Usage

Programmatic:

List<String> customMethods = List.of(
    "org.my.util.Text hasText(java.lang.String)",
    "org.my.util.Text isEmptyOrNull(java.lang.String)"
);
var recipe = new AnnotateNullableParameters(null, customMethods);

YAML configuration:

- org.openrewrite.staticanalysis.AnnotateNullableParameters:
    additionalNullCheckingMethods:
      - "org.my.util.Text hasText(..))"
      - "org.my.util.Text isEmptyOrNull(..)"

Result:

// Both parameters now get annotated
public PersonBuilder setInfo(@Nullable String name, @Nullable String lastName) {
    if (Text.hasText(name)) {           // ✅ Now recognized
        this.name = name;
    }
    if (!Text.isEmptyOrNull(lastName)) { // ✅ Now recognized (negated)
        this.lastName = lastName;
    }
    return this;
}

Implementation Details

  • Method patterns are combined with the default list at runtime
  • Supports both specific parameter types and wildcard patterns (..)
  • Works with negated conditions (!customMethod(param))

Testing

  • ✅ Validates both positive and negated null checks
  • ✅ Backward compatibility verified - all existing tests pass
  • ❌ Invalid patterns not covered

Motivation

This enhancement enables teams with custom utility libraries to benefit from automated nullable annotation without needing to modify their existing code patterns. It's particularly valuable for:

  • Legacy codebases with established utility patterns
  • Organizations with domain-specific null-checking methods

@timtebeek

Relates to: #578

@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Sep 11, 2025
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Some suggestions could not be made:

  • src/main/java/org/openrewrite/staticanalysis/AnnotateNullableParameters.java
    • lines 68-72
    • lines 200-201

timtebeek and others added 2 commits September 12, 2025 14:23
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Copy link
Member

@timtebeek timtebeek left a comment

Choose a reason for hiding this comment

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

Thanks for your continued involvement here! I've added a light bit of touch up to avoid unnecessary array allocations per visitor instance, and to use proper markdown in the docs. 🙏🏻

@github-project-automation github-project-automation bot moved this from In Progress to Ready to Review in OpenRewrite Sep 12, 2025
@timtebeek timtebeek merged commit 0f4b3bd into openrewrite:main Sep 12, 2025
2 checks passed
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Sep 12, 2025
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Some suggestions could not be made:

  • src/main/java/org/openrewrite/staticanalysis/AnnotateNullableParameters.java
    • lines 66-70

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

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants